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 theCallable
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:
Notice there is no use of the Shiro API in the 2nd code block, encouraging the principle of loose coupling across your codebase.Callable
applicationWork = //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, getTargetExecutor
public SubjectAwareExecutorService()
public SubjectAwareExecutorService(ExecutorService target)
public ExecutorService getTargetExecutorService()
public void setTargetExecutorService(ExecutorService targetExecutorService)
public void setTargetExecutor(Executor targetExecutor)
SubjectAwareExecutor
setTargetExecutor
in class SubjectAwareExecutor
targetExecutor
- the target Executor instance that will actually execute the subject-associated Runnable
instances.public void shutdown()
shutdown
in interface ExecutorService
public List<Runnable> shutdownNow()
shutdownNow
in interface ExecutorService
public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isTerminated()
isTerminated
in interface ExecutorService
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
awaitTermination
in interface ExecutorService
InterruptedException
public <T> Future<T> submit(Callable<T> task)
submit
in interface ExecutorService
public <T> Future<T> submit(Runnable task, T result)
submit
in interface ExecutorService
public Future<?> submit(Runnable task)
submit
in interface ExecutorService
protected <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 ExecutorService
InterruptedException
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
invokeAll
in interface ExecutorService
InterruptedException
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
invokeAny
in interface ExecutorService
InterruptedException
ExecutionException
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
invokeAny
in interface ExecutorService
InterruptedException
ExecutionException
TimeoutException
Copyright © 2004–2016 The Apache Software Foundation. All rights reserved.