I'm trying to make a Spring Batch and I have no experience with it.
Is it possible to pass information from each batch step or must they be completely independent?
For example if I have
<batch:step id="getSQLs" next="runSQLs">
<batch:tasklet transaction-manager="TransactionManager"
ref="runGetSQLs" />
</batch:step>
<batch:step id="runSQLs">
<batch:tasklet transaction-manager="TransactionManager"
ref="runRunSQLs" />
</batch:step>
And getSQLs triggers a bean which executes a class which generates a List of type String. Is it possible to reference that list for the bean triggered by runSQLs? ("triggered" may not be the right term but I think you know what I mean)
UPDATE: So getSQLs step triggers this bean:
<bean id="runGetSQLs" class="myTask"
scope="step">
<property name="filePath" value="C:\Users\username\Desktop\sample.txt" />
</bean>
which triggers myTask class which executes this method:
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("theListKey", sourceQueries);
return RepeatStatus.FINISHED;
}
Do I need to somehow pass stepExecution to the execute method?
java config way.
Step 1 : Configure ExecutionContextPromotionListener
@Bean
public ExecutionContextPromotionListener executionContextPromotionListener()
{
ExecutionContextPromotionListener executionContextPromotionListener = new ExecutionContextPromotionListener();
executionContextPromotionListener.setKeys(new String[] {"MY_KEY"});
return executionContextPromotionListener;
}
Step 2 : Configure Step with ExecutionContextPromotionListener
@Bean
public Step myStep() {
return stepBuilderFactory.get("myStep")
.<POJO, POJO> chunk(1000)
.reader(reader()
.processor(Processor())
.writer(Writer()
.listener(promotionListener())
.build();
}
Step 3 : Accessing data in processor
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
jobExecutionContext = stepExecution.getJobExecution().getExecutionContext();
jobExecutionContext.getString("MY_KEY")
}
Step 4 : setting data in processor
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
stepExecution.getJobExecution().getExecutionContext().put("MY_KEY", My_value);
}
I recommend to think twice in case you want to use ExecutionContext to pass information between steps. Usually it means the Job is not designed perfectly. The main idea of Spring Batch is to process HUGE amount of data. ExecutionContext used for storing information about progress of a Job/Step to reduce unnecessary work in case of failure. It is by design you can't put big data into ExectionContext. After completion of a step, you should have your information in reliably readable form - File, DB, etc. This data can be used on next steps as input. For simple jobs I would recommend to use only Job Parameters as information source.
In your case "runGetSQLs" doesn't look like a good candidate for a Step, but if you want you can implement it as a Spring bean and autowire in "runRunSQLs" step (which again is arguably good candidate for a Step). Based on your naming, runGetSQLs looks like ItemReader and runRunSQLs looks like ItemWriter. So they are parts of a step, not different steps. In this case you don't need to transfer information to other steps.
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