项目中开启异步注解@EnableAsync
项目中使用异步注解:@Async
在远程调用时需要携带授权信息,如果不配置特定的线程池,系统使用默认的异步线程池,无法传递授权信息,需要在项目中配置:
@Component
public class AsyncConfig extends AsyncConfigurerSupport {
/**
* 异步执行需要使用权限框架自带的包装线程池 保证权限信息的传递
*/
@Override
public Executor getAsyncExecutor() {
int processors = Runtime.getRuntime().availableProcessors();
ExecutorService executorService = new ThreadPoolExecutor(processors, processors << 1,
5000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
new NamedThreadFactory("async-security-pool-", false));
return new DelegatingSecurityContextExecutorService(executorService);
}
}
同样项目中如果使用线程池调用远程接口,需要使用DelegatingSecurityContextExecutorService线程池。