简体   繁体   English

如何从TASKLET中的execute方法获取Spring Batch作业实例ID

[英]how to get Spring Batch job instance id from execute method in TASKLET

I am using a layout using Spring Batch 3.0 version.我正在使用使用 Spring Batch 3.0 版本的布局。

Create a Job and execute the placement by executing the JobLauncher run method of the TASKLET.创建一个 Job 并通过执行 TASKLET 的 JobLauncher run 方法来执行放置。

I want to know more accurately whether the Job is executed or not through insert logic in the query in TASKLET with the corresponding JobId and other tables other than the metatables.我想更准确地知道Job是否通过在TASKLET中的查询中插入逻辑与相应的JobId和元表以外的其他表来执行。

public class SampleScheduler {

protected final Logger log = LoggerFactory.getLogger(this.getClass());

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job sampleJob;

public void run() {
    try {
        String dateParam = new Date().toString();
        JobParameters param = new JobParametersBuilder().addString("date",dateParam).toJobParameters();
        JobExecution execution = jobLauncher.run(sampleJob, param);

        log.debug("###################################################################");
        log.debug("Exit Status : " + execution.getStatus());
        log.debug("###################################################################");

    } catch (Exception e) {
        // e.printStackTrace();
        log.error(e.toString());
    }
}   
}

Code for calling tasklet -调用 tasklet 的代码 -

public class SampleTasklet implements Tasklet{

@Autowired
private SampleService sampleService;

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

    sampleService.query();

    return RepeatStatus.FINISHED;
    }
}

This is my tasklet code.这是我的 tasklet 代码。

    StepContext stepContext = chunkContext.getStepContext();
    StepExecution stepExecution = stepContext.getStepExecution();
    JobExecution jobExecution = stepExecution.getJobExecution();
    long jobInstanceId = jobExecution.getJobId();

Is it right to try this in the TASKLET code above?在上面的TASKLET代码中尝试这个是否正确?

how to get Spring Batch job instance id from execute method in TASKLET如何从TASKLET中的execute方法获取Spring Batch作业实例ID

The org.springframework.batch.core.step.tasklet.Tasklet#execute method gives you access to the ChunkContext which in turn allows you to get the parent StepExecution and JobExecution .org.springframework.batch.core.step.tasklet.Tasklet#execute方法,您可以访问到ChunkContext这反过来又可以让你得到父StepExecutionJobExecution You can then get the job instance id from the job execution.然后,您可以从作业执行中获取作业实例 ID。

Is it right to try this in the TASKLET code above?在上面的TASKLET代码中尝试这个是否正确?

Yes, that's the way to go.是的,这就是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM