package com.example.springbootrocketdemo.config;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Service;
/**
* 消费批量消息
* 配置RocketMQ监听
* ConsumeMode.ORDERLY-顺序消费
* @RocketMQMessageListener中的属性配置不⾜以满⾜业务需求,可以实现DefaultMQPushConsumer
*/
@Service
@RocketMQMessageListener(consumerGroup = "test-batch",topic = "test-topic-batch",consumeMode = ConsumeMode.ORDERLY)
public class RocketMQBatchConsumerListener implements RocketMQListener<String> , RocketMQPushConsumerLifecycleListener {
@Override
public void onMessage(String s) {
System.out.println("consumer 批量消息,收到消息:"+s);
}
@Override
public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
//设置每次消息拉取的时间间隔 单位 毫秒
defaultMQPushConsumer.setPullInterval(1000);
//最小消费线程池数
defaultMQPushConsumer.setConsumeThreadMin(1);
//最大消费线程池数
defaultMQPushConsumer.setConsumeThreadMax(10);
//设置消费者单次批量消费的消息数目上限 默认1
defaultMQPushConsumer.setConsumeMessageBatchMaxSize(3);
//设置每个队列每次拉取的最大消费数
defaultMQPushConsumer.setPullBatchSize(16);
}
}
Rocketmq批量消费
于 2024-06-03 19:35:42 首次发布