是否可以在 java 中使用 "AsyncRabbitTemplate.RabbitConverterFuture" 进行异步调用链接

Is it possible to do asynchronous calls chaining in java with "AsyncRabbitTemplate.RabbitConverterFuture"

我想链接异步调用并在完成时获得单个回调响应。

例如这样的事情:

 public invokeAsyncAPI1 () {

        AsyncRabbitTemplate.RabbitConverterFuture<Response1> futureResponse1 = asyncAPI1();

        futureResponse1.addCallback {

            successCallBack (Response1 result1) {
                if(result != OK) {
                    return immediately with false
                } else {
                    invokeAsyncAPI2(result1);
                }
            }

            failureCallBack () {
                return immediately with false
            }
        }
    }

    public invokeAsyncAPI2 (result1) {

        AsyncRabbitTemplate.RabbitConverterFuture<Response2> futureResponse2 = asyncAPI2();

        futureResponse2.addCallback {

            successCallBack (Response2 result2) {
                if(result != OK) {
                    return immediately with false
                } else {
                    return true response
                }
            }

            failureCallBack () {
                return immediately with false
            }
        }
    }

最后得到result2.get(),如果有效则retrun ok response。

是;这是可能的 - 这是一个例子:

@SpringBootApplication
public class So69213655Application {

    public static void main(String[] args) {
        SpringApplication.run(So69213655Application.class, args);
    }

    @RabbitListener(queues = "service1")
    @SendTo
    public String service1(String in) {
        System.out.println("service1: " + in);
        return in.toUpperCase();
    }

    @RabbitListener(queues = "service2")
    @SendTo
    public String service2(String in) {
        System.out.println("service2: " + in);
        return in.toLowerCase();
    }

    @Bean
    AsyncRabbitTemplate template(RabbitTemplate template) {
        AsyncRabbitTemplate async = new AsyncRabbitTemplate(template);
        return async;
    }

    @Bean
    ApplicationRunner runner(ChainedSender sender) {
        return args -> {
            System.out.println("And the answer is: " + sender.send("TesT").get(10, TimeUnit.SECONDS));
        };
    }

}

@Component
class ChainedSender {

    private final AsyncRabbitTemplate template;

    ChainedSender(AsyncRabbitTemplate template) {
        this.template = template;
    }

    Future<String> send(String out) {
        SettableListenableFuture<String> future = new SettableListenableFuture<>();
        RabbitConverterFuture<Object> future1 = this.template.convertSendAndReceive("service1", out);
        future1.addCallback(result1 -> {
            RabbitConverterFuture<Object> future2 = this.template.convertSendAndReceive("service2", result1);
            future2.addCallback(result2 -> {
                future.set((String) result2);
            }, ex -> {
                future.setException(ex);
            });
        }, (ex) -> {
            future.setException(ex);
        });
        return future;
    }

}
service1: TesT
service2: TEST
And the answer is: test