4

I am using Guice's RequestScoped and Provider in order to get instances of some classes during a user request. This works fine currently. Now I want to do some job in a background thread, using the same instances created during request. However, when I call Provider.get(), guice returns an error:

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.

afaik, this is due to the fact that Guice uses thread local variables in order to keep track of the current request instances, so it is not possible to call Provider.get() from a thread different from the thread that is handling the request.

How can I get the same instances inside new threads using Provider? It is possible to achieve this writing a custom scope?

3 Answers 3

7

I recently solved this exact problem. There are a few things you can do. First, read up on ServletScopes.continueRequest(), which wraps a callable so it will execute as if it is within the current request. However, that's not a complete solution because it won't forward @RequestScoped objects, only basic things like the HttpServletResponse. That's because @RequestScoped objects are not expected to be thread safe. You have some options:

  • If your entire @RequestScoped hierarchy is computable from just the HTTP response, you're done! You will get new instances of these objects in the other thread though.

  • You can use the code snippet below to explicitly forward all RequestScoped objects, with the caveat that they will all be eagerly instantiated.

  • Some of my @RequestScoped objects couldn't handle being eagerly instantiated because they only work for certain requests. I extended the below solution with my own scope, @ThreadSafeRequestScoped, and only forwarded those ones.

Code sample:

public class RequestScopePropagator {
    private final Map<Key<?>, Provider<?>> requestScopedValues = new HashMap<>();

    @Inject
    RequestScopePropagator(Injector injector) {
        for (Map.Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
            Key<?> key = entry.getKey();
            Binding<?> binding = entry.getValue();
            // This is like Scopes.isSingleton() but we don't have to follow linked bindings
            if (binding.acceptScopingVisitor(IS_REQUEST_SCOPED)) {
                requestScopedValues.put(key, binding.getProvider());
            }
        }
    }

    private final BindingScopingVisitor<Boolean> IS_REQUEST_SCOPED = new BindingScopingVisitor<Boolean>() {
        @Override
        public Boolean visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
            return scopeAnnotation == RequestScoped.class;
        }

        @Override
        public Boolean visitScope(Scope scope) {
            return scope == ServletScopes.REQUEST;
        }

        @Override
        public Boolean visitNoScoping() {
            return false;
        }

        @Override
        public Boolean visitEagerSingleton() {
            return false;
        }
    };

    public <T> Callable<T> continueRequest(Callable<T> callable) {
        Map<Key<?>, Object> seedMap = new HashMap<>();
        for (Map.Entry<Key<?>, Provider<?>> entry : requestScopedValues.entrySet()) {
            // This instantiates objects eagerly
            seedMap.put(entry.getKey(), entry.getValue().get());
        }

        return ServletScopes.continueRequest(callable, seedMap);
    }
}
1

I have faced the exact same problem but solved it in a different way. I use jOOQ in my projects and I have implemented transactions using a request scope object and an HTTP filter.

But then I created a background task which is spawned by the server in the middle of the night. And the injection is not working because there is no request scope.

Well. The solutions is simple: create a request scope manually. Of course there is no HTTP request going on but that's not the point (mostly). It is the concept of the request scope. So I just need a request scope that exists alongside my background task.

Guice has an easy way to create a request scope: ServletScope.scopeRequest.

public class MyBackgroundTask extends Thread {
    @Override
    public void run() {
        RequestScoper scope = ServletScopes.scopeRequest(Collections.emptyMap());
        try ( RequestScoper.CloseableScope ignored = scope.open() ) {
            doTask();
        }
    }

    private void doTask() {

    }
}

Oh, and you probably will need some injections. Be sure to use providers there, you want to delay it's creation until inside the created scope.

-2

Better use ServletScopes.transferRequest(Callable) in Guice 4

1
  • transferRequest is not thread safe, otherwise it would be perfect. May 9, 2017 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.