简体   繁体   English

Spring 批处理:如何使用在一个作业中创建两个两个步骤

[英]Spring batch : how to use create two two steps in one job

I've a one class that I use to create two different Beans.我有一个 class 用于创建两个不同的 Bean。 These two Beans are then used to create two Steps and these two steps must be executed in sequence.然后使用这两个 Bean 来创建两个 Step,这两个 Step 必须按顺序执行。 I use one class to create two beans because the logic to be executed is the same.我使用一个 class 来创建两个 bean,因为要执行的逻辑是相同的。 By passing different values in the constructor, the data to be fetched is different.通过在构造函数中传递不同的值,获取的数据是不同的。

But when Spring-batch is running, an IllegalStateException exception is thrown: What causes this exception to be thrown?但是当Spring-batch运行时,抛出IllegalStateException异常:是什么原因导致抛出这个异常?

Caused by: org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.stepBrussels': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:383)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:195)
    at com.sun.proxy.$Proxy50.getName(Unknown Source)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.isStepRestart(JobFlowExecutor.java:86)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:67)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:68)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    ... 28 common frames omitted
Caused by: java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:167)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:99)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371)




@Configuration
@Slf4j
public class InstrumentReaderConfig {

    @Value("${chartinvest.url.admin}")
    private String adminUrl;

    @Value("${eodHistoricalData.url.listOfTickers}")
    private String listOfTickerslUrl;

    @Bean
    public ItemReader<TickerDto> itemReaderBr() throws JsonProcessingException {
        return new InstrumentReader(adminUrl, listOfTickerslUrl, "XBRU", "BR", "BE");
    }

    @Bean
    public ItemReader<TickerDto> itemReaderAs() throws JsonProcessingException {
        return new InstrumentReader(adminUrl, listOfTickerslUrl, "XAMS", "AS", "NL");
    }

}



@Configuration
public class StepConfig {

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private ItemReader itemReaderBr;

    @Autowired
    private ItemReader itemReaderAs;

    @Autowired
    private ItemProcessor itemProcessorEodHistorical;

    @Autowired
    private ItemWriter<TickerDto> itemWriterEodHistorical;

    @Bean
    @StepScope
    public Step stepBrussels() {
        return stepBuilderFactory.get("Brussels").<TickerDto, TickerDto>chunk(1)
                .reader(itemReaderBr)
                .processor(itemProcessorEodHistorical)
                .writer(itemWriterEodHistorical)
                .build();
    }


    @Bean
    @StepScope
    public Step stepAmsterdam() {
        return stepBuilderFactory.get("Amsterdam").<TickerDto, TickerDto>chunk(1)
                .reader(itemReaderAs)
                .processor(itemProcessorEodHistorical)
                .writer(itemWriterEodHistorical)
                .build();
    }
}


@Configuration
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;


    @Bean
    public Job processJob(Step stepBrussels, Step stepAmsterdam, JobExecutionListener listener) {
        return jobBuilderFactory
                .get("processJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(stepBrussels)
                .next(stepAmsterdam)
                .end()
                .build();
    }

}

Remove @StepScope from your step beans, @StepScope is only for beans used in steps, not steps themselves从您的步骤 bean 中删除 @StepScope,@StepScope 仅适用于步骤中使用的 bean,而不是步骤本身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM