logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remko Popma <remko.po...@gmail.com>
Subject Re: CloseableThreadContext with multiple threads
Date Sun, 19 Jun 2016 22:25:32 GMT
Greg,
By setting system property isThreadContextMapInheritable to true, the
ThreadContext map will be stored in a InheritableThreadLocal.

Will this help?

On Mon, Jun 20, 2016 at 2:41 AM, Greg Thomas <greg.d.thomas@gmail.com>
wrote:

> 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)
>

Mime
View raw message