Class SubjectCallable<V>

  • All Implemented Interfaces:

    public class SubjectCallable<V>
    extends Object
    implements Callable<V>
    A SubjectCallable associates a Subject with a target/delegate Callable to ensure proper Subject thread-state management when the Callable executes. This ensures that any calls to SecurityUtils.getSubject() during the target Callable's execution still work correctly even if the Callable executes on a different thread than the one that created it. This allows Subject access during asynchronous operations.

    When instances of this class execute (typically via a ExecutorService), the following occurs:

    1. The specified Subject any of its associated thread state is first bound to the thread that executes the Callable.
    2. The delegate/target Callable is executed
    3. The previous thread state that might have existed before the Subject was bound is fully restored

    This behavior ensures that the thread that executes this Callable, which is often a different thread than the one that created the instance, retains a Subject to support SecurityUtils.getSubject() invocations. It also guarantees that the running thread remains 'clean' in any thread-pooled environments.


    This is typically considered a support class and is not often directly referenced. Most people prefer to use the Subject.associateWith method, which will automatically return an instance of this class.

    An even more convenient alternative is to use a SubjectAwareExecutorService, which transparently uses instances of this class.

    See Also:
    Subject.associateWith(Callable), SubjectAwareExecutorService