dpr*_*dpr 2 java spring asynchronous shiro
我正在使用 Spring 的@EnableAsync功能来异步执行方法。为了安全起见,我使用 Apache Shiro。在异步执行的代码中,我需要访问附加到触发异步调用的线程的 Shiro 主题。
Shiro 支持在不同线程中使用现有主题,方法是将主题与Callable要在不同线程上执行的主题相关联(请参阅此处):
Subject.associateWith(Callable)
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法直接访问Callable这些东西,因为这些东西是由 Spring 封装的。我发现我需要扩展 SpringAnnotationAsyncExecutionInterceptor来将我的主题与创建的主题相关联Callable(这是简单的部分)。
现在的问题是如何让 Spring 使用我的自定义AnnotationAsyncExecutionInterceptor而不是默认的。默认值是在AsyncAnnotationAdvisor和中创建的AsyncAnnotationBeanPostProcessor。我当然也可以扩展这些类,但这只会转变为问题,因为我需要让 Spring 再次使用我的扩展类。
有什么办法可以实现我想要的吗?
我也可以添加一个新的自定义异步注释。但我认为这不会有太大帮助。
更新:AnnotationAsyncExecutionInterceptor实际上我需要定制的
发现是错误的。一次偶然的机会,我偶然发现org.apache.shiro.concurrent.SubjectAwareExecutorService它完全符合我的要求,并让我觉得我可以简单地提供一个自定义执行器,而不是自定义拦截器。详情请参阅我的回答。
我设法实现了我想要的 - shiro subject 自动绑定和取消绑定到由 spring 的异步支持执行的任务 - 通过提供以下扩展版本ThreadPoolTaskExecutor:
public class SubjectAwareTaskExecutor extends ThreadPoolTaskExecutor {
@Override
public void execute(final Runnable aTask) {
final Subject currentSubject = ThreadContext.getSubject();
if (currentSubject != null) {
super.execute(currentSubject.associateWith(aTask));
} else {
super.execute(aTask);
}
}
... // override the submit and submitListenable method accordingly
}
Run Code Online (Sandbox Code Playgroud)
为了让 spring 使用这个执行器,我必须实现一个AsyncConfigurer返回我的自定义执行器的:
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
final SubjectAwareTaskExecutor executor = new SubjectAwareTaskExecutor();
executor.setBeanName("async-executor");
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
Run Code Online (Sandbox Code Playgroud)
通过此更改,父线程的主题将自动在带有注释的方法中可用@Async,并且可能更重要的是,在执行异步方法后,主题将与线程解除连接。
| 归档时间: |
|
| 查看次数: |
1967 次 |
| 最近记录: |