rabbitmq
值得注意的是队列只会在它不存在的时候创建,多次声明并不会重复创建。信息的内容是字节数组,也就意味着你可以传递任何数据。
发布日期:2021-06-30 20:10:46
浏览次数:2
分类:技术文章
本文共 3762 字,大约阅读时间需要 12 分钟。
机制和ActiveMQ差不多,都是 起一个MQ服务,service发送消息,client 注册接收消息。
文档地址:http://www.rabbitmq.com/install-rpm.html
安装 erlang
yum install erlang
安装 rabbitmq-server
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc下载 安装 rabbitmq-server-3.6.10-1.noarch.rpm
地址:https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_6_10/rabbitmq-server-3.6.10-1.el7.noarch.rpm
yum install rabbitmq-server-3.6.10-1.noarch.rpm
启动
service rabbitmq-server start
如果报错:
Redirecting to /bin/systemctl restart rabbitmq-server.serviceJob for rabbitmq-server.service failed because the control process exited with error code. See "systemctl status rabbitmq-server.service" and "journalctl -xe" for details.
执行 journalctl -xe 查看详细信息,查看日志,看端口是否冲突;
他的端口有:
- 4369: , a peer discovery service used by RabbitMQ nodes and CLI tools
- 5672, 5671: used by AMQP 0-9-1 and 1.0 clients without and with TLS
- 25672: used by Erlang distribution for inter-node and CLI tools communication and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). See for details.
- 15672: clients and (only if the is enabled)
- 61613, 61614: without and with TLS (only if the is enabled)
- 1883, 8883: ( without and with TLS, if the is enabled
- 15674: STOMP-over-WebSockets clients (only if the is enabled)
- 15675: MQTT-over-WebSockets clients (only if the is enabled)
启动成功后编写代码:
maven:
com.rabbitmq amqp-client 4.1.1
发送端:Send.java 连接到RabbitMQ(此时服务需要启动),发送一条数据,然后退出。
[java]
- package com.zhy.rabbit._01;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class Send
- {
- //队列名称
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] argv) throws java.io.IOException
- {
- /**
- * 创建连接连接到MabbitMQ
- */
- ConnectionFactory factory = new ConnectionFactory();
- //设置MabbitMQ所在主机ip或者主机名
- factory.setHost("localhost");
- //创建一个连接
- Connection connection = factory.newConnection();
- //创建一个频道
- Channel channel = connection.createChannel();
- //指定一个队列
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- //发送的消息
- String message = "hello world!";
- //往队列中发出一条消息
- channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
- System.out.println(" [x] Sent '" + message + "'");
- //关闭频道和连接
- channel.close();
- connection.close();
- }
- }
接收端:Recv.java 不断等待服务器推送消息,然后在控制台输出。
[java]
- package com.zhy.rabbit._01;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.QueueingConsumer;
- public class Recv
- {
- //队列名称
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] argv) throws java.io.IOException,
- java.lang.InterruptedException
- {
- //打开连接和创建频道,与发送端一样
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- //声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- //创建队列消费者
- QueueingConsumer consumer = new QueueingConsumer(channel);
- //指定消费队列
- channel.basicConsume(QUEUE_NAME, true, consumer);
- while (true)
- {
- //nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- String message = new String(delivery.getBody());
- System.out.println(" [x] Received '" + message + "'");
- }
- }
- }
分别运行Send.java和Recv.java 顺序无所谓。前提RabbitMQ服务开启。
运行结果:
[x]Sent 'hello world!'
----------------------------------------
[*] Waiting for messages. To exitpress CTRL+C
[x] Received 'hello world!'
转载地址:https://liuhuiyao.blog.csdn.net/article/details/74132922 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年05月03日 11时16分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2021.6.1 Array补充
2019-04-30
【收藏】Markdown笔记
2019-04-30
离散数学-图论
2019-04-30
啊沙发沙发
2019-04-30
2021-07-02
2019-04-30
「收藏」HTML笔记-持续更新中
2019-04-30
数据结构期末复习------排序汇总
2019-04-30
数据结构期末复习------查找汇总
2019-04-30
Vue学习(10)————————编程式导航,地址栏的#号去掉,路由的嵌套
2019-04-30
SpringBoot内置Tomcat配置参数
2019-04-30
Nginx基本知识
2019-04-30
微服务公用实体类andLombok
2019-04-30
小问题
2019-04-30
Spring Security之旅————————————基本介绍安装(1)
2019-04-30
Spring IO Platform学习
2019-04-30
局部加权回归
2019-04-30
已知后序和中序求层序
2019-04-30
1051. Pop Sequence (25)
2019-04-30
ELK系列:新版ELK安装(windows与linux环境)
2019-04-30