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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2013-11-14 SQL Server查询表结构语句