RabbitMQ是一个受欢迎的消息代理,通常用于应用程序之间或者程序的不同组件之间通过消息来进行集成。本文简单介绍了如何使用 RabbitMQ,假定你已经配置好了rabbitmq服务器。
RabbitMQ是用Erlang,对于主要的编程语言都有驱动或者客户端。我们这里要用的是Java,所以先要获得Java客户端。。下面是Java客户端的maven依赖的配置。
2 | < groupId >com.rabbitmq</ groupId > |
3 | < artifactId >amqp-client</ artifactId > |
4 | < version >3.0.4</ version > |
像RabbitMQ这样的消息代理可用来模拟不同的场景,例如点对点的消息分发或者订阅/推送。我们的程序足够简单,有两个基本的组件,一个生产者用于产生消息,还有一个消费者用来使用产生的消息。
在这个例子里,生产者会产生大量的消息,每个消息带有一个序列号,另一个线程中的消费者会使用这些消息。
抽象类EndPoint:
我们首先写一个类,将产生产者和消费者统一为 EndPoint类型的队列。不管是生产者还是消费者, 连接队列的代码都是一样的,这样可以通用一些。
01 | package co.syntx.examples.rabbitmq; |
03 | import java.io.IOException; |
05 | import com.rabbitmq.client.Channel; |
06 | import com.rabbitmq.client.Connection; |
07 | import com.rabbitmq.client.ConnectionFactory; |
10 | * Represents a connection with a queue |
14 | public abstract class EndPoint{ |
16 | protected Channel channel; |
17 | protected Connection connection; |
18 | protected String endPointName; |
20 | public EndPoint(String endpointName) throws IOException{ |
21 | this .endPointName = endpointName; |
24 | ConnectionFactory factory = new ConnectionFactory(); |
27 | factory.setHost( "localhost" ); |
30 | connection = factory.newConnection(); |
33 | channel = connection.createChannel(); |
37 | channel.queueDeclare(endpointName, false , false , false , null ); |
42 | * 关闭channel和connection。并非必须,因为隐含是自动调用的。 |
45 | public void close() throws IOException{ |
47 | this .connection.close(); |
生产者:
生产者类的任务是向队列里写一条消息。我们使用Apache Commons Lang把可序列化的Java对象转换成 byte 数组。commons lang的maven依赖如下:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
01 | package co.syntx.examples.rabbitmq; |
03 | import java.io.IOException; |
04 | import java.io.Serializable; |
06 | import org.apache.commons.lang.SerializationUtils; |
10 | * The producer endpoint that writes to the queue. |
14 | public class Producer extends EndPoint{ |
16 | public Producer(String endPointName) throws IOException{ |
20 | public void sendMessage(Serializable object) throws IOException { |
21 | channel.basicPublish( "" ,endPointName, null , SerializationUtils.serialize(object)); |
消费者:
消费者可以以线程方式运行,对于不同的事件有不同的回调函数,其中最主要的是处理新消息到来的事件。
01 | package co.syntx.examples.rabbitmq; |
03 | import java.io.IOException; |
04 | import java.util.HashMap; |
07 | import org.apache.commons.lang.SerializationUtils; |
09 | import com.rabbitmq.client.AMQP.BasicProperties; |
10 | import com.rabbitmq.client.Consumer; |
11 | import com.rabbitmq.client.Envelope; |
12 | import com.rabbitmq.client.ShutdownSignalException; |
16 | * 读取队列的程序端,实现了Runnable接口。 |
20 | public class QueueConsumer extends EndPoint implements Runnable, Consumer{ |
22 | public QueueConsumer(String endPointName) throws IOException{ |
29 | channel.basicConsume(endPointName, true , this ); |
30 | } catch (IOException e) { |
36 | * Called when consumer is registered. |
38 | public void handleConsumeOk(String consumerTag) { |
39 | System.out.println( "Consumer " +consumerTag + " registered" ); |
43 | * Called when new message is available. |
45 | public void handleDelivery(String consumerTag, Envelope env, |
46 | BasicProperties props, byte [] body) throws IOException { |
47 | Map map = (HashMap)SerializationUtils.deserialize(body); |
48 | System.out.println( "Message Number " + map.get( "message number" ) + " received." ); |
52 | public void handleCancel(String consumerTag) {} |
53 | public void handleCancelOk(String consumerTag) {} |
54 | public void handleRecoverOk(String consumerTag) {} |
55 | public void handleShutdownSignal(String consumerTag, ShutdownSignalException arg1) {} |
Putting it together:
在下面的测试类中,先运行一个消费者线程,然后开始产生大量的消息,这些消息会被消费者取走。
01 | package co.syntx.examples.rabbitmq; |
03 | import java.io.IOException; |
04 | import java.sql.SQLException; |
05 | import java.util.HashMap; |
08 | public Main() throws Exception{ |
10 | QueueConsumer consumer = new QueueConsumer( "queue" ); |
11 | Thread consumerThread = new Thread(consumer); |
12 | consumerThread.start(); |
14 | Producer producer = new Producer( "queue" ); |
16 | for ( int i = 0 ; i < 100000 ; i++) { |
17 | HashMap message = new HashMap(); |
18 | message.put( "message number" , i); |
19 | producer.sendMessage(message); |
20 | System.out.println( "Message Number " + i + " sent." ); |
26 | * @throws SQLException |
29 | public static void main(String[] args) throws Exception{ |
转自:http://www.oschina.net/translate/getting-started-with-rabbitmq-in-java?cmp