rabbitmq 第二章
由上一篇 《rabbitmq 第一章》 rabbitmq的学习环境已经准备完毕了,接下来开启rabbitmq 学习学习之旅。
学习可参考官方的RabbitMQ Tutorials
路径: http://www.rabbitmq.com/getstarted.html
下面开始认识,学习第一种 "Hello World!"
学习准备环境
工具:intellij idea
环境:JDK 1.8
1.创建一个项目
PS: 点击 finish 按钮后,项目在创建的过程中加载对应的依赖可能需要一些时间,耐心等待即可。
项目创建完成之后,打开pom.xml 文件添加以下依赖。
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
依赖加载完成之后,在 Java目录同级下 添加 一个 resources 文件夹并且设置为 resources ROOT,目的是为了存放一个log4j配置文件,便于日志的输出。
具体操作如下:
接着,添加一个log4j 配置文件,具体内容如下:
log4j.rootLogger = DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
然后,先编写一个连接rabbitmq 的工具类,具体如下(片段)
/**
*
* @return 创建一个MQ连接
* @throws Exception 异常
*/
public static Connection getMQConnection() throws Exception{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setPort(5672);
connectionFactory.setUsername("lvoyee");
connectionFactory.setPassword("123456");
connectionFactory.setVirtualHost("/test");
return connectionFactory.newConnection();
}
Host : 这里写的是本地
Port :端口号
Username , Password ,VirtualHost 这里填写的是自己设置的用户名,密码和虚拟主机,具体的创建可以参考 《rabbitmq 第一章》。
接着编写 生产者代码,具体如下(片段)
// 队列名字
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
// 获取连接
Connection mqConnection = MQConnectionUtil.getMQConnection();
// 创建通道
Channel channel = mqConnection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
// 发送消息
channel.basicPublish("",QUEUE_NAME,null,"生产者发送的消息".getBytes());
// 关闭连接
channel.close();
mqConnection.close();
}
消费者代码,具体如下(片段)
// 队列名字
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
// 获取连接
Connection mqConnection = MQConnectionUtil.getMQConnection();
// 创建通道
Channel channel = mqConnection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 定义一个消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
// 接收消息
channel.basicConsume(QUEUE_NAME,true,consumer);
while(true){
// 获取消息
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" >>> Producer的消息: '" + message + "'");
}
}
到此代码编写结束了,以上就是官网教程中的第一种简单模式。
简单的发送与接收,没有特别的处理。
下面简单的说一下 生产者和消费者中 共同使用到的一个方法 queueDeclare(),对于这个方法中的各个参数是什么意思呢,最简单的方法就是查看源码。
/**
* Declare a queue
* @see com.rabbitmq.client.AMQP.Queue.Declare
* @see com.rabbitmq.client.AMQP.Queue.DeclareOk
* @param queue the name of the queue
* @param durable true if we are declaring a durable queue (the queue will survive a server restart)
* @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
* @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
* @param arguments other properties (construction arguments) for the queue
* @return a declaration-confirm method to indicate the queue was successfully declared
* @throws java.io.IOException if an error is encountered
*/
Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
Map<String, Object> arguments) throws IOException;
@param queue the name of the queue : 队列的名字
@param durable true if we are declaring a durable queue (the queue will survive a server restart) : 是否持久化,我们的队列模式是在内存中的,如果rabbitmq重启则会丢失,如果设置为 true,则会保存到 erlang自带的数据库中,重启则会重新读取
@param exclusive true if we are declaring an exclusive queue :是否排外,两个作用。一,第一个当我们的连接关闭后是否会自动删除队列。二,是否私有当天队列,如果私有 其他通道不可以访问当前队列
@param autoDelete true if we are declaring an autodelete queue : 是否自动删除
@param arguments other properties (construction arguments) for the queue : 队列中的其他参数