package org.springblade.modules.rocketmq;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
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.Component;
import java.util.List;
@Component
@RocketMQMessageListener(topic = "",consumerGroup="provider")
public class MessageListener implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
@Override
public void onMessage(String str) {
}
@Override
public void prepareStart(DefaultMQPushConsumer consumer) {
try{
consumer.subscribe("UAUTH_SLICE_USER_PUSH_TOPIC","*");
consumer.subscribe("UAUTH_ALL_USER_PUSH_TOPIC","*");
consumer.subscribe("UAUTH_LIVE_USER_PUSH_TOPIC","*");
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();
}
}
}
相关链接:springboot整合rocketmq:一个消费者组怎么订阅多个topic_如何配置rocketmq 消费多个topic-CSDN博客