Spring 我希望MultiResourceItemReader处理的当前资源在beforeStep方法中可用

Spring 我希望MultiResourceItemReader处理的当前资源在beforeStep方法中可用,spring,spring-batch,Spring,Spring Batch,是否有任何方法可以使MultiResourceItemReader处理的currentResource在beforeStep方法中可用。请提供一个工作代码示例。我尝试将multiresourcereader引用注入到stepexecutionlistener,但是spring cglib只接受要注入的接口类型,我不知道是使用ItemReader还是ItemStream接口 目前无法从MultiResourceItemReader检索currentResource。如果需要此API增强,请在中创

是否有任何方法可以使MultiResourceItemReader处理的currentResourcebeforeStep方法中可用。请提供一个工作代码示例。我尝试将multiresourcereader引用注入到stepexecutionlistener,但是spring cglib只接受要注入的接口类型,我不知道是使用ItemReader还是ItemStream接口

  • 目前无法从
    MultiResourceItemReader
    检索
    currentResource
    。如果需要此API增强,请在中创建一个
  • 即使有
    currentResource
    的getter,它的值在
    beforeStep()中也是无效的。它在
    open()
    close()
    之间有效

如果您使用a和概念,这是可能的

简单的代码示例,并发限制为1以模拟“串行”处理:

<bean name="businessStep:master" class="org.springframework.batch.core.partition.support.PartitionStep">
    <property name="jobRepository" ref="jobRepository"/>
    <property name="stepExecutionSplitter">
        <bean class="org.springframework.batch.core.partition.support.SimpleStepExecutionSplitter">
            <constructor-arg ref="jobRepository"/>
            <constructor-arg ref="concreteBusinessStep"/>
            <constructor-arg>
                <bean class="org.spring...MultiResourcePartitioner" scope="step">
                    <property name="resources" value="#{jobParameters['input.file.pattern']}"/>
                </bean>
            </constructor-arg>
        </bean>
    </property> 
    <property name="partitionHandler">
        <bean class="org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler">
            <property name="taskExecutor">
                <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor">
                    <property name="concurrencyLimit" value="1" />
                </bean>
            </property>
            <property name="step" ref="concreteBusinessStep"/>
        </bean>
    </property>
</bean>

<bean id="whateverClass" class="..." scope="step">
    <property name="resource" value="#{stepExecutionContext['fileName']}" />
</bean>

示例步骤配置:

<job id="renameFilesPartitionJob">        
    <step id="businessStep" 
          parent="businessStep:master" />
</job>

<step id="concreteBusinessStep">
    <tasklet>
        <chunk reader="itemReader" 
               writer="itemWriter" 
               commit-interval="5" />
    </tasklet>
</step>

潜在缺陷:

  • 每个文件的不同步骤,而不是一个步骤
  • 更复杂的配置

使用MultiResourceItemReader中的getCurrentResource()
方法,返回当前
资源的
MultiResourceItemReader

方法,更新步骤执行。示例代码如下所示

private StepExecution stepExecution;

@Override
public Resource getCurrentResource() {
    this.stepExecution.getJobExecution().getExecutionContext().put("FILE_NAME", super.getCurrentResource().getFilename());
    return super.getCurrentResource();
}

@BeforeStep
public void beforeStep(StepExecution stepExecution) {
    this.stepExecution = stepExecution;
}

如果使正在读取的项实现ResourceAware,则当前资源将设置为读取状态

public class MyItem implements ResourceAware {

    private Resource resource;
    //others

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    public Resource getResource() {
        return resource;
    }

}
在你的阅读器、处理器或书写器中

myItem.getResource()

将返回从中加载的资源

我建议您仔细设置与您的问题相关的标签,以获得更广泛的受众。我不知道你说的主题/应用程序/任何东西是什么,但我确信它不是Windows批处理文件(这是“批处理”标记的目标)。您将
MultiResourceItemReader
引用声明为
stepexecutionlistener
的想法我并不清楚:对于这个
MultiResourceItemReader
应该实现
stepexecutionlistener
接口,然后
beforeStep()执行步骤之前将调用
MultiResourceItemReader
中的
:谢谢。我看到了消息来源,没有这方面的规定。