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? 在异步通讯中,消息不会立刻到达接收方,而是被存放到一个容器中,当满足一定的条件之后,消息会被容器发送给接收方,这个容器即消息队列,而完成这个功能需要双方和容器以及其中的各个组件遵守统 ...
随机推荐
- c#/vb调用c编写的标准dll
准备: 首先打开vc++ 6.0新建工程,选择Win32 Dynamic Link-Library,命名为stdLibrary 新建library.cpp文件,内容如下 #include <st ...
- Linux升级Ruby
一.简介 Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发.在 Ruby 社 ...
- bzr: ERROR: These branches have diverged. Use the missing command to see how.
这个错误是在提交之后执行bzr pull时出现的,先uncommit,再pull就可以了.
- PAT 1048 数字加密(20)(代码+思路)
1048 数字加密(20)(20 分) 本题要求实现一种数字加密方法.首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取 ...
- ObjC.instancetype
1. instancetype http://nshipster.com/instancetype/ 2. Objc的扩展 http://clang.llvm.org/docs/LanguageExt ...
- Codeforces 689B. Mike and Shortcuts SPFA/搜索
B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...
- POJ 1300.Door Man 欧拉通路
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2596 Accepted: 1046 Descript ...
- [原创] 分享一下Sencha 三种环境(开发环境、测试环境、生产环境)的优雅配置方案
背景介绍: 在一个AspNet MVC Web API的后端Web开发项目中,使用了Sencha6.5+作为前端表现技术. 在进行两种开发框架的物理文件整合的时候,笔者不想把他俩的物理文件都“揉”在一 ...
- const stirng* 类型的指针cp,当cout<<*cp<<endl:会提示没有与之匹配的“<<”运算符
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> usin ...
- 情境领导II
情境领导理论认为,领导者的行为要与被领导者的准备程度相适应,才能取得有效的领导效果,也就是说领导风格不是一成不变的,而要根据环境及员工的变化而改变. 三大技巧分别为诊断.弹性与约定领导型态.诊断是评估 ...