Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 16 ■ Messaging with Your Spring Application Listing 16-10 shows all the necessary information (like the location of the RabbitMQ server; the name of queues and their bindings with the exchange; the message converter and the consumers) that RabbitMQ needs in order to start sending, converting, and receiving messages. This configuration can be done using the RabbitMQ web console or it can be done programmatically, offering more control depending on certain business rules, but here you are going to use the Spring XML configuration, the declarative way. If you want to see how you can do all of this configuration programmatically, you can take a look at the RabbitMQ reference at http://docs.spring.io/spring-amqp/docs/ latest-ga/reference/html/. This configuration (see Listing 16-10) has several sections; let’s take a look at each one of them. Remember that you are using the namespace. The tag will create a caching connection to the RabbitMQ broker and you can pass some extra information like username, password, host, vhost, and port. The tag will create all the necessary exchanges, bindings, and queues, and it is necessary to pass the ID of the connection (rabbitConnectionFactory). The tag is an implementation of the Template pattern, and this will help to send and convert messages to the RabbitMQ broker. You need to specify what connection you are going to use to pass the ID of the connection-factory (rabbitConnectionFactory) and the converter that you are going to use (messageConverter); in this case, it’s the Marshaller defined in Listing 16-9. 225

Chapter 16 ■ Messaging with Your Spring Application and The tag defines a direct exchange, passing in the name (in your case mydocuments). By default, the exchange will be durable=true and autoDelete=false. The tags are a collection that contains one or more tag. The tag defines the bindings that are attached to that exchange—in other words, the rules that the exchange will follow after a message is sent to it. These tags accept the routing key and the name of the queue attributes. Even an exchange can be bound to another exchange! The tag creates a queue with the name provided; by default the queue has some properties that can be overridden, specifying their attributes. By default, your queues will be durable=true, autoDelete=false, and exclusive=false. and The listeners are your consumers and the tag will create a SimpleMessageListener instance that will point to several adapters that will receive the document. These adapters now are your own implementation of a consumer and they don’t depend on implementing the MessageListener as before in Chapter 11. Every adapter points to your classes defined previously; in this case it’s your three consumers. Test the Rabbit Now let’s see the unit test, as shown in Listing 16-11. Listing 16-11. MyDocumentsTest.java package com.apress.isf.spring.test; import static org.junit.Assert.assertNotNull; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.apress.isf.java.model.Document; import com.apress.isf.java.service.DocumentService; import com.apress.isf.spring.amqp.RabbitMQProducer; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:META-INF/spring/mydocuments-context.xml", "classpath:META-INF/spring/mydocuments-mongo-context.xml"}) public class MyDocumentsTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class); 226

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

and <br />

The tag defines a direct exchange, passing in the name (in your case mydocuments).<br />

By default, the exchange will be durable=true and autoDelete=false. The tags are a collection<br />

that contains one or more tag. The tag defines the bindings that are attached<br />

to that exchange—in other words, the rules that the exchange will follow after a message is sent to it. These tags accept<br />

the routing key and the name of the queue attributes. Even an exchange can be bound to another exchange!<br />

<br />

The tag creates a queue with the name provided; by default the queue has some properties that<br />

can be overridden, specifying their attributes. By default, your queues will be durable=true, autoDelete=false, and<br />

exclusive=false.<br />

and <br />

The listeners are your consumers and the tag will create a SimpleMessageListener<br />

instance that will point to several adapters that will receive the document. These adapters now are your own<br />

implementation of a consumer and they don’t depend on implementing the MessageListener as before in Chapter 11.<br />

Every adapter points to your classes defined previously; in this case it’s your three consumers.<br />

Test the Rabbit<br />

Now let’s see the unit test, as shown in Listing 16-11.<br />

Listing 16-11. MyDocumentsTest.java<br />

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

import static org.junit.Assert.assertNotNull;<br />

import org.junit.Test;<br />

import org.junit.runner.RunWith;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

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

import com.apress.isf.java.service.DocumentService;<br />

import com.apress.isf.spring.amqp.RabbitMQProducer;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration({"classpath:META-INF/spring/mydocuments-context.xml",<br />

"classpath:META-INF/spring/mydocuments-mongo-context.xml"})<br />

public class MyDocumentsTest {<br />

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

226

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

Saved successfully!

Ooh no, something went wrong!