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

我定义了简单的工作 . 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仍为null .

我也尝试过这样修改我的Step声明:

*添加了executionContextPromotionListener

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

在filePath param中仍为null

回答(1)

2 years ago

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

您可以在此处的文档中阅读有关 ExecutionContextPromotionListener 的更多信息:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/listener/ExecutionContextPromotionListener.html