使用jobParameters在tasklet和step之间传递参数返回null

时间:2015-01-19 17:10:54

标签: spring spring-batch spring-batch-admin

我定义了简单的工作。 tasklet然后再步骤。

我正在尝试在这两者之间传递filePath。

当我到达词干时,会调用阅读器,并且在那里filePath保持为空。

我错过了什么?

工作配置:

@Bean
    public Job processFileJob() throws Exception {
          return this.jobs.get("processFileJob").start(downloadFileStep()).next(processor()).build();//.next(pushToKafkaStep()).build();

    }



public Step downloadFileStep() {
        return this.steps.get("downloadFileTaskletStep").tasklet(downloadFileTasklet()).build();
    }


    @Bean
    protected Tasklet downloadFileTasklet() {
        return new DownloadFileTasklet();
    }




@Bean
public Step processor() {
         return stepBuilderFactory.get("processor")
      .<PushItemDTO, PushItemDTO>chunk(1) 
                        .reader(reader(OVERRIDDEN_BY_EXPRESSION))
                        ...
                        .build();
            }



                           //here filePath always null!!
@Bean
    @Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
    public ItemStreamReader<PushItemDTO> reader(@Value("#{jobParameters[filePath]}") String filePath) {
        FlatFileItemReader<PushItemDTO> itemReader = new FlatFileItemReader<PushItemDTO>();
        itemReader.setLineMapper(lineMapper());
        itemReader.setLinesToSkip(1);
        itemReader.setResource(new FileSystemResource(filePath));
        return itemReader;
    }

DownloadFileTasklet:

public class DownloadFileTasklet implements Tasklet, StepExecutionListener {

   String filePath;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        filePath="someurl";        
        return RepeatStatus.FINISHED;
    }



    @Override
    public void beforeStep(StepExecution stepExecution) {

    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        JobParameters jobParameters = stepExecution.getJobExecution().getJobParameters();
       // jobParameters.ad
        stepExecution.getExecutionContext().putString("filePath", filePath);
        //Return null to leave the old value unchanged.
        return null;
    }

我设法在调用此作业时直接从jobLauncher传递params,但是当我尝试在tasklet中定义新的param并希望在下一步中使用它时,我将其视为null

谢谢你。

根据推荐,我应该使用ExecutionContextPromotionListener。

所以我添加到我的java配置中:

@Bean
    public ExecutionContextPromotionListener executionContextPromotionListener()
    {
        ExecutionContextPromotionListener executionContextPromotionListener=new ExecutionContextPromotionListener();
        executionContextPromotionListener.setKeys(new String[]{"filePath"});
        return new ExecutionContextPromotionListener();
    }

但是我得到例外:

Caused by: java.lang.IllegalArgumentException: The 'keys' property must be provided

我通过改变返回executionContextPromotionListener;

来修复它

然而,filePath仍为空。

我还试图用这种方式修改我的步骤声明:

*添加了executionContextPromotionListener

public Step downloadFileStep() {
    return this.steps.get("downloadFileTaskletStep").tasklet(downloadFileTasklet()).listener(executionContextPromotionListener()).build();
}

在filePath param

中仍为null

1 个答案:

答案 0 :(得分:0)

为步骤ExecutionContext添加值使其仅可用于该步骤。要使其能够访问另一个步骤,您需要将该键提升为作业ExecutionContext。为此,请查看ExecutionContextPromotionListener。它会将您配置的任何键从当前步骤的ExecutionContext升级到作业的ExecutionContext,以便可以从其他步骤访问它们。

您可以在此处的文档中详细了解ExecutionContextPromotionListenerhttp://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/listener/ExecutionContextPromotionListener.html

相关问题