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 16<br />

Messaging with Your <strong>Spring</strong><br />

Application<br />

In Chapter 11, you integrated your <strong>Spring</strong> application with JMS and RabbitMQ servers. In this chapter, I’ll talk more<br />

about RabbitMQ and the <strong>Spring</strong> AMQP (Advance Message Queue Protocol) module. Imagine for a moment that you<br />

are going to receive a lot of new documents and at some point you will want to classify them by extension.<br />

An easy way to do this classification by extension is to create a RabbitMQ queue. Once the message arrives to the<br />

consumer, they get the Type and the Extension, and then based on these two properties, do some processing. Well,<br />

that sounds like the right way to do it. But you are going to want to process as many documents as you can, and if you<br />

do, this solution might become overwhelming.<br />

So how are you going to solve this problem? The good news is that you can take advantage of a nice set of features<br />

from RabbitMQ that differs from other brokers. RabbitMQ provides a routing mechanism for free. I’ll explain the<br />

RabbitMQ’s routing features in the following paragraphs, but first you need to do some coding.<br />

Using RabbitMQ<br />

Let’s start by creating the producer that will emulate all the documents that will be sent to you. See Listing 16-1 for the<br />

RabbitMQProducer class.<br />

Listing 16-1. RabbitMQProducer.java<br />

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

import org.springframework.amqp.rabbit.core.RabbitTemplate;<br />

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

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

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

@Component("rabbitmqProducer")<br />

public class RabbitMQProducer {<br />

private static final String EXCHANGE = "mydocuments";<br />

@Autowired<br />

private RabbitTemplate rabbitTemplate;<br />

217

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

Saved successfully!

Ooh no, something went wrong!