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

通过STOMP+Python消费ActiveMQ Artemis大消息遇异常求助

问题分析与解决方案

核心问题本质

  1. 嵌入式服务器未配置大消息存储:ActiveMQ Artemis的大消息分片机制依赖持久化存储来暂存分片数据,但嵌入式模式默认未指定存储目录,导致大消息无法落地分片,直接触发NullStorageLargeServerMessage异常。
  2. STOMP连接器未开启分片支持:Artemis的STOMP连接器默认不支持大消息分片,即便服务器端配置了大消息阈值,也不会自动对STOMP消息进行分片处理。
  3. 临时队列默认禁用大消息存储:临时队列默认不允许存储大消息,需要手动开启相关权限。

具体修复操作

1. 补全嵌入式Artemis的存储配置

启动嵌入式服务器时,必须指定存储目录并配置大消息相关参数。修改Java服务端的Configuration代码:

Configuration config = new ConfigurationImpl();
// 必须设置存储目录,大消息分片需落地存储
config.setBindingsDirectory("artemis-data/bindings");
config.setJournalDirectory("artemis-data/journal");
config.setLargeMessagesDirectory("artemis-data/large-messages");
config.setPersistenceEnabled(true);
// 设置大消息阈值(示例设为2MB,小于你的2.8MB消息)
config.setLargeMessageSizeThreshold(2 * 1024 * 1024);
// 允许临时队列存储大消息
config.setAllowTempStoreLargeMessages(true);

2. 开启STOMP连接器的大消息分片支持

在Artemis的broker.xml配置文件中,给STOMP acceptor添加supports-large-messages="true"参数:

<acceptor name="stomp-acceptor">tcp://0.0.0.0:61613?protocols=STOMP;supports-large-messages=true</acceptor>

如果是在Java代码中配置acceptor,直接追加参数即可:

config.addAcceptorConfiguration("stomp-acceptor", "tcp://0.0.0.0:61613?protocols=STOMP;supports-large-messages=true");

3. 调整Python STOMP客户端设置

使用stomp.py库时,确保使用最新稳定版以支持大消息分片。示例客户端代码:

import stomp

class MyListener(stomp.ConnectionListener):
    def on_message(self, headers, body):
        # 处理接收到的大JSON消息
        print(f"Received message size: {len(body)} bytes")

conn = stomp.Connection([('localhost', 61613)])
conn.set_listener('', MyListener())
conn.connect('admin', 'admin', wait=True)
# 创建临时队列并发送请求
temp_sub = conn.subscribe(destination='/temp-queue/myqueue', id=1, ack='auto')
conn.send(body='request', destination='/topic/request', headers={'reply-to': temp_sub})

4. 消息发送的注意事项

  • 服务器端发送大JSON时,需确保消息大小超过配置的largeMessageSizeThreshold,这样Artemis才会将其识别为大消息并触发分片。
  • 不要向临时队列发送非持久化大消息,临时队列的大消息必须依赖存储,因此allowTempStoreLargeMessages必须设为true。

验证步骤

启动服务器后,检查artemis-data/large-messages目录是否生成分片文件;发送消息后查看服务器日志,确认不再抛出IndexOutOfBoundsException,同时客户端能完整接收2.8MB的JSON内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 12:17:44