深入理解RocketMQ(九)---实战(代码)
一、批量发送消息
即多条消息放入List,一次发送,从而减少网络传输,提高效率
DefaultMQProducer producer = new DefaultMQProducer("batch_send_producer_group");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start(); String topic = "batchTopic";
List<Message> messageList = new ArrayList<>(); for (int i = 0; i < 10; i++) {
Message msg = new Message(topic,"TAG1","ORDER" + i, "Hello world".getBytes());
messageList.add(msg);
}
try{
producer.send(messageList);
}catch (Exception e){
e.printStackTrace();
} producer.shutdown();
二、消息发送队列自主选择
例:可以将同一订单(不同操作,例如下单、付款、出库、订单完成等操作)发送到同一个queue中,来保证一个订单不同操作的顺序性
DefaultMQProducer producer = new DefaultMQProducer("select_queue_producer_group");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start(); String topic = "selectQueueTopic"; String[] tags = new String[]{"TAG","TAG2","TAG3","TAG4","TAG5"}; int orderId = 41;
int orderId1 = 42;
try{
for (int i = 0; i < 10; i++) {
Message msg = new Message(topic,tags[i%tags.length],"KEY" + i, ("Hello world"+i).getBytes());
SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
return mqs.get((Integer) arg % mqs.size());
}
},orderId1);
System.out.println(orderId1 + "=======" + sendResult); sendResult = producer.send(msg, new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
return mqs.get((Integer) arg % mqs.size());
}
},orderId);
System.out.println(orderId + "=======" + sendResult);
} }catch (Exception e){
e.printStackTrace();
} producer.shutdown();
输入如下:
可以看到订单ID为41的消息,全部发送到queueId为1的队列中,订单ID为42的消息,全部发送到QueueId为2的队列中
三、订单过滤
1、TAG模式过滤(多个tag使用 || 区分)
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
producer.setNamesrvAddr("127.0.0.1:9876"); producer.setNamesrvAddr("127.0.0.1:9876");
producer.start(); String topic = "TagFilterTopic1";
String tagA = "TagA";
String tagB = "TagB";
for (int i = ; i < ; i++) {
try {
Message msg = new Message(topic,tagA,("tagA==========11111111111").getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg); msg = new Message(topic,tagB,("tagA==========22222222222").getBytes(RemotingHelper.DEFAULT_CHARSET));
sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep();
}
}
producer.shutdown();
消息消费
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("tagFileterConcumer"); consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.setConsumerGroup("tagFileterConcumerGroup");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
consumer.subscribe("TagFilterTopic1", "TagA");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s TagA Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start(); System.out.printf("Consumer Started.%n"); DefaultMQPushConsumer consumer1 = new DefaultMQPushConsumer("tagFileterConcumer1"); consumer1.setNamesrvAddr("127.0.0.1:9876");
consumer1.setConsumerGroup("tagFileterConcumerGroup1");
consumer1.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
consumer1.subscribe("TagFilterTopic1", "TagA || TagB");
consumer1.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s TagA&TagB Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer1.start();
System.out.printf("Consumer Started.%n"); DefaultMQPushConsumer consumer2 = new DefaultMQPushConsumer("tagFileterConcumer2"); consumer2.setNamesrvAddr("127.0.0.1:9876");
consumer2.setConsumerGroup("tagFileterConcumerGroup2");
consumer2.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
consumer2.subscribe("TagFilterTopic1", "TagB");
consumer2.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s TagB Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer2.start();
System.out.printf("Consumer Started.%n");
执行结果:可以看到只需要TagA的消费者,只输出了一条消息;只需要TagB的消费者,也只输出了一条消息
2、SQL过滤
需要开启支持sql92:在broker.conf文件中添加如下配置:enablePropertyFilter=true
发送者:
DefaultMQProducer producer = new DefaultMQProducer("sql_filter_group_name");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start(); String topic = "SqlFilterTopic1";
String tagA = "TagA";
for (int i = ; i < ; i++) {
try {
Message msg = new Message(topic,tagA,("tagA==========11111111111").getBytes(RemotingHelper.DEFAULT_CHARSET));
msg.putUserProperty("orderStatus","");
msg.putUserProperty("userName","lcl");
msg.putUserProperty("orderId","");
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult); msg = new Message(topic,tagA,("tagA==========11111111111").getBytes(RemotingHelper.DEFAULT_CHARSET));
msg.putUserProperty("orderStatus","");
msg.putUserProperty("userName","lcl");
msg.putUserProperty("orderId","");
sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult); msg = new Message(topic,tagA,("tagA==========11111111111").getBytes(RemotingHelper.DEFAULT_CHARSET));
msg.putUserProperty("orderStatus","");
msg.putUserProperty("userName","mm");
msg.putUserProperty("orderId","");
sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult); msg = new Message(topic,tagA,("tagA==========11111111111").getBytes(RemotingHelper.DEFAULT_CHARSET));
msg.putUserProperty("orderStatus","");
msg.putUserProperty("userName","lcl");
msg.putUserProperty("orderId","");
sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult); msg = new Message(topic,tagA,("tagA==========11111111111").getBytes(RemotingHelper.DEFAULT_CHARSET));
msg.putUserProperty("orderStatus","");
msg.putUserProperty("userName","lcl");
msg.putUserProperty("orderId","");
sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep();
}
}
producer.shutdown();
消费者:
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("sqlFileterConcumer"); consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.setConsumerGroup("tagFileterConcumerGroup");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
consumer.subscribe("SqlFilterTopic1", MessageSelector.bySql("(orderStatus = '1' and userName = 'lcl' and orderId > 0)"));
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s TagA Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start(); System.out.printf("Consumer Started.%n"); DefaultMQPushConsumer consumer1 = new DefaultMQPushConsumer("tagFileterConcumer1"); consumer1.setNamesrvAddr("127.0.0.1:9876");
consumer1.setConsumerGroup("tagFileterConcumerGroup1");
consumer1.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
consumer1.subscribe("TagFilterTopic1", "TagA || TagB");
consumer1.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s TagA&TagB Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer1.start();
System.out.printf("Consumer Started.%n"); DefaultMQPushConsumer consumer2 = new DefaultMQPushConsumer("tagFileterConcumer2"); consumer2.setNamesrvAddr("127.0.0.1:9876");
consumer2.setConsumerGroup("tagFileterConcumerGroup2");
consumer2.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
consumer2.subscribe("TagFilterTopic1", "TagB");
consumer2.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
System.out.printf("%s TagB Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer2.start();
System.out.printf("Consumer Started.%n");
消费者输出结果:
3、类过滤模式
深入理解RocketMQ(九)---实战(代码)的更多相关文章
- 看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)
1.前言 最近有同学问我有没有做过在线咨询功能.同时,公司也刚好让我接手一个 IM 项目.所以今天抽时间记录一下最近学习的内容.本文主要剖析了 WebSocket 的原理,以及附上一个完整的聊天室实战 ...
- DOM基础操作实战代码
对于已经讲解给大家的DOM实战,我今天给大家几个实战代码,可以让大家加深对此的理解! 1.用DOM动态生成这样一个结构: <div class=”example”> <p class ...
- 深入理解RocketMQ的消费者组、队列、Broker,Topic
1.遇到的问题:上测试环境,上次描述的鸟问题又出现了,就是生产者发3条数据,我这边只能收到1条数据. 2.问题解决: (1)去控制台看我的消费者启动情况,貌似没什么问题 , (2)去测试服务器里看日志 ...
- HtmlAgilityPack实战代码
C#采集代理服务器ip并设置IE代理--HtmlAgilityPack实战代码 今天在博客园看到一篇文章,说是C#采集某某的数据,其实做采集小软件很久了, 用的最好的还是HtmlAgilityPack ...
- SpringCloud---熔断降级理解、Hystrix实战(五)
SpringCloud---熔断降级理解.Hystrix实战(五) https://www.cnblogs.com/qdhxhz/p/9581440.html https://blog.csdn.ne ...
- SpringCloud(6)---熔断降级理解、Hystrix实战
SpringCloud(6)---熔断降级理解.Hystrix实战 一.概念 1.为什么需要熔断降级 (1)需求背景 它是系统负载过高,突发流量或者网络等各种异常情况介绍,常用的解决方案. 在一个分布 ...
- dubbo项目实战代码展示
最近公司项目使用dubbo服务,于是就去网上搜索关于dubbo的相关资料,真的很多,但是对于很多人并不是很了解框架或者 不是太适合新手的片段代码,于是我就根据项目的相关内容把dubbo部分单独切出来, ...
- EM算法理解的九层境界
EM算法理解的九层境界 EM 就是 E + M EM 是一种局部下限构造 K-Means是一种Hard EM算法 从EM 到 广义EM 广义EM的一个特例是VBEM 广义EM的另一个特例是WS算法 广 ...
- Magenta Demos Magenta 实战代码
Magenta 实战代码 这个仓库包含了很多 Magenta 模型的实现.看 Magenta 库以及模型,看我们的主仓库:https://github.com/tensorflow/magenta A ...
- 创建HttpFilter与理解多个Filter代码的执行顺序
1.自定义的HttpFilter,实现Filter接口 HttpFilter package com.aff.filter; import java.io.IOException; import ja ...
随机推荐
- HTTP 冷知识 | HTTP 请求中,空格应该被编码为 %20 还是 + ?
HTTP 请求中,空格应该被编码为什么?今天我们走进 RFC 文档和 W3C 文档,了解一下这个「史诗级」大坑. 1.%20 还是 + ? 开始讲解前先看个小测试,在浏览器里输入 blank test ...
- Rocket - tilelink - Parameters
https://mp.weixin.qq.com/s/1I6DcONr0Mg7xiX8F1C7SQ 简单介绍TileLink相关的参数实现(具体问题暂时不展开,后续用到时再做分析). ...
- Chisel3 - Tutorial - Tbl
https://mp.weixin.qq.com/s/e8vJ8claauBtiuedxYYaJw 实现可以动态索引的表. 参考链接: https://github.com/ucb-bar/c ...
- [C#.NET拾遗补漏]01:字符串操作
字符串操作在任意编程语言的日常编程中都随处可见,今天来汇总一下 C# 中关于字符串的一些你可能遗忘或遗漏的知识点. 逐字字符串 在普通字符串中,反斜杠字符是转义字符.而在逐字字符串(Verbatim ...
- Linux(十) —— 使用 rz 和 sz 命令上传与下载
以CentOS 7 系统为例,一般上传下载都是使用的第三方工具,但是在操作上并不方便,每次都要找到对应的目录才可以执行上传.下载操作,比较麻烦. 而CentOS为例的 Linux 系统可以通过安装 插 ...
- Java 蓝桥杯 算法训练 貌似化学
** 貌似化学 ** 问题描述 现在有a,b,c三种原料,如果他们按x:y:z混合,就能产生一种神奇的物品d. 当然不一定只产生一份d,但a,b,c的最简比一定是x:y:z 现在给你3种可供选择的物品 ...
- Java实现 蓝桥杯 算法提高金属采集
问题描述 人类在火星上发现了一种新的金属!这些金属分布在一些奇怪的地方,不妨叫它节点好了.一些节点之间有道路相连,所有的节点和道路形成了一棵树.一共有 n 个节点,这些节点被编号为 1~n .人类将 ...
- Myeclipse 2014 破解补丁以及Y2课件迅雷下载
一. 在破解myeclipse2014之前,要先把环境变量配置好: 1)打开我的电脑--属性--高级--环境变量2)新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变 ...
- 【Flutter实战】移动技术发展史
老孟导读:大家好,这是[Flutter实战]系列文章的第一篇,这并不是一篇Flutter技术文章,而是介绍智能手机操作系统.跨平台技术的演进以及我对各种跨平台技术看法的文章. 智能手机操作系统 塞班( ...
- Java复习目录
还是寒假用了十多天的时间在b站把基础部分学习完了,现在刚开学开始上Java课,以博客的方式复习前面学习的内容. 总结: ①吸取前面MySQL学习的教训,每天固定学习的内容,学习效果很有提升,但临近开学 ...