A {@code SubjectRunnable} ensures that a target/delegate {@link Runnable Runnable} will execute such that anycall to {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} during the{@code Runnable}'s execution will return the associated {@code Subject} instance. The {@code SubjectRunnable}instance can be run on any thread (the current thread or asynchronously on another thread) and the {@code SecurityUtils.getSubject()} call will still work properly. This implementation also guarantees that Shiro'sthread state will be identical before and after execution to ensure threads remain clean in any thread-pooled environment.
When instances of this class {@link Runnable#run() run()}, the following occurs:
- The Subject and any of its associated thread state is first bound to the thread that executes the {@code Runnable}.
- The delegate/target {@code Runnable} is {@link #doRun(Runnable) run}
- Any previous thread state that might have existed before the {@code Subject} was bound is fully restored
Usage
This is typically considered a support class and is not often directly referenced. Most people prefer to use the {@code Subject.}{@link Subject#execute(Runnable) execute} or{@code Subject.}{@link Subject#associateWith(Runnable) associateWith} methods, which transparently perform thenecessary association logic.
An even more convenient alternative is to use a {@link org.apache.shiro.concurrent.SubjectAwareExecutor SubjectAwareExecutor}, which transparently uses instances of this class but does not require referencing Shiro's API at all.
@see Subject#associateWith(Runnable)
@see org.apache.shiro.concurrent.SubjectAwareExecutor SubjectAwareExecutor
@since 1.0