0

I have a InheritableThreadLocal variable which I want to pass to another child Thread allocated by the Thread pool of the EJB container as a result of a Async call to a public EJB method. The parent caller initiating the Async call is a REST resource which is a Stateless EJB. InheritableThreadLocal passes value to its child thread only when the child thread is created new. Once a thread is created in the pool it never destroys the Thread till the server is bounced.

Considering the above facts, the child thread for my case comes from a Threadpool maintained by the EJB container. The InheritableThreadLocal passing value to its child thread only when Resource is invoked for the first time after a server starts or if a brand new Thread which just got created in Threadpool is allocated as a child thread. In all other cases I am getting the initial value what got assigned to the child thread from the pool when the Thread got created and allocated for the 1st time.

I am running my code on IBM WebSphere and Passing the ThreadLocal value as a method parameter in the Async call is not an option.

How I can reassign the ThreadLocal value so that for each run I can get the current value assigned to the child thread?

I found the below link which is somewhat similar but for my case the ThreadPool is what provided by the EJB container.

Propagating ThreadLocal to a new Thread fetched from a ExecutorService

Community
  • 1
  • 1
Debjit
  • 1
  • If you don't get an answer, let me suggest adding some code snippets so it's easier for someone reading to make sure they're following along correctly. – Scott Kurz Jul 07 '16 at 13:10

1 Answers1

0

Using ThreadLocal is not a portable way. You need to know exact how the container handle threads.

From my perspective a ThreadLocal is save as in general the container use the same thread to process a sequence of sync Servler/REST/EJB invocations. If Async come into play this will not work.

I would use a portable way like parameters or persistence.

wfink
  • 347
  • 1
  • 6