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 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:
Callable applicationWork = //instantiate or acquire Callable from somewhere
Subject subject = SecurityUtils.getSubject();
Callable work = subject.associateWith(applicationWork);
anExecutorService.submit(work);
Instead, if the 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 somewhere
anExecutorService.submit(work);
Notice there is no use of the Shiro API in the 2nd code block, encouraging the principle of loose coupling across
your codebase.
- Since:
- 1.0
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SubjectAwareExecutorService
public SubjectAwareExecutorService()
SubjectAwareExecutorService
public SubjectAwareExecutorService(ExecutorService target)
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 class SubjectAwareExecutor
- Parameters:
targetExecutor - the target Executor instance that will actually execute the subject-associated Runnable
instances.
shutdown
public void shutdown()
- Specified by:
shutdown in interface ExecutorService
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNow in interface ExecutorService
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown in interface ExecutorService
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated in interface ExecutorService
awaitTermination
public boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
- Specified by:
awaitTermination in interface ExecutorService
- Throws:
InterruptedException
associateWithSubject
protected <T> Callable<T> associateWithSubject(Callable<T> task)
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submit in interface ExecutorService
submit
public <T> Future<T> submit(Runnable task,
T result)
- Specified by:
submit in interface ExecutorService
submit
public Future<?> submit(Runnable task)
- Specified by:
submit in interface ExecutorService
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 interface ExecutorService
- Throws:
InterruptedException
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException
- Specified by:
invokeAll in interface ExecutorService
- Throws:
InterruptedException
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException,
ExecutionException
- Specified by:
invokeAny in interface ExecutorService
- 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 interface ExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.