I have a requirement in which a tasklet, stores all the files in the directories in an arraylist. The size of the list is stored in the job execution context. Later this count is accessed from another tasklet in another step. How do it do this. I tried to store in jobexecution context, at runtime throws unmodifiable collection exception,
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { StepContext stepContext = arg1.getStepContext(); StepExecution stepExecution = stepContext.getStepExecution(); JobExecution jobExecution = stepExecution.getJobExecution(); ExecutionContext jobContext = jobExecution.getExecutionContext(); jobContext.put("FILE_COUNT",150000);
also stored the stepexection reference in beforestep annotation .still not possioble.kindly let me know ,how to share data between two tasklets.
It is often useful to pass information from one step to another. This can be done through the ExecutionContext . The catch is that there are two ExecutionContexts : one at the Step level and one at the Job level.
Data passing between steps using tasklet model In order to save and fetch passing data, get ExecutionContext from ChunkContext and pass the data between the steps. Sr. No. Set the value to be passed to the after step in the ExecutionContext of the step execution context.
Make your item writer with step scope, then make use of expression like #{jobParameters['theKeyYouWant']} or #{jobExecutionContext['someOtherKey']} for value injecting to you item writer. 1. you need the bean being step scope, 2. you need setter for "context" property.
An ExecutionContext is a set of key-value pairs containing information that is scoped to either StepExecution or JobExecution . Spring Batch persists the ExecutionContext , which helps in cases where you want to restart a batch run (e.g., when a fatal error has occurred, etc.).
you have at least 4 possibilities:
Code Example for accessing JobExecution from Tasklet:
setting a value
public class ChangingJobExecutionContextTasklet implements Tasklet { /** {@inheritDoc} */ @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { // set variable in JobExecutionContext chunkContext .getStepContext() .getStepExecution() .getJobExecution() .getExecutionContext() .put("value", "foo"); // exit the step return RepeatStatus.FINISHED; } }
extracting a value
public class ReadingJobExecutionContextTasklet implements Tasklet { private static final Logger LOG = LoggerFactory.getLogger(ChangingJobExecutionContextTasklet.class); /** {@inheritDoc} */ @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { // pull variable from JobExecutionContext String value = (String) chunkContext .getStepContext() .getStepExecution() .getJobExecution() .getExecutionContext() .get("value"); LOG.debug("Found value in JobExecutionContext:" + value); // exit the step return RepeatStatus.FINISHED; } }
i created code examples for the first 3 solutions in my spring-batch-examples github repository, see module complex and package interstepcommunication
Another way is to use StepExecutionListener
which is called after step execution. Your tasklet can implements it and share local attribute.
public class ReadingJobExecutionContextTasklet implements Tasklet, StepExecutionListener { private String value; @Override public ExitStatus afterStep(StepExecution stepExecution) { ExecutionContext jobExecutionContext = stepExecution.getJobExecution().getExecutionContext(); jobExecutionContext.put("key", value); //Return null to leave the old value unchanged. return null; } }
So, in the step, your bean is a tasklet and a listener like bellow. You should also configure the scope of you step to "step" :
<batch:step id="myStep" next="importFileStep"> <batch:tasklet> <ref bean="myTasklet"/> <batch:listeners> <batch:listener ref="myTasklet"/> </batch:listeners> </batch:tasklet> </batch:step> <bean id="myTasklet" class="ReadingJobExecutionContextTasklet" scope="step">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With