Package org.apache.shiro.concurrent
Class SubjectAwareExecutorService
- java.lang.Object
-
- org.apache.shiro.concurrent.SubjectAwareExecutor
-
- org.apache.shiro.concurrent.SubjectAwareExecutorService
-
- All Implemented Interfaces:
Executor
,ExecutorService
- Direct Known Subclasses:
SubjectAwareScheduledExecutorService
public class SubjectAwareExecutorService extends SubjectAwareExecutor implements ExecutorService
ExecutorService
implementation that will automatically first associate any argumentRunnable
orCallable
instances with thecurrently available subject
and then dispatch the Subject-enabled runnable or callable to an underlying delegateExecutorService
instance. The principle is the same as the parentSubjectAwareExecutor
class, but enables the richerExecutorService
API. This is a simplification for applications that want to execute code as the currently executingSubject
on another thread, but don't want or need to call theSubject.associateWith(Runnable)
orSubject.associateWith(Callable)
methods and dispatch them to a Thread manually. This simplifies code and reduces Shiro dependencies across application source code. Consider this code that could be repeated in many places across an application:Callable
applicationWork = //instantiate or acquire Callable from somewhereSubject
subject =SecurityUtils
.getSubject()
;Callable
work = subject.associateWith(applicationWork)
;anExecutorService
.submit(work)
;ExecutorService
instance used at runtime is an instance of this class (which delegates to the target ExecutorService that you want), all places in code like the above reduce to this:Callable
applicationWork = //instantiate or acquire Callable from somewhereanExecutorService
.submit(work)
;- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description SubjectAwareExecutorService()
SubjectAwareExecutorService(ExecutorService target)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> Collection<Callable<T>>
associateWithSubject(Collection<? extends Callable<T>> tasks)
protected <T> Callable<T>
associateWithSubject(Callable<T> task)
boolean
awaitTermination(long timeout, TimeUnit unit)
ExecutorService
getTargetExecutorService()
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
boolean
isShutdown()
boolean
isTerminated()
void
setTargetExecutor(Executor targetExecutor)
Sets target Executor instance that will actually execute the subject-associated Runnable instances.void
setTargetExecutorService(ExecutorService targetExecutorService)
void
shutdown()
List<Runnable>
shutdownNow()
Future<?>
submit(Runnable task)
<T> Future<T>
submit(Runnable task, T result)
<T> Future<T>
submit(Callable<T> task)
-
Methods inherited from class org.apache.shiro.concurrent.SubjectAwareExecutor
associateWithSubject, execute, getSubject, getTargetExecutor
-
-
-
-
Constructor Detail
-
SubjectAwareExecutorService
public SubjectAwareExecutorService()
-
SubjectAwareExecutorService
public SubjectAwareExecutorService(ExecutorService target)
-
-
Method Detail
-
getTargetExecutorService
public ExecutorService getTargetExecutorService()
-
setTargetExecutorService
public void setTargetExecutorService(ExecutorService targetExecutorService)
-
setTargetExecutor
public void setTargetExecutor(Executor targetExecutor)
Description copied from class:SubjectAwareExecutor
Sets target Executor instance that will actually execute the subject-associated Runnable instances.- Overrides:
setTargetExecutor
in classSubjectAwareExecutor
- Parameters:
targetExecutor
- the target Executor instance that will actually execute the subject-associated Runnable instances.
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNow
in interfaceExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfaceExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
awaitTermination
in interfaceExecutorService
- Throws:
InterruptedException
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submit
in interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submit
in interfaceExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submit
in interfaceExecutorService
-
associateWithSubject
protected <T> Collection<Callable<T>> associateWithSubject(Collection<? extends Callable<T>> tasks)
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
-