4

I have a REST webservice and want to log any incoming and outgoing XML requests. As they can be quite large and I also have to apply some sort of transformation, I'd like to execute that in an async thread.

So far I'm just using @Async annotation on the logger method. That will use the default SimpleAsyncTaskExecutor, which "does not reuse any threads": https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/htmlsingle/#scheduling-task-executor-types

Question: should I better define my own ThreadPoolTaskExecutor rather than relying on the default simple executor? Would it be wise to have a "reusing threads" executor for the short-lived logging tasks?

Further do consider: I will also be having some async database row updates that should also be executing using @Async, and probably with the same executor.

My main problem is: 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?

Would eg the following executor fit?

@Bean
public ThreadPoolTaskExecutor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(4);
    return executor;
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

1

should I better define my own ThreadPoolTaskExecutor rather than relying on the default simple executor?

Use default ThreadPoolTaskExecutor unless you need to customize it.

Would it be wise to have a "reusing threads" executor for the short-lived logging tasks?

Yes.

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 is good enough. set pool size as Runtime.getRuntime().availableProcessors() in your example code.

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Should I set `corePoolSize` or `maxPoolSize` to processors count? Or both? – membersound Oct 13 '17 at 10:40
  • maxPoolSize. If you wan't to treat it as FixedThreadPool, set both the values. – Ravindra babu Oct 13 '17 at 10:41
  • And if I start eg with a `corePoolSize=1`: when will it be increased? If one process is still ongoing? So if I have 4 concurrent log statements, will they be logged distributed on 4 cores/threads directly? Or would they stack in the queue of pool1, because the poolSize only increases if the queue of that pool is full? (and afaik the queue is inifinite by default in ThreadPoolTaskExecutor?) – membersound Oct 13 '17 at 10:45
  • 1
    Have a look at this page for more details : https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html. . When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. – Ravindra babu Oct 13 '17 at 10:55