1. “ * ”的使用:

生产者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.toov5.utils.MQConnectionUtils; //生产者 交换机类型 producerFanout类型
public class TopicProducer {
//交换机名称
private static final String EXCHANGE_NAME = "my_topic";
public static void main(String[] args) throws IOException, TimeoutException {
//建立MQ连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel();
//生产者绑定交换机
channel.exchangeDeclare(EXCHANGE_NAME, "topic"); //交换机名称 交换机类型
String routingKey="log.email"; //消息只会给邮件类型的
//创建对应的消息
String msString = "my_Routing_destination_msg"+routingKey;
//通过频道 发送消息
System.out.println("生产者投递消息:"+msString);
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msString.getBytes());
//关闭通道 和 连接
channel.close();
connection.close();
} }

消费者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException; 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;
import com.toov5.utils.MQConnectionUtils; //邮件消费者
public class ConsumerSMSTopic {
private static final String SMS_QUEUE ="sms_queue_topic";
//交换机名称
private static final String EXCHANGE_NAME = "my_topic";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("短信消费者启动");
//建立MQ连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel(); //消费者声明队列
channel.queueDeclare(SMS_QUEUE, false, false, false, null);
//消费者队列绑定 路由
channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, "log.*");
//消费者监听消息
DefaultConsumer defaultConsumer = 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("短信消费者获取生产者消息"+msg);
}
};
channel.basicConsume(SMS_QUEUE,true, defaultConsumer); //绑定队列 事件监听 }
}
package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException; 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;
import com.toov5.utils.MQConnectionUtils; //邮件消费者
public class ConsumerEmailTopic {
private static final String EMAIL_QUEUE ="email_queue_topic";
//交换机名称
private static final String EXCHANGE_NAME = "my_topic";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("邮件消费者启动");
//建立MQ连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel(); //消费者声明队列
channel.queueDeclare(EMAIL_QUEUE, false, false, false, null);
//消费者队列绑定 路由
channel.queueBind(EMAIL_QUEUE, EXCHANGE_NAME, "log.email");
//消费者监听消息
DefaultConsumer defaultConsumer = 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("邮件消费者获取生产者消息"+msg);
}
};
channel.basicConsume(EMAIL_QUEUE,true, defaultConsumer); //绑定队列 事件监听 }
}

可以看到两个消费者都可以接收到

2.换成 “#”

生产者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.toov5.utils.MQConnectionUtils; //生产者 交换机类型 producerFanout类型
public class TopicProducer {
//交换机名称
private static final String EXCHANGE_NAME = "my_topic";
public static void main(String[] args) throws IOException, TimeoutException {
//建立MQ连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel();
//生产者绑定交换机
channel.exchangeDeclare(EXCHANGE_NAME, "topic"); //交换机名称 交换机类型
String routingKey="log.email.sms"; //消息只会给邮件类型的
//创建对应的消息
String msString = "my_Routing_destination_msg"+routingKey;
//通过频道 发送消息
System.out.println("生产者投递消息:"+msString);
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msString.getBytes());
//关闭通道 和 连接
channel.close();
connection.close();
} }

消费者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException; 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;
import com.toov5.utils.MQConnectionUtils; //邮件消费者
public class ConsumerSMSTopic {
private static final String SMS_QUEUE ="sms_queue_topic";
//交换机名称
private static final String EXCHANGE_NAME = "my_topic";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("短信消费者启动");
//建立MQ连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel(); //消费者声明队列
channel.queueDeclare(SMS_QUEUE, false, false, false, null);
//消费者队列绑定 路由
channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, "log.#");
//消费者监听消息
DefaultConsumer defaultConsumer = 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("短信消费者获取生产者消息"+msg);
}
};
channel.basicConsume(SMS_QUEUE,true, defaultConsumer); //绑定队列 事件监听 }
}
package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException; 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;
import com.toov5.utils.MQConnectionUtils; //邮件消费者
public class ConsumerEmailTopic {
private static final String EMAIL_QUEUE ="email_queue_topic";
//交换机名称
private static final String EXCHANGE_NAME = "my_topic";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("邮件消费者启动");
//建立MQ连接
Connection connection = MQConnectionUtils.newConnection();
//创建通道
Channel channel = connection.createChannel(); //消费者声明队列
channel.queueDeclare(EMAIL_QUEUE, false, false, false, null);
//消费者队列绑定 路由
channel.queueBind(EMAIL_QUEUE, EXCHANGE_NAME, "log.email");
//消费者监听消息
DefaultConsumer defaultConsumer = 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("邮件消费者获取生产者消息"+msg);
}
};
channel.basicConsume(EMAIL_QUEUE,true, defaultConsumer); //绑定队列 事件监听 }
}

