From 4d215d484ba2cd2319c8d7c94f4557d065ab5ff7 Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Tue, 7 Apr 2009 16:52:01 +0000 Subject: [PATCH] Added section on passing data to future steps to common-patterns.xml --- .../docbook/reference/common-patterns.xml | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/site/docbook/reference/common-patterns.xml b/src/site/docbook/reference/common-patterns.xml index 57972aeaf..5cd052541 100644 --- a/src/site/docbook/reference/common-patterns.xml +++ b/src/site/docbook/reference/common-patterns.xml @@ -510,4 +510,76 @@ which will not affect the status of the Step. + +
+ Passing data to future steps + + It is often useful to pass information from one step to another. + This can be done using the ExecutionContext. The + catch is that there are two ExecutionContexts: one + at the Step level and one at the + Job level. The Step + ExecutionContext lives only as long as the step + while the Job + ExecutionContext lives through the whole + Job. On the other hand, the + Step ExecutionContext is + updated every time the Step commits a chunk while + the Job ExecutionContext is + updated only at the end of each Step. + + The consequence of this separation is that all data must be placed + in the Step ExecutionContext + while the Step is executing. This will ensure that + the data will be stored properly while the Step is + on-going. If data is stored to the Job + ExecutionContext, then it will not be persisted + during Step execution and if the + Step fails, that data will be lost. + + public void MyItemWriter implements ItemWriter<Object> { + private StepExecution stepExecution; + + public void write(List<? extends Object> items) throws Exception { + // ... + + this.stepExecution.getExecutionContext().put("someKey", someObject); + } + + @BeforeStep + public void saveStepName(StepExecution stepExecution) { + this.stepExecution = stepExecution; + } +} + + To make the data available to future Steps, + it will have to be "promoted" to the Job + ExecutionContext after the step has finished. + Spring Batch provides the + ExecutionContextPromotionListener for this purpose. + The listener must be configured with the keys related to the data in the + ExecutionContext that must be promoted. It can + also, optionally, be configured with a list of exit code patterns for + which the promotion should occur ("COMPLETED" is the default). As with all + listeners, it must be registered on the + Step. + + <step id="step1"> + <tasket> + <chunk reader="reader" writer="writer" commit-interval="10"/> + <listeners> + <listener ref="promotionListener"/> + </listeners> + </tasklet> +</step> + +<step id="step2"> + ... +</step> + +<beans:bean id="promotionListener" + class="org.spr....ExecutionContextPromotionListener"> + <beans:property name="keys" value="someKey"/> +</beans:bean> +