1

We use MDC to add a request id to HTTP requests in our Spring Boot application using SLF4J.

We do this by adding a GenericFilterBean that intercepts the request, converts to HTTTP, and writes the desired information to MDC.put(...);

What is the correct pattern for doing this on, for example, a QuartzJob that uses a CronTrigger to launch jobs? Or a Spring Batch job?

IcedDante
  • 6,145
  • 12
  • 57
  • 100

1 Answers1

0

I solved it for Spring Batch by adding a JobExecutionListener, similar to what you have done for the HTTP with the GenericFilterBean.

In beforeJob() you can do MDC.put(...); and in afterJob() you can do MDC.remove(...);

Also, I added a TaskDecorator to copy the DMC from the parent thread to the sub-threads in case of a multi-threaded Job.

You can find a more detailed answer with code, here: https://stackoverflow.com/a/68347374/6837191

Chris Sekas
  • 69
  • 1
  • 4