rocketMq springboot2 接入配置 发送普通消息

rocketmq的接入配置。

普通消息:无序的消息。没有先后顺序

 

  • 引入jar包
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-client</artifactId>
</dependency>

 

  • spring 配置文件

bootstrap.yml 或 application.yml 增加如下配置

#rockmq配置
rocketmq:
  producer:
    group: mq-group
  consumer:
    group:
mq-group

 

mq topic创建

# mqadmin updateTopic -c rocketmq-cluster -t TOPIC-SYS-MESSAGE

mq group创建

mqadmin updateSubGroup  -c rocketmq-cluster -g GID-LOG

 

普通生产消息sender

其中常量

    public static final String TOPIC_SYS_MESSAGE = "TOPIC-SYS-MESSAGE";
    public static final String TAG_MSG_LOG = "LOG";
    public static final String G_TAG_MSG_LOG = GROUP_PREFIX + TAG_MSG_LOG;

 

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

/**
 * 日志发送
 *
 */
@Slf4j
@Component
public class LogSender {
    /**发送mongo存储*/public void sendMessage(String applicationName, MgSaveReq mgSaveReq) {
        if (null == mgSaveReq) {
            throw new ApiException(applicationName, ApiErrorEnum.SYSERROR, "发送日志消息内容为空");
        }
        RocketMQTemplate rocketMQTemplate = (RocketMQTemplate) SpringUtils.getBean("rocketMQTemplate");
        String queueContent = JSON.toJSONString(mgSaveReq);
        String destination = RocketMqConstants.TOPIC_SYS_MESSAGE + ":" + RocketMqConstants.TAG_MSG_LOG;
        long timeout = 6000; // 6S
        try {
            SendResult sendResult = rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(queueContent).build(), timeout);
            log.info("sendMessage syncSend to topic {} sendResult={}", destination, sendResult);
        }catch(Exception e) {
            log.error("LogSender.sendMessage exception", e);
        }
    }

}

 

消费消息

import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * 日志存储接收
 *
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = RocketMqConstants.TOPIC_SYS_MESSAGE,
        consumerGroup = RocketMqConstants.G_TAG_MSG_LOG,
        selectorExpression = RocketMqConstants.TAG_MSG_LOG)
public class LogListener implements RocketMQListener<String> {
    
    @Autowired
    private MongoLogManager mongoLogManager;
    
    @Override
    public void onMessage(String messageContext) {
        if (StringUtils.isBlank(messageContext)) {
            return ;
        }
        try {
            MgSaveReq mgSaveReq = JSONObject.toJavaObject(JSONObject.parseObject(messageContext), MgSaveReq.class);
            mongoLogManager.saveToRedis(mgSaveReq);
        }catch(Exception e) {
            log.error("信息转换异常", e);
        }
    }
}

 

测试发送消息

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.ccjr.AccBizApp;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AccBizApp.class)
public class BaseTest {

    
    private Long starttime;
    @Rule
    public TestName junitClass= new TestName();
    @Before
    public void before() {
        starttime = System.currentTimeMillis();
        System.out.println(junitClass.getMethodName() + "....................start....................");
    }
    @After
    public void after() {
        double usedtime = (System.currentTimeMillis() - starttime) / 1000.0;
        System.out.println("耗时  " + usedtime + " ms");
        System.out.println(junitClass.getMethodName() + "....................end....................");
    }
    @Test
    public void hello() {
        System.out.println("hello world");
    }
}
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ccjr.acc.mq.sender.AccBizSender;
import com.ccjr.api.base.BaseMqRequest;
import com.ccjr.commons.utils.MyDateUtil;
import com.ccjr.test.BaseTest;

@Component
public class RocketTest extends BaseTest{
    @Autowired
private LogSender logSender;


    @Test
    public void accBizSenderTest() throws Exception {
try {
            MgSaveReq mgSaveReq = new MgSaveReq();
            mgSaveReq.setMgVo(logVo);
            mgSaveReq.setMgType(logType);
            logSender.sendMessage(getApplicationName(), mgSaveReq);
        } catch (Exception e) {
            log.error("记录日志异常:", e);
        }
       
        MyDateUtil.sleep(1000000);
    }
    
}

 

posted on 2022-11-14 11:41  陈惟鲜的博客  阅读(274)  评论(0编辑  收藏  举报

导航