1 tag过滤,只修改消费者程序的代码
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_EXAMPLE");
consumer.subscribe("TOPIC", "TAGA || TAGB || TAGC");
消费者将收到包含TAGA或TAGB或TAGB的消息。但限制是一条消息只能有一个标签,而这对于复杂的情况可能无效。在这种情况下,您可以使用SQL表达式筛选出消息。
用 * 号表示所有的tag的消息
2 sql的方式过滤,生产者和消费者都需要改
如果报错,检查下服务器安装RocketMq目录下的配置文件 brock.conf ,属性enablePropertyFilter=true 可能没有设置
//生产者
Message msg = new Message("topicList", "tag_a",i + "", ("内容-" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
//关键点
msg.putUserProperty("idhao",String.valueOf(i));
SendResult result = producer.send(msg);
####################################################################################################
####################################################################################################
//消费者
DefaultMQPushConsumer xiaofeizhe_group1 = new DefaultMQPushConsumer("xiaofeizhe_group1");
//关键点
xiaofeizhe_group1.subscribe("topicList", MessageSelector.bySql("idhao between 2 and 3"));