activemq artemis源码阅读,读取消息

前言

本文旨在对阅读源代码的过程进行总结,深度揭示其工作原理,以及信息是如何被逐层读取的,并着重探讨关键数据的来源。

编译代码

代码版本选用2.26.0的分支代码编译花了17分钟

梳理流程

测试用例

org/apache/activemq/artemis/jms/tests/TopicTest.java

ini 复制代码
@Testpublic void testTopic() throws Exception {   Connection conn = createConnection();

   Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageProducer p = s.createProducer(ActiveMQServerTestCase.topic1);
   MessageConsumer c = s.createConsumer(ActiveMQServerTestCase.topic1);
   conn.start();

   p.send(s.createTextMessage("payload"));
   TextMessage m = (TextMessage) c.receive();

   ProxyAssertSupport.assertEquals("payload", m.getText());
}

消息的读取

org.apache.activemq.artemis.jms.client.ActiveMQTextMessage#doBeforeReceive

scss 复制代码
@Overridepublic void doBeforeReceive() throws ActiveMQException {   
   super.doBeforeReceive();
   // 读取数据的核心代码
   text = readBodyText(message.getBodyBuffer());
}

进入super.doBeforeReceive()方法里面这里显示从仓库中标记当前的读取的位置到缓存中Repositions the current {@code readerIndex} to the marked {@code readerIndex} in this buffer.*

java 复制代码
public void doBeforeReceive() throws ActiveMQException {   
   message.checkCompletion();

   ActiveMQBuffer body = message.getBodyBuffer();

   if (body != null) {      
       body.resetReaderIndex();
   }
}

最后都去文本读取在这个方法里

org.apache.activemq.artemis.api.core.SimpleString#readSimpleString(io.netty.buffer.ByteBuf, int)

ActiveMQBuffer body = message.getBodyBuffer()

好奇messag跟着代码查创建TextMessage的时候根据session创建了new ActiveMQTextMessage(session); text消息

然后又根据消息里面的ClientMessage读取message.getBodyBuffer()获取ResetLimitWrappedActiveMQBuffer完成消息的读取

消息的接收

知道了消息的读取,那么消息从哪里接收呢?

org.apache.activemq.artemis.core.client.impl.ClientConsumerImpl#receive(long, boolean)

获取消息重点还是得看这段代码m = buffer.poll(),buffer是一个PriorityLinkedList集合获取到用户连接。

java 复制代码
while (true) {
   ClientMessageInternal m = null;

   synchronized (this) {
      while ((stopped || (m = buffer.poll()) == null) && !closed && toWait > 0) {

然后从buffer里面读取byte数组

org.apache.activemq.artemis.api.core.SimpleString#readSimpleString(io.netty.buffer.ByteBuf, int)

arduino 复制代码
byte[] data = new byte[length];
buffer.readBytes(data);
return new SimpleString(data);

总结

看源码不能一次全部搞懂所有代码,只能先看主干,之后再看细枝末节,所以先总结到获取消息部分,这是一个系列的一部份代码。在阅读开源项目源码时,我们需要从整体把握到逐步深入,从顶层设计到底层实现,从主干流程到细节部分,从常规情况到特殊情况,不断深入和拓展自己的知识和技能。

引用

activemq.apache.org/components/...

相关推荐
阿在在15 分钟前
Spring 系列(三):Spring PostProcessor 顶级扩展接口全解析
java·后端·spring
祈安_36 分钟前
深入理解指针(三)
c语言·后端
听风者就是我38 分钟前
(LLM系列)文档切分策略详解:Chunk Size 如何决定 RAG 系统的检索天花板
后端
野犬寒鸦1 小时前
ArrayList扩容机制深度解析(附时序图详细讲解)
java·服务器·数据结构·数据库·windows·后端
逆境不可逃1 小时前
【从零入门23种设计模式03】创建型之建造者模式(简易版与导演版)
java·后端·学习·设计模式·职场和发展·建造者模式
汤姆yu2 小时前
基于springboot的健身爱好者打卡与互动交流系统
java·spring boot·后端
计算机毕设VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue连锁门店管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
南部余额4 小时前
SpringBoot文件上传全攻略
java·spring boot·后端·文件上传·multipartfile
汤姆yu5 小时前
基于springboot的智能民宿预定与游玩系统
java·spring boot·后端
琢磨先生David5 小时前
有了AI,还需要学Springboot吗?
人工智能·spring boot·后端