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

前端 未结 2 798
醉梦人生
醉梦人生 2020-12-05 00:40

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

2条回答
  •  暖寄归人
    2020-12-05 01:22

    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  aMethod(){
    
    }
    

提交回复
热议问题