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 11 ■ Integrating Your <strong>Spring</strong> Application with External Systems<br />

@Component("rabbitmqProducer")<br />

public class RabbitMQProducer {<br />

@Autowired<br />

private RabbitTemplate rabbitTemplate;<br />

}<br />

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

MessageProperties messageProperties = new MessageProperties();<br />

messageProperties.setContentType("text/xml");<br />

byte[] body = XmlUtils.toXML(document).getBytes();<br />

Message message = new Message(body, messageProperties);<br />

rabbitTemplate.send(message);<br />

}<br />

Listing 11-10 shows the producer; here you are using the org.springframework.amqp.rabbit.core.<br />

RabbitTemplate class (provided by the <strong>Spring</strong> Rabbit module) that allows you to send messages through the AMQP<br />

to the RabbitMQ broker. Also, you are creating an instance of the org.springframework.amqp.core.Message class,<br />

a Message class that takes two parameters. The first parameter is the body of the message; in this case the Document<br />

object is converted into an array of bytes by using the XmlUtil class (see Listing 11-3). The second parameter will be<br />

a MessageProperties instance that only indicates that the message will be in XML. Then you send the message by<br />

calling the rabbitTemplate.send method.<br />

Listing 11-11 shows the XML configuration and the use of the namespace to declare some of<br />

the elements needed to connect to the RabbitMQ broker. This is necessary for the <strong>Spring</strong> container to know about the<br />

RabbitMQ broker.<br />

Listing 11-11. mydocuments-context.xml<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!