分类过滤
- 生产者
Message msg = new Message("topic6","tag2",("消息过滤按照tag:hello rocketmq 2").getBytes("UTF-8"));
- 消费者
//接收消息的时候,除了制定topic,还可以指定接收的tag,*代表任意tag
consumer.subscribe("topic6","tag1 || tag2");
*代表任意tag
“tag1 || tag2” 代表两个 tag 那个都行
属性过滤
- 生产者
//为消息添加属性
msg.putUserProperty("vip","1");
msg.putUserProperty("age","18");
- 消费者
//使用消息选择器来过滤对应的属性,语法格式为类SQL语法
consumer.subscribe("topic7", MessageSelector.bySql("age >= 18"));
注意
SQL过滤需要依赖服务器的功能支持,在broker配置文件中添加对应的功能项,并开启对应功能
enablePropertyFilter=true
启动服务器
sh mqbroker -n localhost:9876 -c ../conf/broker.conf