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
ExecutorServiceimplementation that will automatically first associate any argumentRunnableorCallableinstances with thecurrently available subjectand then dispatch the Subject-enabled runnable or callable to an underlying delegateExecutorServiceinstance. The principle is the same as the parentSubjectAwareExecutorclass, but enables the richerExecutorServiceAPI. This is a simplification for applications that want to execute code as the currently executingSubjecton 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:
Instead, if theCallableapplicationWork = //instantiate or acquire Callable from somewhereSubjectsubject =SecurityUtils.getSubject();Callablework = subject.associateWith(applicationWork);anExecutorService.submit(work);ExecutorServiceinstance 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
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)booleanawaitTermination(long timeout, TimeUnit unit)ExecutorServicegetTargetExecutorService()<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> TinvokeAny(Collection<? extends Callable<T>> tasks)<T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)booleanisShutdown()booleanisTerminated()voidsetTargetExecutor(Executor targetExecutor)Sets target Executor instance that will actually execute the subject-associated Runnable instances.voidsetTargetExecutorService(ExecutorService targetExecutorService)voidshutdown()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: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
public void shutdown()
- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNowin interfaceExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfaceExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submitin interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submitin interfaceExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submitin 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:
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
-
-