Jersey - Thread access to @Context objects?
Thread local proxies are injected so can only be called if:1) a request is in scope for the invoking thread; and
2) and from methods on the same invoking thread.
I am unsure how to transition or include additional threads that are
considered within the scope of the request.
You can instead use a Jersey feature and inject:
@Context ThreadLocal<HttpServletRequest> treq;
@Context ThreadLocal<HttpServletResponse> tres;
then call the get method to get the direct instance from the resource
class and that instance can be passed to the worker thread.
> I run all Servlet API access through a ServletUtil class which
> synchronizes all calls, which works fine directly under Tomcat. How
> can I make this work under Jersey?
>
> Will finishing the port to the JAX-RS (HttpHeaders/UriInfo/etc) API
> solve this, or will I have the same problem?
>
If your resource classes are in the default per-request scope then
there will be no issue as direct references of the JAX-RS classes you
mention will be injected.
If you are using non-per-request-scope you can do:
@Context com.sun.jersey.spi.inject.Injectable<UriInfo> ui;
Read full article from Jersey - Thread access to @Context objects?
No comments:
Post a Comment