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
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.
@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer {
@Bean
public MyAsyncBean asyncBean() {
return new MyAsyncBean();
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
//executor.setThreadNamePrefix("MyExecutor-");
executor.setThreadFactory(myThreadFactory());
executor.initialize();
return executor;
}
public ThreadFactory myThreadFactory() {
return new MyCustomThreadFactoryThatKnowsHowToProperlyNameThreads();
}
@Override
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.
source to share
Your log4j template is fine (nested diagnostic context), but you have to feed it yourself by calling:
org.apache.log4j.NDC.push("parentThread1");
... 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");
source to share