1
votes

I have three microservices - Service A, B and C. Service A should call B and C asynchronously, A should build the response based B and C responses.

I am using Rabbit MQ for async ipc.

Tried RabbitTemplate's convertSendAndRecieve with direct-replyTo option to consume, which makes the current processing thread wait/block on the async call to complete which makes it synchronous.

I wouldn't like to use the convertAndSend and let Service A listen on the reply queue and process based on correlation id as there would be thousands of responses in the reply queue and mapping the messages based on correlation id results in poor performance.

Creating separate queues for each session is not an option either due to its own caveats (getting acknowledgement from all clusters on new queue creation impacts the performance too)

Sorry if this problem has been solved before, I couldnt get much help on this on internet. Any help would be appreciated.

1

1 Answers

2
votes

There is AsyncRabbitTemplate for your purpose do not block the caller until the reply: https://docs.spring.io/spring-amqp/docs/2.0.0.RELEASE/reference/html/_reference.html#async-template:

Version 1.6 introduced the AsyncRabbitTemplate. This has similar sendAndReceive (and convertSendAndReceive) methods to those on the AmqpTemplate but instead of blocking, they return a ListenableFuture.

 RabbitConverterFuture<String> future = this.template.convertSendAndReceive("foo");
future.addCallback(new ListenableFutureCallback<String>() {

    @Override
    public void onSuccess(String result) {
        ...
    }

    @Override
    public void onFailure(Throwable ex) {
        ...
    }

});