What exactly is an executionContext in Spring batch? What does getting a handle on it allow us to do? It seems like an overloaded use of the term executionContext. What is the difference between the two "Contexts" below? One of them has jobParameters and the other just gets the execution context. But the first one gets jobParameters from the StepExecution rather than the job. It's confusing me. How many different kinds of execution contexts are there?
chunkContext.getStepContext()
.getStepExecution().getJobParameters()
chunkContext.getStepContext()
.getStepExecution().getJobExecution().getExecutionContext()
Any enlightenment you can provide would be a great help.
Thanks in advance.
Passing data between steps. In Spring Batch, ExecutionContext of execution context that can be used in the scope of each step and job is provided. By using the execution context, data can be shared between the components in the step.
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.).
Spring Batch does not allow to use more than one reader in one step. There is more than one way to solve your problem: Query the web api in the reader. In the processor, execute the sql query for each item received from the reader and pass the combination on to the writer (or next processor).
A chunk oriented tasklet can directly access the StepExecution from the ChunkContext : @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { StepExecution stepExecution = chunkContext. getStepContext(). getStepExecution(); ... }
Within Spring Batch there are two ExecutionContexts. The first is at the Job level. The second is at the Step level. In each case, the ExecutionContext serves as a storage area for information pertinent to that level. For example, in the Step's ExecutionContext, the state of a step (how many records have been read, etc) is typically stored so that on restart the reader and writer can be reset to the correct position. The Job's ExecutionContext is typically used to share information across steps.
With that being said, in the case of what you posted above is the getting of JobParameters from the StepExecution. This is really just a shortcut to getting them from the JobExecution. What you're doing in the first line is the same as:
chunkContext.getStepContext()
.getStepExecution()
.getJobExecution()
.getJobParameters();
The JobParameters have no bearing on the ExecutionContext. They are the parameters passed into a job much like the String []
args passed to a public static void main(String[] args)
.
Where the confusion may lie is the StepContext. The StepContext serves as a wrapper for the StepExecution that allows the StepScope's accessors to work. For example, when you configure a job with a bean like this:
<beans:bean id="outputFile"
class="org.springframework.core.io.FileSystemResource" scope="step">
<beans:constructor-arg value="#{jobParameters[outputFile]}" />
</beans:bean>
The #{jobParameters[outputFile]}
is being roughly evaluated to StepContext.getJobParameters().getString("outputFile");
.
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