我有一个 REST
网络服务,想记录所有传入和传出的 XML
请求。
由于它们可能非常大,而且我还必须应用某种转换,所以我想在异步线程中执行它。
到目前为止,我只是在记录器方法上使用了 @Async
注释。这将使用默认的 SimpleAsyncTaskExecutor
,它“不重用任何线程”:
https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/htmlsingle/#scheduling-task-executor-types
问题:我是否应该更好地定义自己的 ThreadPoolTaskExecutor
而不是依赖默认的简单执行器?为短期日志记录任务设置一个“重用线程”执行程序是否明智?
进一步考虑:我还将进行一些异步数据库行更新,这些更新也应该使用 @Async
执行,并且可能使用相同的执行程序。
我的主要问题是:我不想考虑线程的固定池大小、容量、节流限制等。我只想告诉我的例程:“在异步线程中执行以下逻辑。”只需在上面堆放任何东西即可。
我必须使用哪个 TaskExecutors
,应该应用哪个配置?
例如下面的执行器是否合适?
@Bean
public ThreadPoolTaskExecutor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
return executor;
}
最佳答案
should I better define my own ThreadPoolTaskExecutor rather than relying on the default simple executor?
使用默认的 ThreadPoolTaskExecutor
除非你需要自定义它。
Would it be wise to have a "reusing threads" executor for the short-lived logging tasks?
是的。
I don't want to think about a fixed pool size of threads, capacity, throttle limits etc. I just want to tell my routine: "Execute the following logic in an async thread." And just stack anything on it.
Which of the TaskExecutors would I have to use for it, and which configuration should be applied?
ThreadPoolTaskExecutor足够好了。在示例代码中将池大小设置为 Runtime.getRuntime().availableProcessors()
。
关于java - 哪个 TaskExecutor 用于异步日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46706964/