Solved this problem using Lóránt Pintér's suggestion. Here is my groovy version:
abstract class MdcRetainingCallable<V> implements Callable { private final Map context MdcRetainingCallable() { this.context = MDC.getCopyOfContextMap() } @Override V call() { V answer = null Map originalContext = MDC.getCopyOfContextMap() MDC.setContextMap(context); try { answer = withMdc(); } finally { MDC.setContextMap(originalContext); } return answer } abstract protected V withMdc(); static MdcRetainingCallable cast(Closure c) { return ([withMdc: c] as MdcRetainingCallable) } }
When I need to submit task to executorService I do following
threadPool.submit(MdcRetainingCallable.cast { my_task_code })
Solved this problem using Lóránt Pintér's suggestion. Here is my groovy version:
When I need to submit task to executorService I do following
threadPool.submit(MdcRetainingCallable.cast { my_task_code })