Harshana
Harshana

Reputation: 7647

@Async in a spring controller method

Spring Controller REST methods are by default Asynchronized.

Then what is the purpose of @Async annotation?

@RequestMapping(value = "/status/{id}", method = RequestMethod.GET)
@Async
public Future<Status> getStatus(@PathVariable("id") String id) {    
    return new AsyncResult<Status>(status);    
}

Also what is the role of Future interface and AsyncResult class?

Upvotes: 1

Views: 4633

Answers (1)

Lovababu Padala
Lovababu Padala

Reputation: 2477

@Async : Can be used for fire-and-forget scenarios, such as sending an email, kicking off a database job. Caller immediately get the response, while a background job completes processing.

what is the role of Future interface and AsyncResult class?

Future represents the result of an asynchronous computation.

AsyncResult is an implementation for Future, wraps the return type of asynchronous execution.

As per this blog, Callable is the appropriate return type for controller methods.

ref Async vs callable controllers

Upvotes: 3

Related Questions