Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Spring @Async vs Callable controller (async controller, servlet 3)

I would like to know the general use case of using @Async and Servlet 3 asynchronous request implementation in Spring using Callable.

As I understand, @Async is for making any method (specifically, any service method) execute asynchronously.

@Async void doSomething(String s) { // this will be executed asynchronously } 

and any controller which returns Callable

  @RequestMapping("/view") public Callable<String> callableWithView(final Model model) {     return new Callable<String>() {         @Override         public String call() throws Exception {             Thread.sleep(2000);             model.addAttribute("foo", "bar");             model.addAttribute("fruit", "apple");             return "views/html";         }     }; } 

I am confused on whento use what. What will be the effect of using Asynchronous servlet/controller and with spring @Async together?

like image 324
devThoughts Avatar asked Jun 18 '13 10:06

devThoughts


People also ask

What is difference between spring Webflux and MVC?

The main difference between the two frameworks is that spring-mvc is based on thread pools, while spring-webflux is based on event-loop mechanism. Both the models support commonly used annotations such as @Controller . A developer can run a reactive client from a spring-mvc controller to make calls to remote services.

Which annotation of Spring Model View Controller MVC allows an application to run heavy load jobs on a separate thread?

Spring 3.0 introduced the @Async annotation. @Async's goal is to allow the application to run heavy-load jobs on a separate thread. Also, the caller can wait for the result if interested. Hence the return type must not be void, and it be can be any of Future, CompletableFuture, or ListenableFuture.

What is spring callable?

Also callable is an alternative for Runnable, in the sense, It can return results and throw checked exceptions. Say you have a method public String aMethod(){ } This can be made asynchronous by simply returning a Callable interface. public Callable<String> aMethod(){ }

Are spring controllers async?

By default, Spring uses a SimpleAsyncTaskExecutor to actually run these methods asynchronously. But we can override the defaults at two levels: the application level or the individual method level.


1 Answers

This post has explanation for what you are looking for

Excerpt:

In some cases you can return to the client immediately while a background job completes processing. For example sending an email, kicking off a database job, and others represent fire-and-forget scenarios that can be handled with Spring's @Async support or by posting an event to a Spring Integration channel and then returning a confirmation id the client can use to query for the results.

Callable return type makes a controller method asynchronous. This is usually used in situations like long polling. Read this post by the same author for more information.

Also callable is an alternative for Runnable, in the sense, It can return results and throw checked exceptions.

Say you have a method

public String aMethod(){  } 

This can be made asynchronous by simply returning a Callable interface.

public Callable<String>  aMethod(){  } 
like image 66
shazinltc Avatar answered Sep 19 '22 22:09

shazinltc