Class RequestScopePropagator

  • Direct Known Subclasses:
    GuiceRequestScopePropagator, ThreadLocalRequestScopePropagator

    public abstract class RequestScopePropagator
    extends Object
    Base class for propagating request-scoped data between threads.

    Request scopes are typically linked to a ThreadLocal, which is only available to the current thread. In order to allow background work involving RequestScoped data, the ThreadLocal data must be copied from the request thread to the new background thread.

    Every type of RequestScope must provide an implementation of RequestScopePropagator. See wrap(Callable) for details on the implementation, usage, and restrictions.

    See Also:
    ThreadLocalRequestScopePropagator
    • Constructor Detail

      • RequestScopePropagator

        protected RequestScopePropagator​(com.google.inject.Scope scope,
                                         ThreadLocalRequestContext local)
    • Method Detail

      • wrap

        public final <T> Callable<T> wrap​(Callable<T> callable)
        Ensures that the current request state is available when the passed in Callable is invoked.

        If needed wraps the passed in Callable in a new Callable that propagates the current request state when the returned Callable is invoked. The method must be called in a request scope and the returned Callable may only be invoked in a thread that is not already in a request scope or is in the same request scope. The returned Callable will inherit toString() from the passed in Callable. A ScheduledThreadPoolExecutor does not accept a Callable, so there is no ProjectCallable implementation. Implementations of this method must be consistent with Guice's ServletScopes.continueRequest(Callable, java.util.Map).

        There are some limitations:

        • Derived objects (i.e. anything marked created in a request scope) will not be transported.
        • State changes to the request scoped context after this method is called will not be seen in the continued thread.
        Parameters:
        callable - the Callable to wrap.
        Returns:
        a new Callable which will execute in the current request scope.
      • wrap

        public final Runnable wrap​(Runnable runnable)
        Wraps runnable in a new Runnable that propagates the current request state when the runnable is invoked. The method must be called in a request scope and the returned Runnable may only be invoked in a thread that is not already in a request scope. The returned Runnable will inherit toString() from the passed in Runnable. Furthermore, if the passed runnable is of type ProjectRunnable, the returned runnable will be of the same type with the methods delegated.

        See wrap(Callable) for details on implementation and usage.

        Parameters:
        runnable - the Runnable to wrap.
        Returns:
        a new Runnable which will execute in the current request scope.