Search code examples
springspring-batchbatch-processing

Get jobExecutionContext in xml config spring batch from before step


I am defining my MultiResourceItemReader on this way:

<bean id="multiDataItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
        <property name="resources" value="#{jobExecutionContext['filesResource']}"/>
        <property name="delegate" ref="dataItemReader"/>
</bean>

How you can see I want read from the jobExecutionContext the "filesResource" value.

Note: I changed some names to keep the "code privacy". This is executing, Is somebody wants more info please tell me.

I am saving this value in my first step and I am using the reader in the second step, Should I have access to it?

I am saving it in the final lines from my step1 tasklet:

ExecutionContext jobContext = context.getStepContext().getStepExecution().getJobExecution().getExecutionContext(); 
    jobContext.put("filesResource", resourceString); 


<batch:job id="myJob">

    <batch:step id="step1" next="step2">
        <batch:tasklet ref="moveFilesFromTasklet" />
    </batch:step>

    <batch:step id="step2">
        <tasklet>
            <chunk commit-interval="500" 
                reader="multiDataItemReader" 
                processor="dataItemProcessor" 
                writer="dataItemWriter" />
        </tasklet>
    </batch:step>

</batch:job>

I am not really sure what I am forgetting to get the value. The error that I am getting is:

20190714 19:49:08.120 WARN   org.springframework.batch.item.file.MultiResourceItemReader [[ # ]] - No resources to read. Set strict=true if this should be an error condition.

Solution

  • I see nothing wrong with your config. The value of resourceString should be an array of org.springframework.core.io.Resource as this is the parameter type of the resources attribute of MultiResourceItemReader.

    You can pass an array or a list of String with the absolute path to each resource and it should work. Here is a quick example:

    class MyTasklet implements Tasklet {
    
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
            List<String> resources = Arrays.asList(
                    "/full/path/to/resource1",
                    "/full/path/to/resource2");
            chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext()
                    .put("filesResource", resources);
            return RepeatStatus.FINISHED;
        }
    
    }