In this post we will be discussing about spring boot asynchronous execution support using async task executor feature to execute task in a different thread. We will take a look into configuring SimpleAsyncTaskExecutor, ConcurrentTaskExecutor, ThreadPoolExecutor in a spring project. Apart from this, we will be also looking into how actual method return type can be wrapped in a Future object while dealing with async behaviour in spring.So let us get started with spring boot async task executor.
To enable async behaviour in Spring, annotate your configuration class with @EnableAsync.
@EnableAsync: - It detects @Async annotation.
mode - The mode() attribute controls how advice is applied. By default its value is AdviceMode.PROXY. Note that if the mode() is set to AdviceMode.ASPECTJ, then the value of the proxyTargetClass() attribute will be ignored. Note also that in this case the spring-aspects module JAR must be present on the classpath.
proxyTargetClass - It defines the type of proxy that would be used from CGLIB or JDK.By default its CGLIB.
This annotation is used on the method level for those method which you want it execution to be in a seperate thread.This annotation works as expected if a public method is annotated with this annotation.
Also, the method needs to be called from a different class so that it can be proxied else the proxy will be bypassed.
Following is an example of an @Async annotated method. It does not return any value.
By default, Spring will be searching for an associated thread pool definition: either a unique TaskExecutor bean in the context, or an Executor bean named "taskExecutor" otherwise. If neither of the two is resolvable, a SimpleAsyncTaskExecutor will be used to process async method invocations
The actual return type of a method can be wrapped in a Future object.
Following is a test method for this.
By default spring uses SimpleAsyncTaskExecutor to run methods annotated with @Async. We can also define our custom executor bean as follow and use it at method level.ThreadPoolTaskExecutor
These beans can be used at method level in following ways
SimpleAsyncTaskExecutor does make sense in cases, if you want to execute some long-time-executing tasks, e.g. if you want to compress log files at the end of a day. In other cases, if you want to execute a short-time-executing task every n seconds or minutes, you should use the ThreadPoolTaskExecutor, because of reusing of system resources.
To implement executor at application level, we require to implement AsyncConfigurer and override folowing methods.
Following is the exception handler.AsyncExceptionHandler.class
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.
Contact us: firstname.lastname@example.org