You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JMS获取ActiveMQ Artemis队列中的LargeMessage?

解决ActiveMQ Artemis JMS获取大消息的问题

JMS标准API里并没有LargeMessage这个类型,ActiveMQ Artemis的大消息是通过JMS标准消息类型(比如BytesMessage、StreamMessage)封装的,你需要转换到Artemis的核心API来处理:

  • 首先把收到的JMS Message转成Artemis的ClientMessage:
import org.apache.activemq.artemis.jms.client.ActiveMQMessage;
import org.apache.activemq.artemis.core.client.ClientMessage;

// jmsMessage是你收到的JMS消息
ClientMessage coreMessage = ((ActiveMQMessage) jmsMessage).getCoreMessage();
  • 判断是否为大消息并读取内容:
if (coreMessage.isLargeMessage()) {
    // 通过输入流读取大消息内容,避免内存溢出
    try (InputStream inputStream = coreMessage.getBodyInputStream()) {
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            // 在这里处理读取到的字节数据,比如写入文件、解析内容等
        }
    } catch (IOException e) {
        // 处理IO异常
    }
}

如果你是通过BytesMessage接收的大消息,也可以直接用JMS API的readBytes()方法读取,但对于超大规模的消息,用流的方式(上面的getBodyInputStream())更节省内存。

内容的提问来源于stack exchange,提问作者user21871375

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 15:24:57