0
votes

I am using a layout using Spring Batch 3.0 version.

Create a Job and execute the placement by executing the JobLauncher run method of the TASKLET.

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.

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 -

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.

    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?

1

1 Answers

1
votes

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

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. You can then get the job instance id from the job execution.

Is it right to try this in the TASKLET code above?

Yes, that's the way to go.