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

使用stomp-client连接ActiveMQ发消息报错:Server has gone away

解决STOMP发送消息报错"Server has gone away"的方案

你遇到的问题是:使用stomp-client@^0.9.0连接ActiveMQ,初始连接成功能获取session ID,但调用publish时抛出Error: Server has gone away错误。以下是针对性的解决建议:

1. 监听连接断开事件,实现自动重连

stomp-client库本身没有内置自动重连机制,ActiveMQ会因连接空闲超时主动断开连接,导致后续发送失败。需要监听连接的错误和关闭事件,触发时重新建立连接:

class StompProducer {
  private stompClient: StompClient;
  private isConnected = false;

  constructor(url: string, port: number, username: string, password: string) {
    this.stompClient = new StompClient(url, port, username, password);
    // 监听连接错误与关闭事件
    this.stompClient.on('error', (err) => {
      console.error('STOMP连接错误:', err);
      this.isConnected = false;
      this.reconnect();
    });
    this.stompClient.on('close', () => {
      console.log('STOMP连接已关闭');
      this.isConnected = false;
      this.reconnect();
    });
  }

  private reconnect() {
    // 避免频繁重连,添加延迟
    setTimeout(() => {
      console.log('尝试重新连接ActiveMQ...');
      this.connect().catch(err => console.error('重连失败:', err));
    }, 3000);
  }

  connect(): Promise<void> {
    return new Promise<void>((resolve, reject) => {
      this.stompClient.connect(
        (sessionId: string) => {
          console.log(`Producer connected to ActiveMQ with session ID: ${sessionId}`);
          this.isConnected = true;
          resolve();
        },
        (error: Error) => {
          console.error("Error connecting to ActiveMQ producer:", error);
          this.isConnected = false;
          reject(error);
        },
      );
    });
  }

  sendMessage(destination: string, message: string): void {
    if (!this.isConnected) {
      console.warn('连接未建立,先重连再发送');
      this.reconnect().then(() => {
        this.stompClient.publish(destination, message);
      });
      return;
    }
    try {
      this.stompClient.publish(destination, message);
    } catch (err) {
      console.error('发送消息失败:', err);
      this.isConnected = false;
      this.reconnect();
    }
  }
}

2. 指定STOMP协议版本,匹配ActiveMQ配置

ActiveMQ默认支持STOMP 1.1/1.2,而stomp-client默认可能使用旧版本,协议不匹配会导致连接隐性断开。创建客户端时显式指定协议版本:

this.stompClient = new StompClient(url, port, username, password, {
  protocolVersion: '1.2' // 或 '1.1',根据ActiveMQ配置调整
});

3. 检查消息目的地格式与消息内容

  • ActiveMQ的STOMP目的地格式必须规范:队列用/queue/队列名,主题用/topic/主题名,确保你传入的destination格式正确;
  • 发送消息时可指定Content-Type头部,避免格式解析错误:
sendMessage(destination: string, message: string): void {
  this.stompClient.publish(destination, message, {
    'content-type': 'text/plain'
  });
}

4. 排查网络与服务器配置

  • 用telnet 服务器IP 61613(默认STOMP端口)测试网络连通性,确认端口未被防火墙拦截;
  • 检查ActiveMQ的activemq.xml配置,确认STOMP连接器启用,且未设置过短的空闲超时:
    <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=10485760"/>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:23:11