从步骤之前的 xml 配置 spring 批处理中获取 jobExecutionContext

Get jobExecutionContext in xml config spring batch from before step

我正在这样定义我的 MultiResourceItemReader

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

你怎么能看出我想从 jobExecutionContext 中读取 "filesResource" 值。

注意:我更改了一些名称以保留 "code privacy"。正在执行,有人想要更多信息请告诉我。

我在第一步中保存了这个值,我在第二步中使用了 reader,我应该可以访问它吗?

我将它保存在我的 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>

我不太确定我忘记了获取值的内容。我收到的错误是:

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.

我看不出你的配置有什么问题。 resourceString 的值应该是 org.springframework.core.io.Resource 的数组,因为这是 MultiResourceItemReader.

resources 属性的参数类型

您可以传递一个数组或 String 列表以及每个资源的绝对路径,它应该可以工作。这是一个简单的例子:

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;
    }

}

希望这对您有所帮助。