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? 在异步通讯中,消息不会立刻到达接收方,而是被存放到一个容器中,当满足一定的条件之后,消息会被容器发送给接收方,这个容器即消息队列,而完成这个功能需要双方和容器以及其中的各个组件遵守统 ...
随机推荐
- [leetcode]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- vc到vs2015消息函数
afx_msg LRESULT OnMyIconNotify(WPARAM wParam,LPARAM lParam); vc6 可以是void vs2015不可以 ON_MESSAGE(MYWM_ ...
- python函数嵌套的实用技术
def fun(): def fun1(): print () fun1() fun() fun1()#总结老男孩python里面讲过,这个是函数的嵌套,很有用, #效果就是给函数一个自己的小函数.然 ...
- Linux下timer延时的使用
http://blog.csdn.net/hzpeterchen/article/details/8090385 因笔者工作在嵌入式平台上(非x386),下面给出的结论仅在arm平台上测试过. 1. ...
- 2018.09.26洛谷P3957 跳房子(二分+单调队列优化dp)
传送门 表示去年考普及组的时候失了智,现在看来并不是很难啊. 直接二分答案然后单调队列优化dp检验就行了. 注意入队和出队的条件. 代码: #include<bits/stdc++.h> ...
- 2018.07.25 bzoj2125: 最短路(圆方树+倍增)
传送门 人生的第一道仙人掌. 这道题求是仙人掌上的最短路. 先建出圆方树,然后用倍增跑最短路,当lca" role="presentation" style=" ...
- 批量 truncate 表
如果某个用户下所有表或指定表中所有的数据已确定不再需要,此时可以进行批量 truncate declare cursor cur_trunc is select table_name from use ...
- Java(Android)线程池[转]
介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...
- ScrollView嵌套listview ,滚动问题。设置listview不滚动
对于ListView内部的ListView,一般用来展示少量的列表数据. 内部的ListView的高度看起来是一个固定值且无法滑动,这个就比较蛋疼了.. 提供两种解决方案,方案的核心都是重新设置内部L ...
- ZOJ2418 Matrix 2017-04-18 21:05 73人阅读 评论(0) 收藏
Matrix Time Limit: 2 Seconds Memory Limit: 65536 KB Given an n*n matrix A, whose entries Ai,j a ...