I\'m trying to log job info with MDC. I\'ve got a file called CommonBatchConfiguration
that handles threading and logging job info. I want to log things like
The job execution should be already created when your decorated runnable is invoked. The question is how to get access to it at this point? I'm not sure if this would be easy unless you are able to introspect a final variable in a method of an anonymous inner class instance (the runnable created by Spring Batch) wrapped in an anonymous inner class instance (your decorator) :-)
I want to log things like jobName and executionId for any job that may run.
You probably don't need to have a task decorator. What you can do is subclass SimpleJobLauncher
and override run
, something like:
@Bean(name = "AsyncMccJobLauncher")
public JobLauncher simpleJobLauncher(JobRepository jobRepository) {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher() {
@Override
public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobExecution jobExecution = super.run(job, jobParameters);
// jobExecution is created and accessible here
//MDC.put("execId", String.valueOf(jobExecution.getJobId()));
//MDC.put("jobName", jobExecution.getJobInstance().getJobName());
return jobExecution;
}
};
jobLauncher.setJobRepository(jobRepository);
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
jobLauncher.setTaskExecutor(taskExecutor);
return jobLauncher;
}