rabbitMQ的简单实例——amqp协议带数据回写机制
rabbitMQ是一种高性能的消息队列,支持或者说它实现了AMQP协议(advanced message queue protocol高级消息队列协议)。
下面简单讲一讲一个小例子。我们首先要部署好rabbitMQ,然后实现一个生产者—消费者,生产者向rabbit中发布一个消息,消费者去rabbit取这个消息,在正确收到这个消息后,消费者会通过返回队列回写通知生产者自己收到了消息。
windows下部署rabbit非常简单,先安装erlang运行时,然后安装rabbitMQ安装文件即可,都是exe的,很简单。然后找到rabbit的sbin目录里的bat即可启动rabbitMQ。
下面是producer—consumer代码:
package com.hzfi.rabbitmq; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer; public class Producer {
private final static String QUEUE_NAME = "myQueue"; //上送队列 public static void main(String[] args) throws IOException, TimeoutException{
String replyQueueName = null; //返回队列名 ConnectionFactory connFactory = null;
Connection conn = null;
Channel channel = null;
try{
connFactory = new ConnectionFactory();
connFactory.setHost("localhost");
conn = connFactory.newConnection();
channel = conn.createChannel();
//返回队列
replyQueueName = channel.queueDeclare().getQueue();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(replyQueueName, true, consumer); String corrId = java.util.UUID.randomUUID().toString(); //用来表示返回队列结果的id,唯一
BasicProperties props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build();
String msg = "linyang@hzfi.cn";
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicPublish("", QUEUE_NAME, props, msg.getBytes());
System.out.println("producer has published: \"" + msg + "\""); while(true){
Thread.sleep(1000);
Delivery delivery = consumer.nextDelivery();
System.out.println("from server reply:" + new String(delivery.getBody()));
}
}catch(IOException ioe){
ioe.printStackTrace();
}catch(TimeoutException toe){
toe.printStackTrace();
} catch (ShutdownSignalException e) {
e.printStackTrace();
} catch (ConsumerCancelledException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
if(channel!=null) channel.close();
if(conn!=null) conn.close();
}
}
}
package com.hzfi.rabbitmq; import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery; public class Consumer {
private final static String QUEUE_NAME = "myQueue";
public static void main(String[] args) throws IOException, TimeoutException{
ConnectionFactory connFactory = null;
Connection conn = null;
Channel channel = null;
try{
connFactory = new ConnectionFactory();
connFactory.setHost("localhost");
conn = connFactory.newConnection();
channel = conn.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("listening for event message..."); QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
while(true){
Thread.sleep(1000);
Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties reply_props = new BasicProperties.Builder().correlationId(props.getCorrelationId()).build();
String msg = new String(delivery.getBody(),"utf-8");
System.out.println("receive msg:" + msg);
String retMsg = "ok, give you reply:" + new String(msg.getBytes(),"utf-8");
System.out.println("Consumer中的返回队列名" + props.getReplyTo());
channel.basicPublish( "", props.getReplyTo(), reply_props, retMsg.getBytes());
}
}catch(IOException ioe){
ioe.printStackTrace();
}catch(TimeoutException toe){
toe.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
if(channel!=null) channel.close();
if(conn!=null) conn.close();
}
}
}
开启RabbitMQ的后台管理服务(是个web页面)
\sbin>rabbitmq-plugins enable rabbitmq_management
访问地址 http://localhost:15672/ id/psw: guest/guest
可以对队列,用户,权限等进行管理,例如,默认情况下密码是任意,如上代码所示,ConnectionFactory仅仅设置了主机名,并未设置用户名和密码。
我们可以新建或修改一个用户名和密码,如下图:

