【问题标题】:Gather all Feature objects returned by Async methods收集 Async 方法返回的所有 Feature 对象
【发布时间】: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


【解决方案1】:

这不是对您问题的直接回答,而是一种解决方法的建议。

为@Async 注解的方法创建切面是一种非常特殊的情况。你不能只创建一个 org.springframework.core.task.AsyncTaskExecutor 的专用实现吗?

这是我之前项目中的simple configuration。我用它来处理(记录)任务抛出的异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    相关资源
    最近更新 更多