一个消费者组中的所有消费者订阅关系,可以多个topic,多个tag,但是必须一致,否则就倒沫子了,如下图:
下面贴了如下结构的代码
一个消费组(消费者)订阅多个topic的代码(只写了一个消费组的,其他类似):
@Component
@RocketMQMessageListener(topic = "",consumerGroup="group1")
public class MessageListener1 implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
@Override
public void onMessage(String str) {
}
@Override
public void prepareStart(DefaultMQPushConsumer consumer) {
try{
consumer.subscribe("topic-a","*");
consumer.subscribe("topic-b","*");
consumer.registerMessageListener((List<MessageExt> messages, ConsumeConcurrentlyContext context) -> {
if (CollectionUtils.isNotEmpty(messages)) {
for (MessageExt message : messages) {
System.out.println(Thread.currentThread().getName()+"消费完成:"+message.getTopic()+ new String(message.getBody()));
}
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
}catch (MQClientException e){
e.printStackTrace();
}
}
}
结果:文章来源:https://www.toymoban.com/news/detail-612229.html
文章来源地址https://www.toymoban.com/news/detail-612229.html
到了这里,关于springboot整合rocketmq:一个消费者组怎么订阅多个topic的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!