Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179 and 180: HTTP-DELETE Chapter 12 ■ Exposing
- Page 181 and 182: Chapter 13 Adding E-mail and Schedu
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185 and 186: Chapter 13 ■ Adding E-mail and Sc
- Page 187 and 188: Chapter 13 ■ Adding E-mail and Sc
- Page 189 and 190: Chapter 13 ■ Adding E-mail and Sc
- Page 191 and 192: Part III Spring Framework Advanced
- Page 193 and 194: Chapter 14 ■ Using Dynamic Langua
- Page 195 and 196: Chapter 14 ■ Using Dynamic Langua
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203 and 204: Chapter 15 ■ Spring Data Within Y
- Page 205 and 206: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211 and 212: Chapter 15 ■ Spring Data Within Y
- Page 213 and 214: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
- Page 231 and 232: Chapter 17 ■ Be Social and Go Mob
- Page 233 and 234: Chapter 17 ■ Be Social and Go Mob
- Page 235 and 236: Chapter 17 ■ Be Social and Go Mob
- Page 237 and 238: Chapter 17 ■ Be Social and Go Mob
- Page 239 and 240: Chapter 17 ■ Be Social and Go Mob
- Page 241 and 242: Chapter 17 ■ Be Social and Go Mob
- Page 243 and 244: Chapter 17 ■ Be Social and Go Mob
- Page 245 and 246: Chapter 17 ■ Be Social and Go Mob
- Page 247 and 248: Part IV Spring I/O Spring I/O is no
- Page 249 and 250: Chapter 18 ■ Spring and Groovy
- Page 251 and 252: Chapter 18 ■ Spring and Groovy Li
- Page 253 and 254: Chapter 18 ■ Spring and Groovy cl
- Page 255 and 256: Chapter 18 ■ Spring and Groovy If
- Page 257 and 258: Chapter 18 ■ Spring and Groovy Li
- Page 259 and 260: Chapter 19 ■ Spring Boot, Simplif
- Page 261 and 262: Chapter 19 ■ Spring Boot, Simplif
- Page 263 and 264: Chapter 19 ■ Spring Boot, Simplif
- Page 265 and 266: Chapter 19 ■ Spring Boot, Simplif
- Page 267 and 268: Chapter 19 ■ Spring Boot, Simplif
- Page 269 and 270: Chapter 19 ■ Spring Boot, Simplif
- Page 271 and 272: Chapter 19 ■ Spring Boot, Simplif
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