RocketMQ集成Springboot --Chapter2

消息消费方式

1、集群模式

默认就是集群模式,无需修改

//消费者默认集群模式

@Component
@RocketMQMessageListener(consumerGroup = "htpConsumerGroup",topic = "helloTopicBoot")
public class HelloTopicListener implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("收到的消息:"+new String(messageExt.getBody(), Charset.defaultCharset()));
    }
}

2、广播模式

只需在@RocketMQMessageListener(consumerGroup = “htpConsumerGroup”,topic = “helloTopicBoot”)注解中添加messageModel = MessageModel.BROADCASTING即可

//消费者广播模式
@Component
@RocketMQMessageListener(consumerGroup = "htpConsumerGroup",topic = "helloTopicBoot",messageModel = MessageModel.BROADCASTING)
public class HelloTopicListener implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("收到的消息:"+new String(messageExt.getBody(), Charset.defaultCharset()));
    }
}

你可能感兴趣的:(java-rocketmq,rocketmq,spring,boot)