How it works...

In Spring Boot 2.0, AsyncRabbitTemplate is already autoconfigured and is ready to be @Autowired just like RabbitTemplate, its counterpart for the synchronous data exchange. The main reason why we use this client API is to establish asynchronous requests to the request queue through an exchange router and receive some asynchronous replies from the reply queue. This class uses SimpleMessageListenerContainer, which acts like a broker that fetches the request messages from requestQueue() and manages the replies from replyQueue() asynchronously without polling them into only one queue, just like we saw in the previous recipe. The number of requests fetched is based on the prefetch configuration of the RabbitMQ server. All the send-receive transactions are still delegated internally by the RabbitTemplate class.

Since it is required that AsyncRabbitTemplate implements two queues, requestQueue() and replyQueue(), it can delegate send-receive message-based communication based on the similar methods sendAndReceive() and convertSendAndReceive(). These operators send the message requests with some payloads to requestQueue() for the broker to fetch it and forward it to replyQueue(). Based on the exchange's router key, all the requests are received and processed by replyQueue(), and it later forwards all the responses to the exchange and then to the broker. This returns a ListenableFuture<T>, a special type of a Future<T> that wraps and performs some threaded wait operations while waiting for the response body for 30 seconds at most, by default. It has ListenableFutureCallback<T> to filter and retrieve the response body during failed or successful communication. The default waiting time for the replies can be overridden through its setReceiveTimeout() method during auto wiring.

Comparing the two recipes, send-receive communication is faster because of the broker in the middle that returns non-blocking results and provides ListenableFutureCallback<T>, which is responsible for providing an immediate resilient solution during communication problems.

To build the simplest asynchronous AMQP communication, the next recipe will highlight the use of event-driven messaging, which scraps the use of router keys.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.115