Home > Net >  Is it possible to do asynchronous calls chaining in java with "AsyncRabbitTemplate.RabbitConver
Is it possible to do asynchronous calls chaining in java with "AsyncRabbitTemplate.RabbitConver

Time:09-22

I want to do chaining of async calls and get a single callback response when finish.

For example something like this:

 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
            }
        }
    }

In the end get result2.get(), if valid retrun ok response.

CodePudding user response:

Yes; it's possible - here's an example:

@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
  • Related