总之,没有办法配置Spring来做到这一点。
@Async
注释由AsyncExecutionInterceptor
处理,它将工作委托给AsyncTaskExecutor
。理论上,您可以编写自己的AsyncTaskExecutor
实现,但即使如此,也无法使用注释将期望的等待时间传递给执行程序。即使如此,我不清楚调用者的界面是什么样子,因为他们仍然会得到一个Future
对象。您可能还需要继承Future
对象的子类。基本上,当你完成的时候,你将会从头开始重新编写整个特性。
你总是可以包返回Future
对象在自己WaitingFuture
代理提供一个备用的get实现尽管后来连你有没有对被叫方指定的等待值的方式:
WaitingFuture<ModelObject> future = new WaitingFuture<ModelObject>(service.doSomething());
ModelObject result = future.get(3000); //Instead of throwing a timeout, this impl could just return null if 3 seconds pass with no answer
if(result == null) {
//Path A
} else {
//Path B
}
或者如果你不想写自己的课,那就赶上TimeoutException
。
Future<ModelObject> future = doSomething();
try {
ModelObject result = future.get(3000,TimeUnit.MILLISECONDS);
//Path B
} catch (TimeoutException ex) {
//Path A
}
可能重复(HTTP:/ /stackoverflow.com/questions/3785197/providing-a-timeout-value-when-using-async-for-a-method-using-spring-3-0) – 2013-04-07 08:04:08
没有。这不是重复的问题,因为如何限制整个异步任务执行的时间。 – 2013-04-07 08:35:48