Local flow guidelines for mid-range heavy server applications

Is it recommended to use ThreadLocal to store the thread context?

I am creating a server application where there are standard services that I need to run.

Note. We are not building on the SOA architecture.

Before starting each service, I need to indicate the state in which there is Service Contex, which is a variable card for work. This map variable is shared when services are running in parallel.

Now, for example, the service needs to check the weather, it must be stopped or limited based on some flow-related parameters.

Question: This is a good approach to keep the context of the stream inside the stream local, and then create api through the service context to access the parameters for these variables.

This will help me hide complex behavior, and it will not reveal my inner things.

Thanks Aditya

+7
source share
2 answers

It looks like your application server infrastructure should provide you with the means to implement this feature in a simpler way - if you are not implementing your own infrastructure.

Now, for example, the service needs to check the weather, it must be stopped or timed based on some parameters related to the flow.

An EJB container provides this functionality. EJB also provides a session context and means to ensure that the context can be restored if execution is passed between threads (by passivation + activation).

+1
source

You can use ThreadLocal quite freely, but you should clearly define your thread model (see, if you have control over thread creation) ... also keep in mind that to ensure that the state stored in ThreadLocal can not what do you expect if you rely on any cleanup code.

Also: use (you can no longer emphasize) WeakReference for everything that your code is not directly responsible.

0
source

All Articles