2

We are using using spring-core 4.1.X module and spring-batch-core 3.0.X versions for a spring boot application. We are using SimpleAsyncTaskExecutor for async working of threads. I need to copy MDC information from parent to child thread. I have seen using ThreadDecorators as best way to achieve this. But, i these are not available in version of spring we are using. Any suggestions on equivalent approaches with the spring versions we are using. If we cannot achieve using TaskDecorators, any suggestions on what is the other best way ??

poorna chandra
  • 27
  • 1
  • 1
  • 8
  • These links might help: https://stackoverflow.com/questions/52009107/spring-batch-mdc-logging and https://stackoverflow.com/questions/52171559/accessing-job-information-for-mdc – Mahmoud Ben Hassine Oct 30 '18 at 09:24

1 Answers1

0

I think you are referring to the usage of TaskDecorator that was added in Spring 4.3 as a means to create a custom MDC decorator and register it in a TaskExecutor, a process that is nicely described in this article.

Actually this is a great idea especially since SimpleAsyncTaskExecutor has also been upgraded in Spring 4.3 to support TaskDecorators.

If you cannot upgrade (which is the recommended approach) I don't think it's especially hard to extend SimpleAsyncTaskExecutor to offer a similar hook if you see what is actually happening under the hood.

Here's the original commit on version 4.3 for SimpleAsyncTaskExecutor to support TaskDecorator's

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • thanks for the reply. So u mean, i need to create my own TaskExecutor class which extends SimpleAsyncTaskExecutor and write my own implementation of copying MDC from parent to child thread and call method of SimpleAsyncTaskExecutor? – poorna chandra Oct 30 '18 at 16:03
  • yes. or skip the TaskDecorator similar structure and embed the MDC logic directly in the extended SimpleAsyncTaskExecutor. Not the most elegant of solutions of course but all that in case you can upgrade. – dimitrisli Oct 30 '18 at 19:35
  • thanks. I have implemented my own TaskDecorator and called method of SimpleAsyncTaskExecutor from it. It worked perfectly – poorna chandra Nov 06 '18 at 02:13
  • Great. If you found this answer useful you can upvote and/or accept it. – dimitrisli Nov 06 '18 at 11:32