Search code examples
loggingthreadpooldecoratorhazelcastmdc

Hazelcast thread decoration


We are trying to decorate asynchronous Hazelcast threads with our current log (MDC) context. Typically a Hazelcast thread might look like: hz._hzInstance_1_xxx.cached.thread-1]

Is this possible?

We would like to do something such as the following which is how we can achieve decoration of threads in a ThreadPoolTaskExecutor in Spring:

    private static class MdcTaskDecorator implements TaskDecorator {

        @Override
        public Runnable decorate(Runnable runnable) {
            Map<String, String> contextMap = MDC.getCopyOfContextMap();
            return () -> {
                try {
                    if (contextMap != null) {
                        MDC.setContextMap(contextMap);
                    }
                    runnable.run();
                } finally {
                    MDC.clear();
                }
            };
        }
    }

Solution

  • We achieved what we were after by attaching our thread UUID to our Hazelcast map entries transiently. We then reset the MDC context at each of Hazelcast's async operations using the UUID in the entry.