springboot2.1.6.RELEASE版本结合rocketMq4.3.2测试demo

(一)Linux下Rocketmq安装

Linux下Rocketmq安装教程=>传送门

(二)整体结构

springboot2.1.6.RELEASE版本结合rocketMq4.3.2测试demo_第1张图片

  • 父级pom

	4.0.0
	priv.wei
	rocketmq
	0.0.1-SNAPSHOT
	pom
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.6.RELEASE
		
	
	
		UTF-8
		UTF-8
		1.8
		1.2.62
		3.9
		4.3.2
		3.1.1
	
	
		
			
			
				com.alibaba
				fastjson
				${fastjson.version}
			
			
				org.apache.commons
				commons-lang3
				${commons-lang3.version}
			
			
				org.apache.rocketmq
				rocketmq-client
				${rocketmq.version}
			
			
		
	
	
		
			org.springframework.boot
			spring-boot-starter
		
		
			org.projectlombok
			lombok
		
		
			org.apache.commons
			commons-lang3
		
		
			com.alibaba
			fastjson
		
		
			org.apache.rocketmq
			rocketmq-client
		
		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
	
	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					${java.version}
					${java.version}
				
			

			
				org.apache.maven.plugins
				maven-surefire-plugin
				
					true    
				
			
			
				maven-compiler-plugin
				
					1.8
					1.8
					UTF-8
					true
					false
					true
					false
					
						
						${java.home}/lib/rt.jar;${java.home}/lib/jce.jar
					
				
			
		
	
	
		producer
		consumer
	

(三)生产者模块

springboot2.1.6.RELEASE版本结合rocketMq4.3.2测试demo_第2张图片

  • Producer.java
package priv.wei.producer.mq;

import javax.annotation.PostConstruct;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import priv.wei.producer.mq.config.MqConfig;
import priv.wei.producer.util.ExpUtil;

/**
 * 生产者
 */
@Slf4j
@Component
public class Producer {
	
	@Autowired
	private MqConfig mc;
	
    private DefaultMQProducer producer;
    
    /**
     * 系统启动时自动初始化生产者
     * @throws MQClientException
     */
    @PostConstruct
    private void init() throws MQClientException{
    	 //示例生产者
        producer = new DefaultMQProducer(mc.getProducerGroup());
        //不开启vip通道 开通口端口会减2
        producer.setVipChannelEnabled(false);
        //绑定name server
        producer.setNamesrvAddr(mc.getNameServer());
    	//初始化启动
        producer.start();
        
        log.info("mq生产者初始化成功!");
    }
  
    /**
     * 发送请求
     * @param topic 主题
     * @param tag 标签
     * @param content 消息内容
     */
    public void send(String topic,String tag,String content){
    	try {
    		Message msg = new Message(topic, tag, content.getBytes(RemotingHelper.DEFAULT_CHARSET));
    		SendResult sr = producer.send(msg);
    		log.info("发送响应:MsgId:{},发送状态SendStatus:{}",sr.getMsgId(),sr.getSendStatus());
		} catch (Exception e) {
			log.error("发送信息异常:{}",ExpUtil.getStackMsg(e));
		}
    }
    
    /**
     * 单独获取生产者
     * @return DefaultMQProducer
     */
    public DefaultMQProducer get(){
        return this.producer;
    }
    
    /**
     * 关闭生产者
     */
    public void shutdown(){
        this.producer.shutdown();
    }
}

(四)消费者模块

springboot2.1.6.RELEASE版本结合rocketMq4.3.2测试demo_第3张图片

  • TestDateConsumer.java
package priv.wei.consumer.mq.listener;

import javax.annotation.PostConstruct;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import priv.wei.consumer.mq.config.MqConfig;
import priv.wei.consumer.service.ConsumerService;
import priv.wei.consumer.util.ExpUtil;

/**
 * 监听消息,并消费
 */
@Slf4j
@Component
public class TestDateConsumer {
	
	@Autowired
	private MqConfig mc;
	
	@Autowired
	private ConsumerService consumerService;
	
	private DefaultMQPushConsumer consumer;
	
	/**
	 * 项目启动时初始化监听test_topic-test_tag
	 */
	@PostConstruct
	private void init(){
		try {
			consumer = new DefaultMQPushConsumer();
			consumer.setNamesrvAddr(mc.getNameServer());
			consumer.setConsumerGroup(mc.getConsumerGroup());
			//监听指定的topic,tag消息
			consumer.subscribe("test_topic", "test_tag");
            consumer.registerMessageListener((MessageListenerConcurrently) (list, context) -> {
                try {
                    for (MessageExt messageExt : list) {
                    	//消费内容
                    	consumerService.test(new String(messageExt.getBody()));
                    }
                } catch (Exception e) {
                	log.info("消费异常:{}",ExpUtil.getStackMsg(e));
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER; //稍后再试
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; //消费成功
            });
            consumer.start();
			log.info("初始化监听:topic:test_topic,tag:test_tag启动成功!");
		} catch (Exception e) {
			log.error("初始化监听:topic:test_topic,tag:test_tag启动异常:{}",ExpUtil.getStackMsg(e));
		}
	}
}

(五)代码GitHub地址

https://github.com/shouwangying/rocketmq-demo.git

你可能感兴趣的:(java,RocketMq,springboot)