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 代码比较的更多相关文章

  1. 【RabbitMQ】4、三种Exchange模式——订阅、路由、通配符模式

    前两篇博客介绍了两种队列模式,这篇博客介绍订阅.路由和通配符模式,之所以放在一起介绍,是因为这三种模式都是用了Exchange交换机,消息没有直接发送到队列,而是发送到了交换机,经过队列绑定交换机到达 ...

  2. RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较(转)

    RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...

  3. 8、RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较

    RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较 RabbitMQ中,除了Simple Queue和Work Queue之外的所有生产者提交的消息都由Exc ...

  4. 【转】RabbitMQ三种Exchange模式

    [转]RabbitMQ三种Exchange模式 RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四 ...

  5. RabbitMQ三种Exchange

    Direct Exchange – 处理路由键.需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配.这是一个完整的匹配.如果一个队列绑定到该交换机上要求路由键 “dog”,则只有被标记 ...

  6. RabbitMQ三种Exchange模式(fanout,direct,topic)的特性 -摘自网络

    RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...

  7. RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较

    RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...

  8. [转]RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较

    RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...

  9. rabbitMQ 的三种Exchange

    rabbitMQ 的Exchange有3种路由方式:  direct.fanout.topic ,以下为详细说明 1.  Direct Exchange 处理路由键.需要将一个队列绑定到交换机上,要求 ...

随机推荐

  1. Arctic Network---poj2349 最小生成树

    http://poj.org/problem?id=2349 题意:有n个点给出坐标,点和点之间可以用无线电或者卫星通信,每个点都有无线电收发器可进行无线电通信,但是只有m个点有卫星通信功能.卫星通信 ...

  2. 【python基础】python程序打包成.exe运行时会弹出黑框

    用python调用.bat或者.exe文件时,一般调用 方式如下: os.system("C:\Windows\System32\osk.exe") 对吧,这样就会因为调用了系统s ...

  3. 【虫师】【selenium】参数化

    # 1 #coding=utf-8 from selenium import webdriver import os,time source = open("F:\\test\\info.t ...

  4. PAT 1049 Counting Ones[dp][难]

    1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...

  5. 【Cocos2dx 3.3 Lua】定时器事件

    Cocos2dx 3.x Lua 中使用定时器有两种方式: (1)self:scheduleUpdateWithPriorityLua(update, priority) > 参数一:刷新函数 ...

  6. Twitter OA prepare: Equilibrium index of an array

    Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...

  7. SP Flash Tool New Version v5.1352.01

    Friends, Sp Tool updated to new version with whole new revamped interface New SP Flash Tool 3.1352.0 ...

  8. UVM中的driver组件

    一般UVM环境中的Driver组件,派生自uvm_driver. uvm_dirver派生自uvm_component. class  uvm_driver #(type REQ = uvm_sequ ...

  9. python爬虫对于gb2312

    对于刚刚接触python爬虫的人,常常会碰到一个比较烦的问题, 如果网页是GB2312编码格式,我们直接decode(’GB2312‘)一般python都会报错: GB2312不能编码该页面. 这就比 ...

  10. nginx之gzlib压缩,expires缓存

    gzip压缩网页内容的压缩编码与传输速度优化我们观察news.163.com的头信息请求:Accept-Encoding:gzip,deflate,sdch响应:Content-Encoding:gz ...