首页 文章

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

提问于
浏览
0

我定义了简单的工作 . 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 回答

相关问题