【发布时间】:2013-12-08 22:04:39
【问题描述】:
我正在使用 Spring Framework 堆栈,并且正在尝试实现以下功能。
我想在某个地图中存储@Async 方法返回的每个“Feature”对象。
到目前为止,我设法编写了一个 Aspect,在这样的方法上注册,但是,它没有获得异步的“功能”代理,但它已经获得了该方法的结果 - 方法完成时返回的值。所以方面@AfterReturning 注册不是为了返回代理,而是方法完成后的真实值。到目前为止,我发现如果我有另一个对象,它只执行我的 @Async 方法,并转发生成的功能,然后注册那个 Future,得到我想要的 Future(代理)。但是让另一个对象代理调用每个异步方法是一个麻烦的解决方案。
@Component
public class Sample {
@Async
@MyAnnotation
public Future<Integer> run() {
// long running operation
return new AsyncResult(10);
}
}
@Component
@Aspect
public class SampleAspect {
@AfterReturning(pointcut = "@annotation(myAnnotation )", returning = "retVal")
public Object process(Object retVal, String reqId, MyAnnotation myAnnotation ) throws Throwable {
// method execution has already finished here
// retval is instanceof AsyncResult, I want it to be a Future proxy
return retVal;
}
}
需要额外对象的解决方法
@Component
public class OtherSample {
@Autowired Sample sample;
@OtherAnnotation
public Futgure<Integer> run() {
return sample.run();
}
}
@Component
@Aspect
public class OtherAspect {
@AfterReturning(pointcut = "@annotation(otherAnnotation)", returning = "retVal")
public Object process(Object retVal, String reqId, OtherAnnotation otherAnnotation ) throws Throwable {
// method execution has NOT finished here
// retval is instanceof Future proxy
return retVal;
}
}
【问题讨论】:
-
请贴出代码。它会比描述更容易理解。
-
@SotiriosDelimanolis 我已经添加了代码示例 :)
标签: spring asynchronous aop aspectj future