Transferring Request Scope across multiple threads

229 views
Skip to first unread message

Ganesh J

unread,
May 13, 2017, 6:28:47 PM5/13/17
to google-guice

Hi


We are using Guice 4.1 in out Jetty based web applications. We are exposing APIs through jersey 1.19.3. We are using com.google.inject.servlet.GuiceFilter in our web.xml to intercept all requests. Now we do have couple of request scopes beans defined. Also we have within a single request processing flow multiple thread hops: Main Jetty thread -> FIrst thread -> Second thread. Request scope objects are injected using Provider to classes that are invoked from second thread. So to make that work we wrapped the callable submitted to first thread using ServletScopes.transferRequest. The to transfer scope to second tread we tried wrapping callable submitted to second thread by ServletScopes.transferRequest. Now when this is executed the execution is blocked. When we referred to the javadoc for ServletScopes.transferRequest


"Because request-scoped objects are not typically thread-safe, the callable returned by this method must not be run on a different thread until the current request scope has terminated. The returned callable will block until the current thread has released the request scope."


It is a bit confusing why this is happening only to the second callable. The argument of thread safety can also be made for the first callable.


We are kind of stuck here. Any suggestions on the right approach to propagate scope would help immensely here


Thanks
Ganesh

First L

unread,
Apr 29, 2024, 10:46:50 PMApr 29
to google-guice
Hi Ganish, were you (or anyone else) able to figure out how to get this working?
Oddly enough, when I only have one async call, the com.google.inject.servlet.RequestScoped does not have an issue. It's only when I add a second async call and link it with the first one using thenCombine, do I run into 

[java] com.google.inject.ProvisionException: Unable to provision, see the following errors: [java] [java] 1) [Guice/ErrorInCustomProvider]: OutOfScopeException: Cannot access Key[type=FooContext, annotation=[none]] outside of a scoping block [java] at BarModule.createFooContext(BarModule.java:1111) [java] while locating FooContext [java] [java] Learn more: [java] https://github.com/google/guice/wiki/ERROR_IN_CUSTOM_PROVIDER [java] [java] 1 error
I tried following https://stackoverflow.com/a/33945302/23282628 but I just get "Not in a request scope" error as a result when trying to use ServletScopes.transferRequest
Reply all
Reply to author
Forward
0 new messages