rabbitmq direct、fanout、topic 三种Exchange java 代码比较
Producer端
1、channel的创建
无论是才用什么样的Exchange,创建channel代码都是相同的,如下
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
2、Exchange的创建
2.1 direct
direct使用默认的Exchange,不需要声明,单需要指定消息发送到那个队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
2.2 fanout
channel.exchangeDeclare(EXCHANGE_NAME, "fanout")
2.3 topic如下
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
3、消息的发送
3.1 direct
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
3.2 fanout
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
3.3 topic
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
cusumer端
1、创建channel
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
2、消息前的准备
2.1 direct直接绑定队列进行消息的消费
chanel.queueDeclare(QUEUE_NAME, false, false, false, null);
2.2 fanout,需要先指定exchange类型为fanout
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
2.3 topic
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String queueName = channel.queueDeclare().getQueue();
for(String bindingKey : argv){
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
}
3、具体消费消息的代码是一样的
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
Envelope elp = delivery.getEnvelope();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
channel.basicAck(elp.getDeliveryTag(), false);
//channel.basicNack();
}
rabbitmq direct、fanout、topic 三种Exchange java 代码比较的更多相关文章
- 【RabbitMQ】4、三种Exchange模式——订阅、路由、通配符模式
前两篇博客介绍了两种队列模式,这篇博客介绍订阅.路由和通配符模式,之所以放在一起介绍,是因为这三种模式都是用了Exchange交换机,消息没有直接发送到队列,而是发送到了交换机,经过队列绑定交换机到达 ...
- RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较(转)
RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...
- 8、RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较 RabbitMQ中,除了Simple Queue和Work Queue之外的所有生产者提交的消息都由Exc ...
- 【转】RabbitMQ三种Exchange模式
[转]RabbitMQ三种Exchange模式 RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四 ...
- RabbitMQ三种Exchange
Direct Exchange – 处理路由键.需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配.这是一个完整的匹配.如果一个队列绑定到该交换机上要求路由键 “dog”,则只有被标记 ...
- RabbitMQ三种Exchange模式(fanout,direct,topic)的特性 -摘自网络
RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...
- RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...
- [转]RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...
- rabbitMQ 的三种Exchange
rabbitMQ 的Exchange有3种路由方式: direct.fanout.topic ,以下为详细说明 1. Direct Exchange 处理路由键.需要将一个队列绑定到交换机上,要求 ...
随机推荐
- mybatis之入门
一.mybatis介绍 是apache旗下的一个开源的顶级ORM框架(做dao层的操作) 开始叫ibatis在2010年经过升级后发布到google code上就改名为mybatis 定位:1.是一个 ...
- memcached-session-manager 教程实现session共享
1简单介绍 1.1决定用什么序列化策略. 1.2配置tomcat 1.2.1加入 memcached-session-manager jar 包到tomcat中. ...
- POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)
http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...
- hdu2159FATE(二维背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2159 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在 ...
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- oracle 之创建用户,表空间,授权,修改用户密码
1.创建表空间 create tablespace ilinkcargoagent logging datafile 'D:\app\Administrator\oradata\ilinkcargoa ...
- R语言学习笔记:基础知识
1.数据分析金字塔 2.[文件]-[改变工作目录] 3.[程序包]-[设定CRAN镜像] [程序包]-[安装程序包] 4.向量 c() 例:x=c(2,5,8,3,5,9) 例:x=c(1:100) ...
- android studio 3.0 安装配置
1. 安装jdk1.8 2.复制android sdk 设置代理 mirrors.neusoft.edu.cn 端口 80 http代理 更新sdk 安装 android support ...
- kendo column chart
目录 1.用js操作chart, 2.tooltip template鼠标悬浮显示内容, 3.双坐标轴,axisCrossingValues: [0, 30],3指的是跨越横坐标轴标签项数,显示在右 ...
- FastJson(阿里巴巴)基础
一.所需jar包: fastjson-x.x.xx.jar(本例使用fastjson-1.1.36.jar). 二.解析转化: 1.json字符串 < ------ > js trin ...