方法注释@BeforeStep未被调用

时间:2017-06-19 22:47:02

标签: spring-boot spring-batch

我的目标是将一些值从tasklet传递到另一个步骤,该步骤由itemreader,processor和writer组成。基于参考,我应该使用ExecutionContextPromotionListener。但是,出于某种原因@BeforeStep没有被调用。这就是我所拥有的。

微进程

@Component
public class RequestTasklet implements Tasklet {

@Autowired
private HistoryRepository historyRepository;

@Override
public RepeatStatus execute(StepContribution sc, ChunkContext cc) throws Exception {

    List<History> requests
            = historyRepository.findHistory();

    ExecutionContext stepContext = cc.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
    stepContext.put("someKey", requests);
    return RepeatStatus.FINISHED;
}

}

ItemReader

@Component
public class RequestReader implements ItemReader<History> {

private List<History> requests;

@Override
public History read() throws UnexpectedInputException,
        ParseException,
        NonTransientResourceException {
    System.out.println("requests====>" + requests);
    if (CollectionUtils.isNotEmpty(requests)) {
        History request = requests.remove(0);
        return request;
    }
    return null;
}

@BeforeStep
public void beforeStep(StepExecution stepExecution) {
    System.out.println("====================here======================");
    JobExecution jobExecution = stepExecution.getJobExecution();
    ExecutionContext jobContext = jobExecution.getExecutionContext();
    this.requests = (List<History>) jobContext.get("someKey");
}

}

配置

    @Bean(name = "update-job")
public Job updateUserAttributes(
        JobBuilderFactory jbf,
        StepBuilderFactory sbf,
        ExecutionContextPromotionListener promoListener,
        HistoryProcessor processor,
        RequestReader reader,
        HistoryWriter writer,
        RequestTasklet loadRequestTasklet) {

    Step preStep = sbf.get("load-request-tasklet")
            .tasklet(loadRequestTasklet)
            .listener(promoListener)
            .build();

    Step step = sbf.get("update-step")
            .<History, History>chunk(2)
            .reader(reader)
            .processor(processor)
            .writer(writer)
            .taskExecutor(taskExecutor())
            .build();

    return jbf.get("update-job")
            .incrementer(new RunIdIncrementer())
            .start(preStep).next(step).
            build();
}

    @Bean
public ExecutionContextPromotionListener promoListener() {
    ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
    listener.setKeys(new String[]{
        "someKey",
    });
    return listener;
}

我还尝试在ItemReader中扩展StepExecutionListenerSupport,但得到了相同的结果。

我用谷歌搜索了这个问题,因为春天代理不是我的beforestep issue

我正在测试的更多信息。我设置了listener.setStrict(Boolean.TRUE);查看是否设置了键。但我得到了

  java.lang.IllegalArgumentException: The key [someKey] was not found in the Step's ExecutionContext.
at org.springframework.batch.core.listener.ExecutionContextPromotionListener.afterStep(ExecutionContextPromotionListener.java:61) ~[spring-batch-core-4.0.0.M1.jar:4.0.0.M1]

感谢任何帮助。

0 个答案:

没有答案