rabbitMQ_helloworld(一)
在下图中,“P”是我们的生产者,“C”是我们的消费者。中间的框是队列 - RabbitMQ代表消费者的消息缓冲区。

本例使用maven构建项目,在pom.xml中添加一下依赖
- <dependencies>
- <dependency>
- <groupId>com.rabbitmq</groupId>
- <artifactId>amqp-client</artifactId>
- <version>4.1.</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-simple</artifactId>
- <version>1.7.</version>
- </dependency>
- </dependencies>
接下来通过生产者叫rabbitMQ服务器发送一个消息,这个消息被缓存在了rabbitMQ的队列中,生产者相当于发件人,rabbitMQ相当于申通快递,消费者相当于接收人

定义一个发件人Send.java
- package com.rabbitMQ;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- /**
- * 发送消息后,如果服务器的队列中没有可发现的消费者,那么就会放在队列中等待消费者
- * 可以使用rabbitmqctl.bat list_queues命令查看某个队列的消息个数
- * @author may
- *
- */
- public class Send {
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] args) throws Exception {
- //创建连接工厂
- ConnectionFactory factory = new ConnectionFactory();
- //设置连接rabbitMQ服务器的ip
- factory.setHost("localhost");
- // factory.setPort(5672);
- //创建一个连接到服务器的链接
- Connection connection = factory.newConnection();
- //创建连接通道
- Channel channel = connection.createChannel();
- /**
- * 定义一个队列
- * queueDeclare(String queue,//队列的名字
- * boolean durable, //定义一个耐用队列,即持久化,如果RabbitMQ服务挂机,重启后还能恢复这个队列。
- * boolean exclusive, //排他队列,只能在当前链接中可用,如果这个连接关闭,那么也就无效了。
- * boolean autoDelete,//在连接断开后自动删除队列。
- * Map<String, Object> arguments)
- */
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- // channel.queueDeclareNoWait(queue, durable, exclusive, autoDelete, arguments);
- String message = "Hello World!";
- /**
- * Parameters:
- * exchange: the exchange to publish the message to 转发器的名字
- * routingKey: the routing key 路由key,这里就是队列的名字,表示要发送到这个队列上
- * props: other properties for the message - routing headers etc//这个信息的属性
- * body: the message body 要发送的信息
- */
- channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
- System.out.println(" [x] Sent '" + message + "'");
- channel.close();
- connection.close();
- }
- }
接收消息,消费者从队列中获取消息

- package com.rabbitMQ;
- import java.io.IOException;
- import com.rabbitmq.client.AMQP;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.Consumer;
- import com.rabbitmq.client.DefaultConsumer;
- import com.rabbitmq.client.Envelope;
- public class Recv {
- //队列名要和发送者定义的队列一样的名字,它会告诉rabbitMQ它要再哪个队列中获取消息
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] argv) throws Exception {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- /**
- * queue the name of the queue
- durable true if we are declaring a durable queue (the queue will survive a server restart)
- exclusive true if we are declaring an exclusive queue (restricted to this connection)
- autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
- arguments other properties (construction arguments) for the queue
- */
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- //定义一个消费者
- Consumer consumer = new DefaultConsumer(channel) {
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
- byte[] body) throws IOException {
- String message = new String(body, "UTF-8");
- System.out.println(" [x] Received '" + message + "'");
- }
- };
- //异步
- /**
- * queue the name of the queue 队列名
- * 如果autoAck设置为true,这个Consumer在收到消息之后会马上返回ack。服务器将立即删除在内存中排队的消息
- * false 在消息的任务处理完之后再手动ack,如果正在处理的时候,消费者发生异常,就不能返回ack,那么就不会删除这个消息,等待发现其他消费者发送给他们
- autoAck true if the server should consider messages acknowledged once delivered; false if the server should expect explicit acknowledgements
- callback an interface to the consumer object
- */
- channel.basicConsume(QUEUE_NAME, true, consumer);
- //rabbitmqctl.bat list_queues 命令可以列出当前有多少个队列
- }
- }
运行Send,向rabbitMQ发送了一个消息,我们可以通过rabbitMQ的一个工具查看当前的队列中有多少个消息在排队。
hello是队列的名字,1表示当前的队列中存在一个消息等待着被消费者处理
这个时候,启动消费者去取快递,启动后输出一下内容,成功打印出了Hello World
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'
再次查看rabbitMQ中hello队列中的消息个数
消息个数为零。
rabbitMQ_helloworld(一)的更多相关文章
随机推荐
- JavaScript 一元正号运算符
本文适合JavaScript初学者. 一元正号介绍 一元正号运算符(+)位于其操作数前面,计算其操作数的数值,如果操作数不是一个数值,会尝试将其转换成一个数值. 尽管一元负号也能转换非数值类型,但是一 ...
- node.js中模块,require
在php,C++中都有命名空间的概念,命名空间主要是用来解决引入文件存在函数,类,变量重名的问题,在node.js中,没有命名空间这么复杂的概念,在node中,有模块的概念,也就是将功能性的代码都放在 ...
- 设计模式之策略模式和状态模式(strategy pattern & state pattern)
本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...
- Mysql事务隔离级别和锁机制
一.Spring支持四种事务隔离级别: 1.ISOLATION_READ_UNCOMMITTED(读未提交):这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据. 2.ISOLAT ...
- Java多线程(三):volatile
volatile volatile是一种轻量同步机制.请看例子 MyThread25类 public class MyThread25 extends Thread{ private boolean ...
- 【运维实战】利用tar -g 实现简单全量备份和增量备份(带演示)
备份产生 全量备份指完全备份,增量备份指针对上次至今的修改进行备份.linux提供tar -g可实现备份功能. 第一次运行 tar -g 备份存放目录/snapshot -czvf 备份存放目录/备 ...
- BZOJ 2957:楼房重建(分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2957 题意:…… 思路:对于每一个块,维护一个单调递增的斜率(因为小于前面的斜率的话是肯定看不见的) ...
- CSDN,CNBLOGS博客文章一键转载插件(转载测试)
插件地址: https://greasyfork.org/zh-CN/scripts/381053-csdn%E5%8D%9A%E5%AE%A2%E6%96%87%E7%AB%A0%E8%BD%AC% ...
- Spring MVC中使用FastJson自定义注解
最近在做.net转译成Java.其中遇到一个很蛋疼的问题.以前.net属性名都是首字母大写.造成返回给客户端的JSON字符串属性名称都是首字母大写.为了和前端对接我们以前都是如下图所示做法 publi ...
- WPF 入门笔记之控件内容控件
一.控件类 在WPF中和用户交互的元素,或者说.能够接受焦点,并且接收键盘鼠标输入的元素所有的控件都继承于Control类. 1. 常用属性: 1.1 Foreground:前景画刷/前景色(文本颜色 ...