Step listener

The step listener acts on a single step. Like the job listener, for each step, we have two methods: a start and an end:

@Named
public class StepCheckpointListener implements StepListener {
...
public final static string STARTED_AFTER_STEP = "started after step";
public final static string BEFORE_STEP_PROPERTY = "beforeStepProperty";
@Inject
private JobContext jobContext;
@Inject
private StepContext stepContext;
@Override
public void beforeStep() throws Exception {
Properties jobProperties = jobContext.getProperties();
Properties stepProperties = stepContext.getProperties();
stepProperties.put(BEFORE_STEP_PROPERTY, "Before step property");
...
}
@Override
public void afterStep() throws Exception {
CheckpointUserData checkpointUserData = (CheckpointUserData) jobContext.getTransientUserData();
checkpointUserData.setStartedAfterStep(STARTED_AFTER_STEP);
...
PersistentCheckpointUserData persistentCheckpointUserData = new PersistentCheckpointUserData();
persistentCheckpointUserData.setStartedAfterStep
(
STARTED_AFTER_STEP);
stepContext.setPersistentUserData
(
persistentCheckpointUserData);
...
}
}

The listener implements the javax.batch.api.listener. The StepListener interface provides the beforeStep() and afterStep() methods. As for the job listener, it will be executed for each returning state, either with or without errors. In this sample, the beforeStep() method adds a property in the step context, so it can be read and elaborated only inside the step. After the end of the current step, a persistent user data is created. This object is alive only inside the step context. When it closes and another step is created, this object will be hibernated and reused in the next start of the job because it is persistent. Here's the PersistentCheckpointUserData object:

public class PersistentCheckpointUserData extends CheckpointUserData implements Serializable {
...
}

It simply extends the CheckpointUserData class seen in the Job listener section. The important thing is that this class implements the Java.io.Serializable interface to guarantee the persistence and serialization of the object.
This is the declaration of the step context in the JSL descriptor file:

<job version="1.0" id="CheckpointJob" restartable="true"
xmlns="http://xmlns.jcp.org/xml/ns/javaee">
...
<step id="step1">
<listeners>
<listener ref="stepCheckpointListener">
<properties partition="0">
<property name="name1" value="value1-stepcheckpointlistener" />
<property name="name2" value="value2-stepcheckpointlistener" />
</properties>
</listener>
...

As for the job listener, the position of the declaration inside the step tag is important. The name of the listener reference is the name of the class declared as @Named, in lowercase. Automatically, the beans are referenced in this mode if the name of the reference is not declared. Refer to Chapter 2 , Working with Dependency Injection, for more details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.8