总结 “*” 匹配一个词   “#”匹配多个词

RabbitMQ之Exchange Topics模式的更多相关文章

  1. RabbitMQ之Exchange Direct模式

    场景: 生产者发送消息到交换机并指定一个路由key, 消费者队列绑定到交换机时要指定路由key(key匹配就能接受消息,key不匹配就不能接受消息) 例如:我们可以把路由key设置为insert ,那 ...

  2. RabbitMq 6种使用模式

    RabbitMQ的5种模式与实例 1.1 简单模式Hello World 功能:一个生产者P发送消息到队列Q,一个消费者C接收 生产者实现思路: 创建连接工厂ConnectionFactory,设置服 ...

  3. RabbitMQ的六种工作模式总结

    最近学习RabbitMQ的使用方式,记录下来,方便以后使用,也方便和大家共享,相互交流. RabbitMQ的六种工作模式: 1.Work queues2.Publish/subscribe3.Rout ...

  4. 面试官:RabbitMQ有哪些工作模式?

    哈喽!大家好,我是小奇,一位不靠谱的程序员 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 文章持续更新 一.前言 今天又.又.又来面试了,还是老规 ...

  5. RabbitMQ系列(三)RabbitMQ交换器Exchange介绍与实践

    RabbitMQ交换器Exchange介绍与实践 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器Exchang ...

  6. Rabbitmq交换器Exchange和消息队列

    通常我们谈到队列服务, 会有三个概念: 发消息者.队列.收消息者,RabbitMQ 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (Exchange). 这样发消息者 ...

  7. 【RabbitMQ学习之二】RabbitMQ四种交换机模式应用

    环境 win7 rabbitmq-server-3.7.17 Erlang 22.1 一.概念1.队列队列用于临时存储消息和转发消息.队列类型有两种,即时队列和延时队列. 即时队列:队列中的消息会被立 ...

  8. RabbitMQ交换器Exchange介绍与实践

    RabbitMQ交换器Exchange介绍与实践 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器Exchang ...

  9. 手把手一起入门 RabbitMQ 的六大使用模式(Java 客户端)

    原文地址:手把手一起入门 RabbitMQ 的六大使用模式(Java 客户端) 为什么使用 MQ? 在这里我就不多说了,无非就是削峰.解耦和异步.这里没有很多关于 MQ 的理论和概念,只想手把手带你一 ...

随机推荐

  1. 盘古分词demo,盘古分词怎么用

    1.下载PanGu.dll dll地址:http://download.csdn.net/detail/dhfekl/7493687 2.将PanGu.dll和词库引入到项目 最新词库地址:http: ...

  2. 怎样在fastboot 里面加入新的命令

    fastboot 是android 默认的一种debug 方法.它的优点是在进入linux kernel 之前就可以操作. 默认fastboot 支持的命令: usage: fastboot [ &l ...

  3. two sum, three sum和four sum问题

    1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash ...

  4. php编译安装与配置

    php编译安装与配置 =========================================== 官网:http://php.net/ 官网下载:http://php.net/downlo ...

  5. centos 7 安装 mail

    yum install -y sendmail 将下面内容粘贴到/etc/mail.rc中 set ssl-verify=ignore set nss-config-dir=/root/.certs ...

  6. org.hibernate.type.SerializationException: could not deserialize 反序列化失败

    1.查看实体类有没有实现Serializable接口 例:public class Student implements Serializable { ***** } 2.看表中的字段有没有在实体中进 ...

  7. 图像处理之基础---傅立叶c实现

    http://blog.csdn.net/lzhq28/article/details/7847047 http://blog.csdn.net/lishizelibin/article/detail ...

  8. Angular1.0 在Directive中调用Controller的方法

    Controller中定义了$scope.method = function(){} Directive中需要引入$scope http://stackoverflow.com/questions/2 ...

  9. django定时任务python调度框架APScheduler使用详解

    # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a ...

  10. iPhone缓存网络数据

    本文转载至 http://blog.csdn.net/wwang196988/article/details/7542918   在iPhone应用程序中,我们经常要用去网络下载一些文件,比如xml, ...