RequestScoped and background threads

703 views
Skip to first unread message

Arun Thirupathi

unread,
Jun 13, 2017, 2:21:00 PM6/13/17
to google-guice
In one of our projects we are binding an implementation in RequestScope. But when this is run from a background thread, it fails with the exception below. 

Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object.
Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply
com.google.inject.servlet.GuiceFilter as a servlet filter for this request.

Can I bind different implementations based on the scope ? I want to provide one implementation in RequestScope and other implementation in another scope.

Is there a way to configure Guice to ignore these errors silently. 

Currently I am thinking of the naive way of, try/catch all the usage of this object, which is far too many and there are new interactions with this object.

I run my project inside Google App Engine, if that matters.

Thanks,
Arun.

Stephan Classen

unread,
Jun 13, 2017, 3:18:40 PM6/13/17
to google...@googlegroups.com
The request scope uses internally a thread local. So when you run stuff in a background thread you are not in a request as guice sees it. You need to call the same method as the mentioned filter does inorder to begin and end a request.

Mario Saban

unread,
Jun 16, 2017, 9:56:42 AM6/16/17
to google-guice

Am Dienstag, 13. Juni 2017 20:21:00 UTC+2 schrieb Arun Thirupathi:
In one of our projects we are binding an implementation in RequestScope. But when this is run from a background thread, it fails with the exception below. 

Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object.
Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply
com.google.inject.servlet.GuiceFilter as a servlet filter for this request.

Have a look at ServletScopes on how to transfer the current request to another thread.

Reply all
Reply to author
Forward
0 new messages