25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16 ■ Messaging with Your <strong>Spring</strong> Application<br />

public void send(Document document){<br />

}<br />

}<br />

rabbitTemplate.convertAndSend(EXCHANGE,document.getType().getExtension(),document);<br />

Listing 16-1 shows the new producer. You can see that you are using a variable named Exchange with the value<br />

of mydocuments, and you are still using the RabbitTemplate, but now you are using a different method to publish the<br />

document. If you remember in Chapter 11, I never mentioned any exchange; you used a Message object and you<br />

converted the document into an XML format. But wait! What does that exchange do?<br />

RabbitMQ: Exchanges, Bindings, and Queues<br />

Let’s review the basic RabbitMQ messaging architecture. RabbitMQ is based on the AMQP (Advance Message<br />

Queue Protocol) and it offers routing capabilities through exchanges. You can see an exchange as an entry point for<br />

the message, and the exchange is in charge of delivering the message to the correct queue based on the routing key<br />

provided by the bindings. Figure 16-1 shows the basic architecture based on the direct exchange.<br />

Figure 16-1. The RabbitMQ Direct Exchange Routes the Messages Based on the Routing Key to the Correct Queue<br />

As you can see in Figure 16-1, a producer sends a message to the mydocuments exchange; this message<br />

has a routing key of either .pdf or .txt. Then, based on the binding, the exchange will put the message in the<br />

corresponding queue, either docs-pdf or docs-txt.<br />

RabbitMQ also has Topic, Headers, and Fanout exchanges, and each one offers different routing mechanisms. In<br />

Listing 16-1, you are using the convertAndSend method that accepts three parameters. The first parameter is the name of<br />

the exchange, in this case, mydocuments; the second parameter is the routing key, in this case the document’s extension<br />

(either .pdf, .txt, or .url); and the last parameter is the document itself. The convertAndSend method is one of several<br />

methods that allows you to publish using the org.springframework.amqp.rabbit.core.RabbitTemplate class.<br />

This class is part of the <strong>Spring</strong>-AQMP extension. You can take a look at the <strong>Spring</strong>-AMQP extension to explore more<br />

about the API at http://docs.spring.io/spring-amqp/docs/latest-ga/api/.<br />

As you can see in Figure 16-1, there is a queue per extension, so you need to create a consumer for every queue;<br />

in your case, it will be one consumer for the PDF, one for the text, and one for the web documents. In other words, a<br />

consumer will be listening from the docs-pdf queue, another consumer will be listening from the docs-txt queue, and<br />

the last consumer will be listening from the docs-web queue. Listings 16-2, 16-3, 16-4, and 16-5 show your consumers.<br />

218

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!