On Oct 23, 2009, at 9:22 PM, Chris Hubick wrote:
> Hi,
>
> I am in the process of porting my Servlet application to Jersey using
> it's ServletContainer. Currently, until I can transition to the
> JAX-RS API, @Context HttpServletRequest/Response references are used
> for things like "Accept-Language" header access, response URL
> encoding, etc.
>
> I have found that if I attempt to access these objects from a spawned
> worker thread, it throws java.lang.IllegalStateException inside
> com.sun.jersey.server.impl.container.servlet.ThreadLocalInvoker.
>
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;
Hope this helps,
Paul.
> If it's needed (?), I can construct a synchronization utility class
> for JAX-RS calls, like I do for the Servlet API, but since not even
> that appears to be working, I'm scared I have to somehow funnel all
> calls from the worker threads back through the original calling
> thread, or replicate all the data I need access to?
>
> Much Thanks!
>
> --
> Chris Hubick
> mailto:chris_at_hubick.com
> http://www.hubick.com/chris/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>