引言

它是一种完全按照routing key(路由关键字)进行投递的:当消息中的routing key和队列中的binding key完全匹配时,才进行会将消息投递到该队列中

1.模型

2.创建生产者

package com.dwz.rabbitmq.exchange.direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; public class Producer {
private static final String EXCHANGE_NAME = "test_direct_exchange"; public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection(); Channel channel = connection.createChannel(); String routingKey_1 = "test.direct_1";
String routingKey_2 = "test.direct_2"; String msg_1 = "send rabbit direct message--1";
String msg_2 = "send rabbit direct message--2"; channel.basicPublish(EXCHANGE_NAME, routingKey_1, null, msg_1.getBytes());
channel.basicPublish(EXCHANGE_NAME, routingKey_2, null, msg_2.getBytes());
channel.close();
connection.close();
}
}

3.创建消费者1

package com.dwz.rabbitmq.exchange.direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties; public class Consumer {
private static final String QUEUE_NAME = "test_direct_queue_1";
private static final String EXCHANGE_NAME = "test_direct_exchange"; public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection(); Channel channel = connection.createChannel(); String routingKey = "test.direct_1"; channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, false, null);
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey); DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "utf-8");
System.out.println("rec direct_1 message:" + msg);
}
}; channel.basicConsume(QUEUE_NAME, true, consumer);
}
}

4.创建消费者2

package com.dwz.rabbitmq.exchange.direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties; public class Consumer2 {
private static final String QUEUE_NAME = "test_direct_queue_2";
private static final String EXCHANGE_NAME = "test_direct_exchange"; public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection(); Channel channel = connection.createChannel(); String routingKey = "test.direct_2"; channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, false, null);
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey); DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "utf-8");
System.out.println("rec direct_2 message:" + msg);
}
}; channel.basicConsume(QUEUE_NAME, true, consumer);
}
}

5.运行代码

通过路由键 routingKey  得到指定信息,success!

Routing(exchange--direct)的更多相关文章

  1. RabbitMQ --- Publish/Subscribe(发布/订阅)

    目录 RabbitMQ --- Hello Mr.Tua RabbitMQ --- Work Queues(工作队列) 前言 在第二篇文章中介绍了 Work Queues(工作队列),它适用于把一个消 ...

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

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

  3. RabbitMQ --- Routing(路由)

    目录 RabbitMQ --- Hello Mr.Tua RabbitMQ --- Work Queues(工作队列) RabbitMQ --- Publish/Subscribe(发布/订阅) 前言 ...

  4. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  5. Linux Direct 文件读写(文件DIO)

    有时候,读写文件并不想要使用系统缓存(page cache),此时 direct 文件读写就派上了用场,使用方法: (1)打开文件时,添加O_DIRECT参数: 需要定义_GNU_SOURCE,否则找 ...

  6. 使用 EWS(Exchange Web Service)协议读取邮件、发送邮件

    问题: 公司之前可以通过POP3协议收发邮件,因而在SoapUI中用JavaMail可以读取邮件,后来配置了Office 365,POP3协议端口不再开放,邮件全部读取失败,报login timeou ...

  7. 品友推广的投放原理 RTB:Real Time Bidding(实时竞价) DSP:Demand-Side Platform(需求方平台) 广告交易平台:AD Exchange

    总结: 1.实时竞价 0.1秒出价各个广告主出价,投放价高者: RTB(Real Time Bidding)实时竞价,是一种利用第三方技术在数以百万计的网站或移动端针对每一个用户展示行为进行评估以及出 ...

  8. Currency Exchange(最短路)

    poj—— 1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29851   Ac ...

  9. RabbitMQ学习第四记:路由模式(direct)

    1.什么是路由模式(direct) 路由模式是在使用交换机的同时,生产者指定路由发送数据,消费者绑定路由接受数据.与发布/订阅模式不同的是,发布/订阅模式只要是绑定了交换机的队列都会收到生产者向交换机 ...

随机推荐

  1. spring-cloud 学习二 服务发现

    注册中心服务发现的例子 添加module pom文件如下 <?xml version="1.0" encoding="UTF-8"?> <pr ...

  2. nginx之健康检查

    正常情况下,nginx做反向代理,如果后端节点服务器宕掉的话,nginx默认是不能把这台realserver踢出upstream负载集群的,所以还会有请求转发到后端的这台realserver上面,这样 ...

  3. iptables防火墙操作-查看、配置、重启、关闭

    查看iptables端口配置 iptables -L -n --line-number iptables端口配置(不开通3389无法远程连接,不开通icmp无法ping) iptables -A IN ...

  4. Qt5配置winpCap

    在网上查了很多资料,搞了差不多一天总算解决Qt5使用winPcap配置的问题了!记录一下 以便后续忘记 1.下载winpcap4.1.3,百度即可搜索到 2.下载winpCap开发者工具包http:/ ...

  5. HttpClient 源码阅读

    在项目中常用的HttpClient,与我们非常的亲密,为了能处理遇到的Http问题,我们应该去了解里面的机制和实现. 官方文档:http://hc.apache.org/httpcomponents- ...

  6. 记一次自启动的docker容器将宿主机的开机用户登录界面覆盖事件

    宿主机的系统为CentOS7_7.7.1908,默认为GUI启动,安装了宝塔面板,docker-ce为最新版. 在启动了一个centos7的容器(镜像为centos官方镜像)后,将该容器重启策略设置为 ...

  7. Linux 命令配置IP

    配置静态IP:ip addr add 192.168.18.18/24 dev eth0 启动网卡:ifup eth0/ifup ifcfg-eth0 添加默认网关路由:ip route add de ...

  8. manjaro 基本系统配置

    1.更新源 vim /etc/pacman.conf [archlinuxcn] SigLevel = Never Server = http://mirrors.tuna.tsinghua.edu. ...

  9. Git学习笔记(2)-Eclipse中Git插件使用

    目前我使用的Eclipse luna版本中已经集成了git插件,这里就不介绍如何安装Git插件了,不懂可以看其他的博客. 上篇笔记介绍了Git的基本指令,实际开发中我基本都使用eclipse插件进行代 ...

  10. TF启程

    我第一次开始接触到TensorFlow大概是去年五月份,大三下,如果一年多已过,我却还在写启程..这进度,实在汗颜.. 一个完整的tensorflow程序可以分为以下几部分: Inputs and P ...