Hello,
I have also tried my luck with the Hello World example, and made it run in its basic configuration. I then tried to expand it, so that I can save details about the jobExecution and the stepExecution in the executionContext.
I did it by implementing the StepExecutionListener interface in the Tasklet class. That gives me access to the stepExecution (without changing the configuration of the job ??!), but the Id (of the stepExecution) is null.
Can any of you guide me in the right direction ? My goal is to get access to the stepExecutionId and the jobExecutionId from the Tasklet class.
helloWorldJob.xml
PrintTasklet:Code:<beans xmlns=...> <import resource="helloWorldApplicationContext.xml"/> <bean id="printtasklet" class="dk.bec.helloworld.PrintTasklet"/> <bean id="hello" parent="printtasklet"> <property name="message" value="Hello"/> </bean> <bean id="space" parent="printtasklet"> <property name="message" value=" "/> </bean> <bean id="world" parent="printtasklet"> <property name="message" value="World!"/> </bean> <bean id="taskletStep" abstract="true" class="org.springframework.batch.core.step.tasklet.TaskletStep"> <property name="jobRepository" ref="jobRepository"/> <!-- The below configuration line gives "No property 'listeners' found" --> <!-- <property name="listeners" ref="printtasklet"/> --> </bean> <bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob"> <property name="name" value="simpleJob" /> <property name="steps"> <list> <bean parent="taskletStep"> <property name="tasklet" ref="hello"/> </bean> <bean parent="taskletStep"> <property name="tasklet" ref="space"/> </bean> <bean parent="taskletStep"> <property name="tasklet" ref="world"/> </bean> </list> </property> <property name="jobRepository" ref="jobRepository"/> </bean> </beans>
Code:package dk.bec.helloworld; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.ExitStatus; public class PrintTasklet implements Tasklet, StepExecutionListener{ private String message; StepExecution stepExecution; public void setMessage(String message) { this.message = message; } public ExitStatus execute() throws Exception { System.out.print(message); stepExecution.getExecutionContext().putString("Message", message); stepExecution.getExecutionContext().putDouble("Time", System.currentTimeMillis()); stepExecution.getExecutionContext().putLong("JobExecutionId", stepExecution.getJobExecutionId().longValue()); // The .getId() gives java.lang.NullPointerException: // stepExecution.getExecutionContext().putLong("Id", stepExecution.getId().longValue()); return ExitStatus.FINISHED; } public ExitStatus afterStep(StepExecution stepExecution) { // TODO Auto-generated method stub return null; } public void beforeStep(StepExecution stepExecution) { this.stepExecution = stepExecution; } public ExitStatus onErrorInStep(StepExecution stepExecution, Throwable e) { // TODO Auto-generated method stub return null; } }


Reply With Quote