预付网站建设费用怎么做分录,怎样建立小程序,社交类电商平台,济南做网站维护的公司这篇文章#xff0c;主要介绍消息队列RabbitMQ七种模式之Topics主题模式。 目录
一、消息队列
1.1、主题模式#xff08;Topics#xff09;
1.2、案例代码
#xff08;1#xff09;引入依赖
#xff08;2#xff09;编写生产者
#xff08;3#xff09;编写消费… 这篇文章主要介绍消息队列RabbitMQ七种模式之Topics主题模式。 目录
一、消息队列
1.1、主题模式Topics
1.2、案例代码
1引入依赖
2编写生产者
3编写消费者 一、消息队列
1.1、主题模式Topics
前一篇文章介绍了Routing路由模式它是根据消息的唯一标识routing key路由键将其分发到不同的Queue队列里面这篇文章介绍的Topics主题模式也是需要依赖与routing key路由键来进行分发消息但是不同的是Topics主题可以采用通配符的方式来指定routing key。
Topics主题模式下它要求【routing key路由键】必须采用【多个单词】来表示每一个单词采用点好【.】分割例如【xxx.yyy.zzz】这就是三个单词分别是xxx、yyy、zzz路由键的最长长度是255bytes。
Topics主题模式支持两种通配符
星号【*】通配符只能够匹配1个单词Topics主题模式下按照点好【.】号来判断单词。井号【#】通配符匹配0个或者多个单词。
Topics主题模式大致如下所示 主题模式下每一个Queue队列能够接收到的消息类型就非常多了只要消息满足routing key的格式就会被分发到这个Queue队列里面。Topics主题模式和Routing路由模式有什么区别呢
Routing路由模式需要指定具体的routing key。Topics主题模式可以采用通配符来指定routing key如果Topics主题模式中没有使用通配符那么这个时候就相当于Routing路由模式所以Topics主题模式比Routing路由模式更加的灵活。
1.2、案例代码
1引入依赖
!-- 引入 RabbitMQ 依赖 --
dependencygroupIdcom.rabbitmq/groupIdartifactIdamqp-client/artifactIdversion5.16.0/version
/dependency
2编写生产者
生产者发送消息时候设置routing key是采用【spring.xxx】格式的。
package com.rabbitmq.demo.topics;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;/*** version 1.0.0* Date: 2023/2/25 16:23* Copyright (C) ZhuYouBin* Description: 消息生产者*/
public class Producer {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost(127.0.0.1); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection null; // 连接Channel channel null; // 通道try {connection factory.newConnection();// 4、获取通道channel connection.createChannel();// 5、声明 Exchange如果不存在则会创建String exchangeName exchange_topics_2023;channel.exchangeDeclare(exchangeName, topic);// 6、发送消息for (int i 0; i 10; i) {// 路由键唯一标识String routingKey spring.error;if (i % 3 0) {routingKey spring.info;} else if (i % 3 1) {routingKey java.warn;}String message 这是Topics模式发送的第【 (i1) 】条【 routingKey 】消息数据;channel.basicPublish(exchangeName, routingKey, null, message.getBytes());}} catch (Exception e) {e.printStackTrace();} finally {if (null ! channel) {try {channel.close();} catch (Exception e) {}}if (null ! connection) {try {connection.close();} catch (Exception e) {}}}}
}3编写消费者
消费者消费消息的时候采用Topic主题模式并且只消费routing key满足【spring.*】的消息。
package com.rabbitmq.demo.topics;import com.rabbitmq.client.*;import java.io.IOException;/*** version 1.0.0* Date: 2023/2/25 16:30* Copyright (C) ZhuYouBin* Description: 消息消费者*/
public class Consumer {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost(127.0.0.1); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection null; // 连接Channel channel null; // 通道try {connection factory.newConnection();// 4、获取通道channel connection.createChannel();// 5、声明 Exchange如果不存在则会创建String exchangeName exchange_topics_2023;channel.exchangeDeclare(exchangeName, topic);// 6、指定需要操作的消息队列如果队列不存在则会创建String queueName queue_topics_2023;channel.queueDeclare(queueName, false, false, false, null);// 7、绑定 Exchange 和 Queue, 接收 routingKey spring.* 的消息channel.queueBind(queueName, exchangeName, spring.*);// 8、消费消息DeliverCallback callback new DeliverCallback() {public void handle(String s, Delivery delivery) throws IOException {// 接收消息System.out.println(这是接收的消息 new String(delivery.getBody()));}};channel.basicConsume(queueName, true, callback, i-{});} catch (Exception e) {e.printStackTrace();}}
}运行结果如下所示 到此RabbitMQ消息队列中的Topic主题模式就介绍完啦。 综上这篇文章结束了主要介绍消息队列RabbitMQ七种模式之Topics主题模式。
文章转载自: http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.spftz.cn.gov.cn.spftz.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.nwnbq.cn.gov.cn.nwnbq.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.bqdgr.cn.gov.cn.bqdgr.cn http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.zlrrj.cn.gov.cn.zlrrj.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn