Class SubjectAwareExecutorService
java.lang.Object
org.apache.shiro.concurrent.SubjectAwareExecutor
org.apache.shiro.concurrent.SubjectAwareExecutorService
- All Implemented Interfaces:
Executor,ExecutorService
- Direct Known Subclasses:
SubjectAwareScheduledExecutorService
ExecutorService implementation that will automatically first associate any argument
Runnable or Callable instances with the currently available subject and then
dispatch the Subject-enabled runnable or callable to an underlying delegate
ExecutorService instance. The principle is the same as the
parent SubjectAwareExecutor class, but enables the richer ExecutorService API.
This is a simplification for applications that want to execute code as the currently
executing Subject on another thread, but don't want or need to call the
Subject.associateWith(Runnable) or Subject.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:
Instead, if theCallableapplicationWork = //instantiate or acquire Callable from somewhereSubjectsubject =SecurityUtils.getSubject();Callablework = 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:
Notice there is no use of the Shiro API in the 2nd code block, encouraging the principle of loose coupling across your codebase.CallableapplicationWork = //instantiate or acquire Callable from somewhereanExecutorService.submit(work);
- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescription -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> Collection<Callable<T>> associateWithSubject(Collection<? extends Callable<T>> tasks) protected <T> Callable<T> associateWithSubject(Callable<T> task) booleanawaitTermination(long timeout, TimeUnit unit) invokeAll(Collection<? extends Callable<T>> tasks) invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) <T> TinvokeAny(Collection<? extends Callable<T>> tasks) <T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) booleanbooleanvoidsetTargetExecutor(Executor targetExecutor) Sets target Executor instance that will actually execute the subject-associated Runnable instances.voidsetTargetExecutorService(ExecutorService targetExecutorService) voidshutdown()Future<?> <T> Future<T> <T> Future<T> Methods inherited from class org.apache.shiro.concurrent.SubjectAwareExecutor
associateWithSubject, execute, getSubject, getTargetExecutor
-
Constructor Details
-
SubjectAwareExecutorService
public SubjectAwareExecutorService() -
SubjectAwareExecutorService
-
-
Method Details
-
getTargetExecutorService
-
setTargetExecutorService
-
setTargetExecutor
Description copied from class:SubjectAwareExecutorSets target Executor instance that will actually execute the subject-associated Runnable instances.- Overrides:
setTargetExecutorin classSubjectAwareExecutor- Parameters:
targetExecutor- the target Executor instance that will actually execute the subject-associated Runnable instances.
-
shutdown
- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
- Specified by:
shutdownNowin interfaceExecutorService
-
isShutdown
- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
- Specified by:
isTerminatedin interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
associateWithSubject
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
associateWithSubject
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-