Discussion:
RequestScoped and background threads
Arun Thirupathi
2017-06-13 18:21:00 UTC
Permalink
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.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/2238b96f-0b42-408b-8bbb-08d58f3a0d1a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2017-06-13 19:17:33 UTC
Permalink
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.
Post by 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.
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.
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/2238b96f-0b42-408b-8bbb-08d58f3a0d1a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/EA5D362D-7206-443F-9AA1-CC261370124E%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Mario Saban
2017-06-16 13:56:41 UTC
Permalink
Post by 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
<https://google.github.io/guice/api-docs/4.1/javadoc/com/google/inject/servlet/ServletScopes.html>on
how to transfer the current request to another thread.
Post by Arun Thirupathi
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.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/251762fa-04de-477c-909a-c4d9972b3ef1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...