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 jobName
and executionId
for any job that may run.
I've got a launcher like this:
@Bean(name = "AsyncMccJobLauncher")
public JobLauncher simpleJobLauncher(JobRepository jobRepository) {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
taskExecutor.setTaskDecorator(new TaskDecorator() {
@Override
public Runnable decorate(Runnable runnable) {
// MDC.put("execId", jobExecution.getJobId());
// MDC.put("jobName", "test jobName");
return new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// This adds batch logging info while the job is running
// MDC.put("execId", "here");
// MDC.put("jobName", "here");
runnable.run();
}
};
}
});
jobLauncher.setTaskExecutor(taskExecutor);
return jobLauncher;
}
how can I access the job info here? When I try using JobExecution
it always comes up null
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) :-)
You probably don't need to have a task decorator. What you can do is subclass
SimpleJobLauncher
and overriderun
, something like: