Steve Hu
Steve Hu

Reputation: 388

How to share a util object which is not thread safe in light-4j handler

I have generated code from an openapi yaml file. I'm implementing the handleRequest methods. I need to share the same instance of a "Util" object to reuse it in all the handleRequest calls. Could you please tell me where to store my Util object instance? My Util class is not thread safe, so I should have one instance for each client thread.

Upvotes: 0

Views: 36

Answers (1)

Steve Hu
Steve Hu

Reputation: 388

If your class is thread safe, the best place is https://www.networknt.com/concern/service/

If the object is not thread safe then save one object per thread with ThreadLocal. That means one request might be calling two or more instance of util objects when a request is dispatched from an IO thread to a worker thread.

If it's actually a util object can you make it stateless so that it is thread safe? Maybe add an additional context type object for the state if you really need it. Attaching it to the exchange as an attachment could work.

Upvotes: 0

Related Questions