25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11 ■ Integrating Your <strong>Spring</strong> Application with External Systems<br />

Listing 11-13 shows the tag that allows you to consume messages from RabbitMQ<br />

through the rabbitmqConsumer component. Listing 11-14 shows the RabbitMQ consumer.<br />

Listing 11-14. RabbitMQConsumer.java<br />

package com.apress.isf.spring.amqp;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.amqp.core.Message;<br />

import org.springframework.amqp.core.MessageListener;<br />

import org.springframework.stereotype.Component;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.java.utils.XmlUtils;<br />

@Component("rabbitmqConsumer")<br />

public class RabbitMQConsumer implements MessageListener {<br />

private static final Logger log = LoggerFactory.getLogger(RabbitMQConsumer.class);<br />

}<br />

@Override<br />

public void onMessage(Message message) {<br />

Document document = XmlUtils.fromXML(new String(message.getBody()),Document.class);<br />

log.debug("Document received: " + document);<br />

}<br />

In Listing 11-14, you are using the org.springframework.amqp.core.MessageListener class by implementing<br />

its method onMessage(Message message), so every time there is a new message on RabbitMQ and in the<br />

mydocumentsQueue, this method will receive the message. Take a look at this method; its parameter is a<br />

org.springframework.amqp.core.Message class, so it will be necessary to convert it back from the array of bytes[]<br />

to XML String and from XML String to a Document object. Now if you run Listing 11-12 again, you should get output<br />

saying Document received!<br />

■ ■Note The book’s companion source code contains all the dependencies (through gradle) used in this chapter.<br />

Summary<br />

In this chapter, you saw how you can interact and integrate your application between external systems. The <strong>Spring</strong><br />

<strong>Framework</strong> provides useful extensions to do these integrations: the JMS and the AMQP. But that’s not all; <strong>Spring</strong><br />

also provides the <strong>Spring</strong> Integration module that facilitates even more of the integration between systems, requiring<br />

minimal or even zero code! Apress has an excellent book called Pro <strong>Spring</strong> Integration written by the creators of this<br />

extension.<br />

In the next chapter, you are going to expose a REST API for your My Documents <strong>Spring</strong> application so that other<br />

developers can add documents to your application.<br />

167

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

Saved successfully!

Ooh no, something went wrong!