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

Apache Camel路由发送数据到WSS(STOMP)端点报错,求适配组件

问题描述

我已使用SpringBoot搭建WSS(STOMP)服务,经在线工具及SpringBoot客户端测试运行正常,服务端点为wss://spring-ou5b.onrender.com/web-socket-endpoint,消息接收路径为/app/message。现尝试通过Apache Camel项目向该端点发送数据,编写的路由代码如下:

package learn.camelmicorservices.routes;

import org.apache.camel.InOut;
import org.apache.camel.Produce;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ActiveMqSenderRouter extends RouteBuilder {
 
    @Override
    @InOut
    @Produce(value ="app/message" )
    public void configure() throws Exception {
int i=0;
        from("timer:wss-timer?period=5000").transform().constant("{\"name\":\"new\"+(++i)+\"\"}" )
            .log("Created"+"${body}")
            .to("wss://spring-ou5b.onrender.com/web-socket-endpoint?sendToAll=true")
            .log("After End"+"${body}");

    }
}

但运行时出现错误:

Caused by: org.apache.camel.NoSuchEndpointException: No endpoint could be found for: wss://.....

我已尝试websocket、vertx、ahc组件,均无法正常发送数据或出现其他错误,请问需要使用哪个Apache Camel组件才能实现该功能?

解决方案

要向STOMP协议的WSS服务发送消息,必须使用Apache Camel STOMP组件——普通WebSocket组件仅能处理基础的WebSocket通信,无法解析STOMP协议的帧格式和交互逻辑,这是你之前尝试失败的核心原因。

步骤1:添加依赖

在项目中引入STOMP组件依赖(版本需与你的Apache Camel版本匹配):

  • Maven:
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-stomp</artifactId>
    <version>x.x.x</version>
</dependency>
  • Gradle:
implementation 'org.apache.camel:camel-stomp:x.x.x'

步骤2:修正路由配置

STOMP组件的端点格式需要适配STOMP协议的连接规则,修改后的路由代码如下:

package learn.camelmicorservices.routes;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ActiveMqSenderRouter extends RouteBuilder {
 
    @Override
    public void configure() throws Exception {
        from("timer:wss-timer?period=5000")
            // 用定时器内置计数器替代自定义变量,避免线程安全问题
            .transform().simple("{\"name\":\"new${exchangeProperty.CamelTimerCounter}\"}")
            .log("Created message: ${body}")
            // STOMP端点配置:指定WSS服务地址、启用SSL、目标消息路径
            .to("stomp://spring-ou5b.onrender.com:443/web-socket-endpoint?ssl=true&destination=/app/message")
            .log("Message sent successfully");
    }
}

关键说明

  1. 端点参数解析:
    • ssl=true:启用WSS加密连接,对应服务的wss协议
    • destination=/app/message:指定STOMP消息的目标路径,与你的服务接收路径完全匹配
  2. 协议适配:STOMP组件会自动处理STOMP握手、帧封装、心跳等协议细节,无需手动实现
  3. 线程安全优化:使用定时器内置的CamelTimerCounter属性替代自定义变量i,避免多线程环境下的计数错误

额外注意事项

  • 如果你的STOMP服务需要认证,可在端点URL中添加login=<用户名>&passcode=<密码>参数
  • 确保Camel版本与STOMP组件版本一致,避免依赖冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 23:45:33