java 为使用 Spring 3.0 的方法使用 @Async 时提供超时值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3785197/
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
Providing a timeout value when using @Async for a method using Spring 3.0
提问by Aayush Puri
I looked through the documentation but couldn't find if there is a way to specify a timeout for async operations spawned when using @Async annotated methods using Spring 3.0.
我查看了文档,但找不到是否有办法为使用 Spring 3.0 的 @Async 注释方法时产生的异步操作指定超时。
Is there a way to do that? I think this is pretty essential whenever making triggering an async computation.
有没有办法做到这一点?我认为这在触发异步计算时非常重要。
回答by skaffman
Timeouts are not provided by the @Async
annotation, since the timeout should be decided by the caller of the function, not the function itself.
@Async
注释不提供超时,因为超时应该由函数的调用者决定,而不是函数本身。
I'm assuming you're referring to the timeout on an @Async
-annotated method which returns a result. Such methods should return an instance of Future
, and the get()
method on Future
is used to specify the timeout.
我假设您指的是@Async
返回结果的-annotated 方法的超时。此类方法应返回 的实例Future
,而get()
方法 onFuture
用于指定超时。
e.g.
例如
@Async
public Future<String> doSomething() {
return new AsyncResult<String>("test");
}
and then
接着
Future<String> futureResult = obj.doSomething(); // spring makes this an async call
String result = futureResult.get(1, TimeUnit.SECOND);
回答by amra
In @Async source codeis no option for configuration.
在@Async 源代码中没有配置选项。