Registering the name of the parent thread with the executor service

I am using Spring task:executor

in the context of spring integration. When the call is called, the log looks like this:
2015-04-21 10:56:14,371 INFO [com.Class] (activatorExecutor-2:null) Processing user: abc

Below is the log4j transform template: The
%d %-5p [%c] (%t:%x) %m%n %throwable{10}

question is whether it is possible to put the calling thread name in MDC or as a prefix for the child thread name like:
2015-04-21 10:56:14,371 INFO [com.Class] (activatorExecutor-2:parentThread1) Processing user: abc

or this:
2015-04-21 10:56:14,371 INFO [com.Class] (parentThread1_activatorExecutor-2:null) Processing user: abc

so that I can correlate with which one the thread has activated this executor task. Thank.


source to share

2 answers

I don't have a full blown well thought out solution for this, but I recommend taking a look at the Java setup approach .

I see that you can choose the implementation Executor

to use for starting threads through @EnableAsync

and AsyncConfigurer

. The implementation provided in the example ( ThreadPoolTaskExecutor

) has a method ExecutorConfigurationSupport#setThreadFactory(ThreadFactory)

that you can use to customize the creation of streams.

You can create an implementation ThreadFactory

that uses a delegate ThreadFactory

to actually create threads, but now you have a hook that will allow you to name the threads according to your criteria. Do this as above and hopefully it works for you.

 public class AppConfig implements AsyncConfigurer {

      public MyAsyncBean asyncBean() {
          return new MyAsyncBean();

      public Executor getAsyncExecutor() {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          return executor;

      public ThreadFactory myThreadFactory() {
          return new MyCustomThreadFactoryThatKnowsHowToProperlyNameThreads();

      public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
          return MyAsyncUncaughtExceptionHandler();


This is the not very thoughtful part. I figured I'd let you come up with an implementation ThreadFactory

that best suits your needs.



Your log4j template is fine (nested diagnostic context), but you have to feed it yourself by calling:



... in the "right place".

Alternatively, you use a "matched" diagnostic context with a template:

... %X{myKey} ...


.. and feeding:

org.apache.log4j.MDC.put("myKey", "parentThread1");




All Articles