public class SubjectAwareExecutorService extends SubjectAwareExecutor implements ExecutorService
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);
| Constructor and Description |
|---|
SubjectAwareExecutorService() |
SubjectAwareExecutorService(ExecutorService target) |
| Modifier and Type | Method and Description |
|---|---|
protected <T> Callable<T> |
associateWithSubject(Callable<T> task) |
protected <T> Collection<Callable<T>> |
associateWithSubject(Collection<? extends Callable<T>> tasks) |
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() |
<T> Future<T> |
submit(Callable<T> task) |
Future<?> |
submit(Runnable task) |
<T> Future<T> |
submit(Runnable task,
T result) |
associateWithSubject, execute, getSubject, getTargetExecutorpublic SubjectAwareExecutorService()
public SubjectAwareExecutorService(ExecutorService target)
public ExecutorService getTargetExecutorService()
public void setTargetExecutorService(ExecutorService targetExecutorService)
public void setTargetExecutor(Executor targetExecutor)
SubjectAwareExecutorsetTargetExecutor in class SubjectAwareExecutortargetExecutor - the target Executor instance that will actually execute the subject-associated Runnable
instances.public void shutdown()
shutdown in interface ExecutorServicepublic List<Runnable> shutdownNow()
shutdownNow in interface ExecutorServicepublic boolean isShutdown()
isShutdown in interface ExecutorServicepublic boolean isTerminated()
isTerminated in interface ExecutorServicepublic boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface ExecutorServiceInterruptedExceptionpublic <T> Future<T> submit(Callable<T> task)
submit in interface ExecutorServicepublic <T> Future<T> submit(Runnable task, T result)
submit in interface ExecutorServicepublic Future<?> submit(Runnable task)
submit in interface ExecutorServiceprotected <T> Collection<Callable<T>> associateWithSubject(Collection<? extends Callable<T>> tasks)
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
invokeAll in interface ExecutorServiceInterruptedExceptionpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
invokeAll in interface ExecutorServiceInterruptedExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
invokeAny in interface ExecutorServiceInterruptedExceptionExecutionExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
invokeAny in interface ExecutorServiceInterruptedExceptionExecutionExceptionTimeoutExceptionCopyright © 2004-2016 The Apache Software Foundation. All Rights Reserved.