Spring Boot Asynchronous Execution Using Async Annotation
Posted By : Vishal Kumar | 26-May-2018
In this post we will talk about spring boot asynchronous execution bolster utilizing async errand agent highlight to execute an undertaking in an alternate string. We will investigate designing SimpleAsyncTaskExecutor, ConcurrentTaskExecutor, ThreadPoolExecutor in a spring venture. Aside from this, we will be likewise investigating how real technique return compose can be wrapped in a Future question while managing async conduct in spring. So let us begin with spring boot async errand agent.
Async Configuration in Spring
To empower async conduct in Spring, comment on your setup class with @EnableAsync.
@EnableAsync
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@EnableAsync: - It recognizes @Async explanation.
mode - The mode() trait controls how exhortation is connected. Naturally, its esteem is AdviceMode.PROXY. Note that if the mode() is set to AdviceMode.
proxyTargetClass - It characterizes the sort of intermediary that would be utilized from CGLIB or JDK.By default its CGLIB.
Using @Async Annotation
This comment is utilized on the strategy level for those strategies which you need its execution to be in a different string. This explanation fills in of course if an open technique is clarified with this comment.
Additionally, the strategy should be called from an alternate class so it can be proxied else the intermediary will be skirted.
Following is a case of a @Async commented on technique. It doesn't restore any esteem.
@Override
@Async
public void createUserWithDefaultExecutor(){
//SimpleAsyncTaskExecutor
System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
System.out.println("User created with default executor");
}
@Async Annotation with Return Type of a Method
The real return kind of a technique can be wrapped in a Future object.
@Override @Async public FuturecreateUserAndReturn() { System.out.println("Currently thread name - " + Thread.currentThread().getName()); try { User user = new User(); user.setEmail("[email protected]"); Thread.sleep(3000); return new AsyncResult (user); } catch (InterruptedException e) { System.out.println(e.getLocalizedMessage()); } return null; }
Following is a test method for this.
@Test public void createUserAndReturnTest() throws ExecutionException, InterruptedException { System.out.println("Current Thread " + Thread.currentThread().getName()); long startingTime = System.currentTimeMillis(); Futureuser = service.createUserAndReturn(); user.get(); assertTrue((System.currentTimeMillis() - startingTime) >= 3000); }
In the next article, we will discuss ThreadPoolTastExecutor.
Thanks.
Hope this will help you!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vishal Kumar
Vishal Kumar is Master in Computers Application. He has good technical skills in Java and always motivated to learn new things.