OK, so I've been using the CloseableThreadContext introduced in 2.6 quite a
lot, and it seems to be working well.

The only problem I have it with it is with threading.

With a CTC it makes no sense to set isThreadContextMapInheritable to true,
as only one of the threads can close the try-with-resources object - any
others never have the context cleared.

The best solution I've been able to come up with is an object that
represents the state of the thread context at a given point in time, and
pass this to other threads to create a new CTC, something like the
following contrived example ...

try (final CloseableThreadContext.Instance ctc =
CloseableThreadContext.put("user", user.getUsername())) {
    final CloseableThreadContext.State state = ctc.getState();
    new Thread(new Runnable() {
        public void run() {
            try (final CloseableThreadContext.Instance ctc =
CloseableThreadContext.from(state)) {
                logger.debug("Message 1");
                ...
                logger.debug("Message 2");
            }
        }
    }).start();
}

Any thoughts on this, of perhaps a better way of solving the problem?

Thanks,

Greg
(I'm happy to submit a patch for whatever the best solution appears to be)

Reply via email to