Java Reference
In-Depth Information
@BeforeStep , @AfterStep , @BeforeChunk , and @AfterChunk annotations to simplify the implementation.
Listing 4-38 shows a StepListener that uses annotations to identify the methods.
Listing 4-38. Logging Step Start and Stop Listeners
package com.apress.springbatch.chapter4;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.core.annotation.BeforeStep;
public class LoggingStepStartStopListener {
@BeforeStep
public void beforeStep(StepExecution execution) {
System.out.println(execution.getStepName() + “ has begun!");
}
@AfterStep
public ExitStatus afterStep(StepExecution execution) {
System.out.println(execution.getStepName() + “ has ended!");
return execution.getExitStatus();
}
}
The configuration for all the step listeners is combined into a single list in the step configuration.
Similar to the job listeners, inheritance works the same way, allowing you to either override the list or
merge them together. Listing 4-39 configures the LoggingStepStartStopListener that you coded earlier.
Listing 4-39 . Configuring LoggingStepStartStopListener
...
<beans:bean id="loggingStepListener"
class="com.apress.springbatch.chapter4.LoggingStepStartStopListener"/>
<job id="stepListenerJob">
<step id="step1">
<tasklet>
<chunk reader="inputReader" writer="outputWriter"
commit-interval="50"/>
<listeners>
<listener ref="loggingStepListener"/>
</listeners>
</tasklet>
</step>
</job>
...
As you can see, listeners are available at just about every level of the Spring Batch framework to
allow you to hang processing off your batch jobs. They're commonly used not only to perform some
 
Search WWH ::




Custom Search