You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Greg Thomas <gr...@gmail.com> on 2016/06/19 17:41:37 UTC
CloseableThreadContext with multiple threads
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)
Re: CloseableThreadContext with multiple threads
Posted by Mikael Ståldal <mi...@magine.com>.
I don't think there is a general solution for this, but there are specific
solutions for different runtime environments. We should provide some kind
of hook to allow plugging in a solutions for a particular runtime
environment.
On Mon, Jun 20, 2016 at 2:09 PM, Mikael Ståldal <mi...@magine.com>
wrote:
> I have had the same issue for a long time, and it is a tricky one.
>
> It is the background for this ticket:
> https://issues.apache.org/jira/browse/LOG4J2-1010
>
> On Mon, Jun 20, 2016 at 9:36 AM, Greg Thomas <gr...@gmail.com>
> wrote:
>
>> On 19 June 2016 at 23:25, Remko Popma <re...@gmail.com> wrote:
>> > By setting system property isThreadContextMapInheritable to true, the
>> > ThreadContext map will be stored in a InheritableThreadLocal.
>>
>> Unfortunately not; firstly, it won't work with a ThreadPoolExecutor
>> (which admittedly my example didn't use, but I did say it was
>> contrived ;))
>>
>> Greg
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>>
>>
>
>
> --
> [image: MagineTV]
>
> *Mikael Ståldal*
> Senior software developer
>
> *Magine TV*
> mikael.staldal@magine.com
> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>
> Privileged and/or Confidential Information may be contained in this
> message. If you are not the addressee indicated in this message
> (or responsible for delivery of the message to such a person), you may not
> copy or deliver this message to anyone. In such case,
> you should destroy this message and kindly notify the sender by reply
> email.
>
--
[image: MagineTV]
*Mikael Ståldal*
Senior software developer
*Magine TV*
mikael.staldal@magine.com
Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
Privileged and/or Confidential Information may be contained in this
message. If you are not the addressee indicated in this message
(or responsible for delivery of the message to such a person), you may not
copy or deliver this message to anyone. In such case,
you should destroy this message and kindly notify the sender by reply
email.
Re: CloseableThreadContext with multiple threads
Posted by Mikael Ståldal <mi...@magine.com>.
I have had the same issue for a long time, and it is a tricky one.
It is the background for this ticket:
https://issues.apache.org/jira/browse/LOG4J2-1010
On Mon, Jun 20, 2016 at 9:36 AM, Greg Thomas <gr...@gmail.com>
wrote:
> On 19 June 2016 at 23:25, Remko Popma <re...@gmail.com> wrote:
> > By setting system property isThreadContextMapInheritable to true, the
> > ThreadContext map will be stored in a InheritableThreadLocal.
>
> Unfortunately not; firstly, it won't work with a ThreadPoolExecutor
> (which admittedly my example didn't use, but I did say it was
> contrived ;))
>
> Greg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>
>
--
[image: MagineTV]
*Mikael Ståldal*
Senior software developer
*Magine TV*
mikael.staldal@magine.com
Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
Privileged and/or Confidential Information may be contained in this
message. If you are not the addressee indicated in this message
(or responsible for delivery of the message to such a person), you may not
copy or deliver this message to anyone. In such case,
you should destroy this message and kindly notify the sender by reply
email.
Re: CloseableThreadContext with multiple threads
Posted by Greg Thomas <gr...@gmail.com>.
On 19 June 2016 at 23:25, Remko Popma <re...@gmail.com> wrote:
> By setting system property isThreadContextMapInheritable to true, the
> ThreadContext map will be stored in a InheritableThreadLocal.
Unfortunately not; firstly, it won't work with a ThreadPoolExecutor
(which admittedly my example didn't use, but I did say it was
contrived ;))
Greg
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org
Re: CloseableThreadContext with multiple threads
Posted by Remko Popma <re...@gmail.com>.
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 <gr...@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)
>