SpringBatch from entry to abandon 004- Listener

first JobListener

During the execution of the Job, it is necessary to inject a custom code in the right place. Spring Batch provides JobExecutionListener interface, and provides the corresponding code executed according to the state of Job Job execution before and after execution. With Listener behind the difference is, Job Listener in afterJob whether it is to be executed in the Job success or failure will be. JobExecutionListener interface is shown below:

XML implementation class may be added (sampleListener) by a JobExecutionListener interface configuration,

<job id="footballJob">
      <step id="playerload"          parent="s1" next="gameLoad"/>
      <step id="gameLoad"            parent="s2" next="playerSummarization"/>
      <step id="playerSummarization" parent="s3"/>
      <listeners>
          <listener ref="sampleListener"/>
      </listeners>
</job>

Java-base configuration can be configured in the following ways:

 @Bean
  public Job footballJob() {
      return this.jobBuilderFactory.get("footballJob")
                       .listener(sampleListener())
                       ...
                       .build();
}

Similarly, we can also use annotations manner Listener:

@BeforeJob
public void executeBeforeJob(JobExecution jobExecution){
    …………
}

@AfterJob
public void executeAfterJob(JobExecution jobExecution){
    …………
}

2. StepListener

And Job, as there are many events during the execution of steps, the user may need to perform certain functions. If realized StepListener interface ItemReader / ItemProcessoe / ItemWriter three, and is injected directly into the Step, then they would be injected into a default Listener. As with Job Listener, Step-level Listener also provides a way to achieve comment Listener function. Note that listeners need to declare an element in the step, tasklet or chunk at three levels:
the Step-level Listener as follows:

* StepExecutionListener 
* ChunkListener
* ItemReadListener 
* ItemProcessListener 
* ItemWriteListener 
* SkipListener 

StepExecutionListener

Step perform the most common listener, who listens provided before and after the end of Step Step beginning, the same, regardless of the outcome of success or failure can be monitored. Interface is defined as follows:

We note afterStep the return value is no longer a void but ExitStatus, which is to this end that we can change the state after the end of Step.
Spring Batch provides the same support for annotations:

 @BeforeStep
 @AfterStep

ChunkListener

Chunk defined once a transaction commit process, each commit interval committing a transaction. ChunkListener provides Listener and after transaction execution after the start of the transaction, the rollback is not performed if there is afterChunk ():

ChunkListener notes as follows:

• @BeforeChunk
• @AfterChunk
• @AfterChunkError

If we did not use the standard format Batch, but uses the interface to implement a Tasklet, then Tasklet method ChunkListener responsible for the call.

ItemReadListener

ItemReadListener interface definition:

ItemReadListener notes:

• @BeforeRead 
• @AfterRead
• @OnReadError

Call beforeRead ItemReader method before each call. Call afterRead method after each successful call to read, and pass the item read. An error occurred while reading, onReadError method is called. It provides an exception encountered, so that you can record it.

ItemProcessListener

ItemProcessListener interface definition:

ItemProcessListener comments:

• @BeforeProcess
• @AfterProcess
• @OnProcessError

beforeProcess ItemProcessor on the method of process () is called before, and passed to the item to be processed. Call afterProcess method after a successful treatment program. An error occurred while processing, onProcessError method is called. An exception is encountered, and provides project attempts to deal with in order to record them.

ItemWriteListener

ItemWriteListener interface definition:

ItemWriteListener interfaces annotated as follows:

• @BeforeWrite 
• @AfterWrite
• @OnWriteError

在write()执行之前调用beforeWrite方法,并将其写入已写入的项列表。成功写入项后调用afterWrite方法。如果写入时出错,则调用onWriteError方法。遇到异常并尝试写入项目,以便记录它们。

SkipListener

ItemReadListener,ItemProcessListener和ItemWriteListener都提供了发生错误的时候的回调方法,但没有一个接口在跳过了一条记录的时候进行回调。例如,即使重试并成功执行某个项,也会调用onWriteError。因此,有一个单独的Listener用于跟踪跳过的项目,这个 Listener 就是SkipListener,接口定义如下:

SkipListener注解如下:

• @OnSkipInRead
• @OnSkipInWrite
• @OnSkipInProcess

在读取时跳过某个项目,就会调用onSkipInRead。应该注意的是,回滚可能导致同一项目被注册为多次跳过。在写入时跳过项目时会调用onSkipInWrite。由于该项已成功读取(并且未被跳过),因此它还将项目本身作为参数提供。

SkipListener最常见的用例之一是注销跳过的项目,以便可以使用另一个批处理过程甚至人工过程来评估和修复导致跳过的问题。也就是说如果发生了异常,SkipListener 并不会立即执行,而是等最终事务提交前触发。如果在读阶段发生了Skip异常,那么框架会放弃当前的数据,直接读下一条数据,如果是Processor 阶段发生了Skip异常,会回滚事务,并用下一条数据进行新的处理,如果 Writer 阶段发生了Skip异常,因为提交的时候可能是多条批量提交的,所以 Writer 并不知道是那一条发生了 Skip异常,那么 Writer会为这次事务中的所有记录每一个都起一个新事务进行提交。

3. RepeatListener

通常,能够在每一次的重启中触发额外的回调是非常有用的。因此Spring Batch提供了RepeatListener接口,允许在重启的时候使用RepeatContext和RepeatStatus给出回调。
RepeatListener接口具有以下定义:

open()和close() 会在所有重试发生的时候回调。 before,after和onError 适用于个别的 RepeatCallback调用。
需要注意的是如果定义了多个 RepeatListener,那么open/before 是按照定义的顺序执行,after/close 则是按照的定义的反序执行。

4. RetryListener

通常,能够在每一次的重试中触发额外的回调也是非常有用的。因此Spring Batch提供了RetryListener接口,允许用户在重试时RetryContext和Throwable提供回调。
以下代码显示了RetryListener的接口定义:

最简单的情况下,open()和close() 在每一次的重试前后执行,onError适用于个别 RetryCallback调用。 close方法也可能会收到Throwable。如果出现错误,则它是RetryCallback抛出的最后一个错误。

需要注意的是如果定义了多个 RetryListener,那么open/before 是按照定义的顺序执行,after/close 则是按照的定义的反序执行

5. 各个 Listener 的执行顺序

  1. JobExecutionListener.beforeJob()
  2. StepExecutionListener.beforeStep()
  3. ChunkListener.beforeChunk()
  4. ItemReaderListener.beforeReader()
  5. ItemReaderListener.afterReader()
  6. ItemProcessListener.beforeProcess()
  7. ItemProcessListener.afterProcess()
  8. ItemWriterListener.beforeWriter()
  9. ItemWriterListener.afterWriter()
  10. ChunkListener.afterChunk()
  11. StepExecutionListener.afterStep()
  12. JobExecutionListener.afterJob()

需要注意的是如果在同一个级别定一个多个 Listener,比如 parent Step中定一个了一个 StepListener, 自身又定义了一个 StepListener ,如果需要两个都执行,那么需要在自身 listener 上加上merge 属性。

Batch 提供了丰富的 Listener 来方便我们在各个过程中加入自己的代码。如果还不能满足你的需求,那就需要你自己创建自定的 Listener,鹏哥不推荐这种实现,也不常见,这边就不做介绍了,好了,Batch的所有 Listener 就介绍完了,下一篇我们将了解 Batch 的重试和重启。

Guess you like

Origin www.cnblogs.com/ckp-henu/p/springbatch-cong-ru-men-dao-fang-qi004-listener.html