Spring 收集异步方法返回的所有要素对象

Spring 收集异步方法返回的所有要素对象,spring,asynchronous,aop,aspectj,future,Spring,Asynchronous,Aop,Aspectj,Future,我正在使用Spring框架堆栈,我正在尝试实现以下特性 我想将@Async方法返回的每个“Feature”对象存储在某个映射中。 到目前为止,我成功地编写了一个方面,在这样的方法上注册,但是,它没有得到异步“Feature”代理,但是它已经得到了这个方法的结果——方法完成时返回的值。所以aspect@AfterReturning寄存器不是用于返回代理,而是用于在方法完成后返回实际值。到目前为止,我发现如果我有另一个对象,它只执行我的@Async方法,并转发生成的特性,然后注册该未来,就会得到我想

我正在使用Spring框架堆栈,我正在尝试实现以下特性

我想将@Async方法返回的每个“Feature”对象存储在某个映射中。

到目前为止,我成功地编写了一个方面,在这样的方法上注册,但是,它没有得到异步“Feature”代理,但是它已经得到了这个方法的结果——方法完成时返回的值。所以aspect@AfterReturning寄存器不是用于返回代理,而是用于在方法完成后返回实际值。到目前为止,我发现如果我有另一个对象,它只执行我的@Async方法,并转发生成的特性,然后注册该未来,就会得到我想要的未来(代理)。但是,让另一个对象代理对每个异步方法的调用是一个麻烦的解决方案

@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;
    }
}
@组件
公共类样本{
@异步的
@MyAnnotation
公共未来运行(){
//长时间运行
返回新的异步结果(10);
}
}
@组成部分
@面貌
公共类样本{
@返回后(pointcut=“@annotation(myAnnotation)”,returning=“retVal”)
公共对象进程(对象检索、字符串请求ID、MyAnnotation MyAnnotation)抛出可丢弃{
//方法执行已在此处完成
//retval是AsyncResult的实例,我希望它是未来的代理
返回返回;
}
}
需要附加对象的变通方法

@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;
    }
}
@组件
公共类其他示例{
@自动接线样品;
@其他注释
公共未来运行(){
返回sample.run();
}
}
@组成部分
@面貌
公共类其他方面{
@返回后(pointcut=“@annotation(otherAnnotation)”,returning=“retVal”)
公共对象进程(对象检索、字符串请求ID、其他注释其他注释)抛出可丢弃的{
//方法执行尚未在此完成
//retval是未来代理的实例
返回返回;
}
}

这不是对你问题的直接回答,而是一个解决方案

为@Async注释方法创建方面是一种非常特殊的情况。您不能创建一个专门的org.springframework.core.task.AsyncTaskExecutor实现吗


这是我以前项目的一个例子。我用它来处理(记录)任务引发的异常。

请发布代码。这比描述更容易理解。@SotiriosDelimanolis我已经添加了代码示例:)