java - 哪个 TaskExecutor 用于异步日志记录?

标签 java spring multithreading asynchronous spring-async

我有一个 REST 网络服务,想记录所有传入和传出的 XML 请求。 由于它们可能非常大,而且我还必须应用某种转换,所以我想在异步线程中执行它。

到目前为止,我只是在记录器方法上使用了 @Async 注释。这将使用默认的 SimpleAsyncTaskExecutor,它“不重用任何线程”: https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/htmlsingle/#scheduling-task-executor-types

问题:我是否应该更好地定义自己的 ThreadPoolTask​​Executor 而不是依赖默认的简单执行器?为短期日志记录任务设置一个“重用线程”执行程序是否明智?

进一步考虑:我还将进行一些异步数据库行更新,这些更新也应该使用 @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?

使用默认的 ThreadPoolTask​​Executor 除非你需要自定义它。

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/

相关文章:

java - Retrofit:使用 GSON 将 JSON 解析为多个 Java 对象

java - HTTP 请求不会经过 Controller

java - Spring 配置 - 初始化代码的最佳实践

java - 线程 "Basic L&F File Loading Thread"中出现异常

来自 CursorWindow 的 java.lang.IllegalStateException : Couldn't read row 0, col -1。确保 Cursor 之前正确初始化

spring - @secured 具有授予的权限抛出访问被拒绝异常

c++ - QTimer 与独立线程

c# - 如何保持进程在远程 Windows 服务器上运行

.net - 使用 ThreadStatic 替换昂贵的 locals —— 好主意?

java - ActiveMQ-如何检测非 Activity 主题