Class SubjectRunnable
java.lang.Object
org.apache.shiro.subject.support.SubjectRunnable
- All Implemented Interfaces:
Runnable
A
SubjectRunnable ensures that a target/delegate Runnable will execute such that any
call to SecurityUtils.getSubject() during the
Runnable's execution will return the associated Subject instance. The SubjectRunnable
instance can be run on any thread (the current thread or asynchronously on another thread) and the
SecurityUtils.getSubject() call will still work properly. This implementation also guarantees that Shiro's
thread state will be identical before and after execution to ensure threads remain clean in any thread-pooled
environment.
When instances of this class run(), the following occurs:
- The Subject and any of its associated thread state is first bound to the thread that executes the
Runnable. - The delegate/target
Runnableisrun - Any previous thread state that might have existed before the
Subjectwas bound is fully restored
Usage
This is typically considered a support class and is not often directly referenced. Most people prefer to use
the Subject.execute or
Subject.associateWith methods, which transparently perform the
necessary association logic.
SubjectAwareExecutor, which transparently uses
instances of this class but does not require referencing Shiro's API at all.- Since:
- 1.0
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionSubjectRunnable(Subject subject, Runnable delegate) Creates a newSubjectRunnablethat, when executed, will execute the targetdelegate, but guarantees that it will run associated with the specifiedSubject.protectedSubjectRunnable(ThreadState threadState, Runnable delegate) Creates a newSubjectRunnablethat, when executed, will perform thread statebindingand guaranteedrestorationbefore and after theRunnable's execution, respectively. -
Method Summary
-
Field Details
-
threadState
-
-
Constructor Details
-
SubjectRunnable
Creates a newSubjectRunnablethat, when executed, will execute the targetdelegate, but guarantees that it will run associated with the specifiedSubject.- Parameters:
subject- the Subject to associate with the delegate's execution.delegate- the runnable to run.
-
SubjectRunnable
protected SubjectRunnable(ThreadState threadState, Runnable delegate) throws IllegalArgumentException Creates a newSubjectRunnablethat, when executed, will perform thread statebindingand guaranteedrestorationbefore and after theRunnable's execution, respectively.- Parameters:
threadState- the thread state to bind and unbind before and after the runnable's execution.delegate- the delegateRunnableto execute when this instance isrun().- Throws:
IllegalArgumentException- if either theThreadStateorRunnablearguments arenull.
-
-
Method Details
-
run
-
doRun
-