何时使用 Spring @Async vs Callable 控制器(异步控制器,servlet 3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17167020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use Spring @Async vs Callable controller (async controller, servlet 3)
提问by devThoughts
I would like to know the general use case of using @Async and Servlet 3 asynchronous request implementation in Spring using Callable.
我想知道在 Spring 中使用 Callable 使用 @Async 和 Servlet 3 异步请求实现的一般用例。
As I understand, @Async is for making any method (specifically, any service method) execute asynchronously.
据我了解,@Async 用于使任何方法(特别是任何服务方法)异步执行。
@Async
void doSomething(String s) {
// this will be executed asynchronously
}
and any controller which returns Callable
以及任何返回 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?
我对何时使用什么感到困惑。将异步 servlet/控制器和 spring @Async 一起使用会产生什么效果?
采纳答案by shazinltc
Thispost 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-forgetscenarios that can be handled with Spring's
@Asyncsupport 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.
在某些情况下,您可以在后台作业完成处理时立即返回客户端。例如,发送电子邮件、启动数据库作业和其他代表 可以通过 Spring 的支持或通过将事件发布到 Spring 集成通道然后返回一个确认 id 客户端可以用来查询来处理的即发即忘的场景
@Async结果。
Callable return type makes a controller method asynchronous. This is usually used in situations like long polling. Read thispost 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.
callable 也是 Runnable 的替代品,从某种意义上说,它可以返回结果并抛出已检查的异常。
Say you have a method
说你有一个方法
public String aMethod(){
}
This can be made asynchronous by simply returning a Callable interface.
这可以通过简单地返回一个 Callable 接口来实现异步。
public Callable<String> aMethod(){
}
回答by poyraz
you can not improve single request performance with using Callable interface, it helps to take more request in some cases. If your response type would be void, you can use runnable instead of callable, so with using runnable you can improve single request response time.
使用 Callable 接口无法提高单个请求的性能,在某些情况下它有助于处理更多请求。如果您的响应类型为空,您可以使用 runnable 而不是 callable,因此使用 runnable 您可以改善单个请求响应时间。

