Also note that if you call execute as shown, any dispatching to other
threads via Shiro's SubjectAwareExecutor, SubjectAwareExecutorService,
and SubjectAwareScheduledExecutorService components will automatically
retain the Subject and perform automatic cleanup.
For example:
subject.execute(new Runnable() {
public void run() {
//subject is thread-bound here
doSomethingConcurrently();
}
}
//subject is NOT thread-bound here
public void doSomethingConcurrently() {
//subject is thread-bound here
Collection<Callable> tasks = //get tasks
//the returned service instance must be one of Shiro's
//SubjectAware* implementations, but note that with DI
//this method isn't aware of Shiro APIs at all:
ExecutorService execSvc = getExecutorService();
//each task will be able to call SecurityUtils.getSubject()
//successfully, with the current Subject being retained.
//when each task is done, its thread will be cleared of
//the Subject automatically:
List<Future> results = invokeAll(tasks);
}
HTH,
Les