Rocketmq根据Tag消费消息
package com.lazycece.sbac.rocketmq.tags;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author hjt
* @date 2021/8/6
*/
@Component
@Slf4j
public class TagProducer {
@Resource
private RocketMQTemplate rocketMQTemplate;
public void sendTagsMessage() {
String[] tags = new String[]{"A", "B", "C", "D"};
String message = "tags message : ";
for (int i = 0; i < tags.length; i++) {
// int k = i % tags.length;
rocketMQTemplate.syncSend("topic-tags:" + tags[i], message + tags[i]);
// rocketMQTemplate.syncSend("topic-tags:" + tags[k], message + tags[k]);
}
}
}
消费端消费
package com.lazycece.sbac.rocketmq.tags;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* @author hjt
* @date 2021/8/6
*/
@Component
@Slf4j
@RocketMQMessageListener(
topic = "topic-tags",
consumerGroup = "tags-consumer-group",
selectorExpression = "A||B||C")
public class TagConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("messgaetag:"+message);
log.info("======={}=======", message);
}
}
输出
代码可以私信找我要哦