这样,我们上面的代码也要做相应的调整:
ConnectionFactory connFactory = new ConnectionFactory();
connFactory.setHost("localhost");
connFactory.setUsername("guest");
connFactory.setPassword("123");
rabbitMQ的简单实例——amqp协议带数据回写机制的更多相关文章
- RabbitMQ MQTT协议和AMQP协议
RabbitMQ MQTT协议和AMQP协议 1 序言... 1 1.1 RabbitMq结构... 1 1.2 RabbitMq消息接收... 4 1.3 Ex ...
- 消息中间件——RabbitMQ(三)理解RabbitMQ核心概念和AMQP协议!
前言 本章学习,我们可以了解到以下知识点: 互联网大厂为什么选择RabbitMQ? RabbiMQ的高性能之道是如何做到的? 什么是AMQP高级协议? AMQP核心概念是什么? RabbitMQ整体架 ...
- RabbitMQ核心概念和AMQP协议(二)
RabbitMQ是什么? RabbitMQ是一个开源的消息代理和队列服务器,用来通过普通协议,在完全不同的应用之间共享数据,RabbirMQ是使用Erlang语言来编写的,并且RabbitMQ是基于A ...
- linux下数据同步、回写机制分析
一.前言在linux2.6.32之前,linux下数据同步是基于pdflush线程机制来实现的,在linux2.6.32以上的版本,内核彻底删掉了pdflush机制,改为了基于per-bdi线程来实现 ...
- 一、Rabbitmq的简单介绍
以下只是本人从零学习过程的整理 部分内容参考地址:https://www.cnblogs.com/ysocean/p/9240877.html 1.RabbitMQ的概念 RabbitMQ是实现了高级 ...
- RabbitMQ介绍2 - AMQP协议
这一节介绍RabbitMQ的一些概念,当然也是AMQP协议的概念.官方网站也有详细解释,包括协议的命令: http://www.rabbitmq.com/tutorials/amqp-concepts ...
- RabbitMQ与AMQP协议
AMQP(Advanced Message Queuing Protocol, 高级消息队列协议)是一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计 ...
- Paxos协议超级详细解释+简单实例
转载自: https://blog.csdn.net/cnh294141800/article/details/53768464 Paxos协议超级详细解释+简单实例 Basic-Paxos算法 ...
- AMQP协议与RabbitMQ、MQ消息队列的应用场景
什么是AMQP? 在异步通讯中,消息不会立刻到达接收方,而是被存放到一个容器中,当满足一定的条件之后,消息会被容器发送给接收方,这个容器即消息队列,而完成这个功能需要双方和容器以及其中的各个组件遵守统 ...
随机推荐
- 无法打开登录所请求的数据库 "****"。登录失败
错误:无法打开登录所请求的数据库 "****".登录失败.用户 '****' 登录失败. sql2005连接时出现的错误 解决方法:权限不够,给登录名授权,赋予管理员角色,在登录名 ...
- Volley的使用
Volley加载图片到控件上 VolleyUtils.getLoader(getContext()).get(zixun.getPicurl(), ImageLoader.getImageListen ...
- Message: u'$ is not defined' ; Stacktrace
status.html <html> <head> <meta http-equiv="content-type" content="tex ...
- ADF backing Bean中常用的代码
// 刷新iterator bindings.refreshControl(); iterBind.executeQuery(); iterBind.refresh(DCIteratorBinding ...
- .NET中的Request
获得浏览器中的URL 例:http://121.41.30.93:8010/ch/spell.aspx?id=58 Request.Url.PathAndQuery:/ch/spell.aspx?id ...
- [NOI.AC]DELETE(LIS)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRMAAASJCAYAAABLtYu4AAAgAElEQVR4Xuzdf2xTd74n/PeqI/NsNB ...
- 2018.10.16 NOIP模拟 华莱士(并查集)
传送门 按照题意模拟维护最小的环套树森林就行了. 然而考试的时候naivenaivenaive瞎写了一个错误的贪心. 代码
- 2018.09.25 bzoj2286: [Sdoi2011]消耗战(虚树+树形dp)
传送门 又一道虚树入门题. 这个dp更简单啊. 直接记录每个点到1的距离,简单转移就行了. 代码: #include<bits/stdc++.h> #define N 250005 #de ...
- Django入门与实践-第14章:用户注册(完结)
http://127.0.0.1:8000/signup/ django-admin startapp accounts INSTALLED_APPS = [ 'accounts', ] # mypr ...
- k8s的port、targetport、nodeport之间的区别
先看举例: k8s集群中跑着一个tomcat服务,tomcat容器expose的端口为8080 apiVersion: v1 kind: Service metadata: name: tomcat- ...