Rabbitmq编程实战
生产消费模型
RabbitMQ 整体上是一个生产者与消费者模型,主要负责接收、存储和转发消息。可以把消息传递的过程就如邮件投递何接受,投递人将一个包裹送到邮局,邮局会暂存并最终将邮件通过邮递员送到收件人的手上,RabbitMQ 就好比由邮局、邮箱和邮递员组成的一个系统。因此对于业务实现,通常转化为编写生产者客户端和消费者客户端。
代码实现
从实现上讲,需要编写生产者客户端和消费者客户端。生产者客户端负责发送消息,消费者客户端负责订阅消息。
引进依赖
本教程使用 IDEA 编写,创建 maven 项目,使用 maven 添加依赖,当前 amqp-client 的版本为 3.6.5。
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>
</dependencies>
生产者代码
Rabbitmq 所有的消息发送都必须通过操作 channel,使用 channel.basicPublish 发送消息。使用完需要关闭 channel 和 Connection,整个过程可以概括为:
- 通过指定服务的 ip、port、vhost 创建连接工厂 ConnectionFactory ;
- 从连接工厂中获取一个连接实例。创建通道 Channel ;
- 调用 channel.basicPublish 方法发送消息;
以下为 channel.basicPublish 方法介绍和生产者的代码实现
/**
* Publish a message.
*
* Invocations of <code>Channel#basicPublish</code> will eventually block if a
* <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
*
* @see com.rabbitmq.client.AMQP.Basic.Publish
* @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
* @param exchange 交换机
* @param routingKey 路由键
* @param mandatory 告诉RabbitMq如果消息不可路由,应该通过Basic.Return RPC命令将消息返回给发布者
* @param props 消息属性
* @param body 消息体
* @throws java.io.IOException if an error is encountered
*/
void basicPublish(String exchange, String routingKey, boolean mandatory, BasicProperties props, byte[] body)
throws IOException;
package com.skystep.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class SimpleProducer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.2");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exchange = "test_consumer_exchange";
String routingKey = "consumer.save";
String msg = "Hello RabbitMQ Consumer Message";
for (int i = 0; i < 5; i++) {
channel.basicPublish(exchange, routingKey, true, null, msg.getBytes());
}
channel.close();
connection.close();
}
}
消费者代码
Rabbitmq 所有的消息消费也是通过操作 channel,调用 channel.basicConsume 进行消息异步订阅。使用完需要关闭 channel 和 Connection,整个过程可以概括为:
- 通过指定服务的 ip、port、vhost 创建连接工厂 ConnectionFactory ;
- 从连接工厂中获取一个连接实例。创建通道 Channel ;
- 申请交换机、队列,并且将队列绑定到交换机上,确保交换机、队列已经存在;
- 调用 channel.basicConsume 方法进行消息处理;
以下为 channel.basicConsume 方法介绍和消费者的代码实现
/**
* Start a non-nolocal, non-exclusive consumer, with
* a server-generated consumerTag.
* @param queue 队列名称
* @param autoAck 是否自动回复
* acknowledged once delivered; false if the server should expect
* explicit acknowledgements
* @param callback 回调处理对象
* @return the consumerTag generated by the server
* @throws java.io.IOException if an error is encountered
* @see com.rabbitmq.client.AMQP.Basic.Consume
* @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
* @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)
*/
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
package com.skystep.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class SimpleConsumer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.2");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exchangeName = "test_consumer_exchange";
String routingKey = "consumer.#";
String queueName = "test_consumer_queue";
channel.exchangeDeclare(exchangeName, "topic", true, false, null);
channel.queueDeclare(queueName, true, false, false, null);
channel.queueBind(queueName, exchangeName, routingKey);
channel.basicConsume(queueName, true, new MyConsumer(channel));
}
}
package com.skystep.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
public class MyConsumer extends DefaultConsumer {
public MyConsumer(Channel channel) {
super(channel);
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.err.println("-----------consume message----------");
System.err.println("consumerTag: " + consumerTag);
System.err.println("envelope: " + envelope);
System.err.println("properties: " + properties);
System.err.println("body: " + new String(body));
}
}