如何在运行并行作业时安全地将params从Tasklet传递到步骤

时间:2015-04-21 15:40:19

标签: java multithreading spring spring-batch-admin

我正试图将params中的params安全地传递给同一个工作中的一个步骤。

我的工作包括3个tasklet(第1步,第2步,第3步),最后是step4(处理器,读者,作者)

这项工作正在多次并行执行。

在tasklet里面的第一步我通过web服务评估param(hashId),而不是把它传遍我的链,直到我的读者(在第4步)

在第3步中,我创建了一个名为:filePath的新param,它基于hashid,我将其作为文件资源位置发送到step4(读者)

我正在使用stepExecution传递此参数(hashId和filePath)。

我通过tasklet尝试了3种方法:

传递param(hash1d从step1进入step2,从step2进入步骤3)我这样做:

chunkContext.getStepContext()
        .getStepExecution()
        .getExecutionContext()
        .put("hashId", hashId);

在第4步中,我将基于hashId填充filePath并将其传递给我的最后一步(读者处理器和编写器)

public class DownloadFileTasklet implements Tasklet, StepExecutionListener {
..

    @Override
     public RepeatStatus execute(ChunkContext chunkContext, ExecutionContext    
     executionContext) throws IOException {

    String hashId = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("hashId");

          ...

filepath="...hashId.csv";
//I used here executionContextPromotionListener in order to promote those keys

        chunkContext.getStepContext()
        .getStepExecution()
        .getExecutionContext()
        .put("filePath", filePath);
    } 

logger.info("filePath + "for hashId=" + hashId);

}
@Override
public void beforeStep(StepExecution stepExecution) {
    this.stepExecution = stepExecution;
}

请注意我在完成该步骤之前打印hashId和filePath值(步骤3)。通过日志,它们是一致的,并按预期填充

我还在我的阅读器中添加了日志,以查看我得到的参数。

@Bean
    @StepScope
    public ItemStreamReader<MyDTO> reader(@Value("#{jobExecutionContext[filePath]}") String filePath) {
              logger.info("test filePath="+filePath+");

        return itemReader;
    }

当我执行这个作业~10次时,我可以看到param filePath值在并行执行时填充了其他作业filePath值

这是我使用executionContextPromotionListener提升作业键的方式:

工作定义:

 @Bean
    public Job processFileJob() throws Exception {
        return this.jobs.get("processFileJob").
                start.(step1).
                next(step2)
                next(downloadFileTaskletStep()). //step3
                next(processSnidFileStep()).build();  //step4

    }

第3步定义

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


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

相同的结果线程弄乱了参数

我可以通过spring批处理数据库表跟踪结果:batch_job_execution_context.short_context:

在这里你可以看到由hashid构建的filePatch与原始hashId不同 //错误的记录///

{ “地图”:[{ “条目”:[{ “串”: “总记录”, “INT”:5},{ “串”: “segmentId”, “长”:13},{“串“:[” 文件路径 “ ”在/ etc / MYDIR /服务/ notification_processor /文件/ 2015_04_22 /的 f1c7b0f2180b7e266d36f87fcf6fb7aa 的.csv“]},{ ”串“:[ ”hashId“,” 的 20df39d201fffc7444423cfdf2f43789 “]}]}]}

现在,如果我们检查其他记录,他们似乎很好。但总是有一两个搞砸了

//更正记录

{"map":[{"entry":[{"string":"totalRecords","int":5},{"string":"segmentId","long":13},{"string":["filePath","\/etc\/mydir\/services\/notification_processor\/files\/2015_04_22\/**c490c8282628b894727fc2a4d6fc0cb5**.csv"]},{"string":["hashId","**c490c8282628b894727fc2a4d6fc0cb5**"]}]}]}

{"map":[{"entry":[{"string":"totalRecords","int":5},{"string":"segmentId","long":13},{"string":["filePath","\/etc\/mydir\/services\/notification_processor\/files\/2015_04_22\/**2b21d3047208729192b87e90e4a868e4**.csv"]},{"string":["hashId","**2b21d3047208729192b87e90e4a868e4**"]}]}]}   

知道为什么我有这些线程问题?

1 个答案:

答案 0 :(得分:2)

要查看您尝试过的方法:

  • 方法1 - 编辑JobParameters JobParameters在作业中是不可变的,因此不应尝试在作业执行期间尝试修改它们。
  • 方法2 - 编辑JobParameters v2 方法2与方法1非常相似,您只是以不同的方式获取JobParameters的引用。
  • 方法3 - 使用ExecutionContextPromotionListener。这是正确的方法,但你做错了。 ExecutionContextPromotionListener会查看步骤 ExecutionContext,并将您指定的密钥复制到作业的ExecutionContext。您将密钥直接添加到Job ExecutionContext,这是个坏主意。

简而言之,方法3最接近正确,但您应该将要共享的属性添加到步骤ExecutionContext,然后配置ExecutionContextPromotionListener以将适当的密钥提升为工作ExecutionContext

代码将更新如下:

chunkContext.getStepContext()
            .getStepExecution()
            .getExecutionContext()
            .put("filePath", filePath);