我已经使用 Java DSL 创建了一个可用的 Camel Websocket 路由,但我不知道如何让 Camel 在新客户端连接时触发事件,以便我可以向客户端发送初始化消息...
LOG.info("ReplicationSink route is loading");
String uri = "websocket://0.0.0.0:2081/v3/replication";
boolean interest_sent = false;
onException(AccessDeniedException.class)
.handled(true)
.transform()
.simple("{\"error\": \"Access Denied\", \"message\":\"${exception.message}\"}")
.to(uri);
from(uri) // How can I specify here that upon initial connection to send the INTEREST_MESSAGE?
.log(DEBUG, "replication-sink", simple("${body}").getText())
.choice()
.when().simple("${header.interested} == null")
.log(DEBUG, "Preparing to send interest message")
.setBody().constant(INTEREST_MESSAGE)
.setHeader("interested").constant("true")
.to(uri)
.when().simple("${header.qfor} == null")
.log(DEBUG, "Setting QFOR headers")
.setHeader("data").body()
.setHeader("qfor").constant("TEST")
.setHeader("self").javaScript("JSON.parse(request.body).self")
.to(uri)
.otherwise()
.log(DEBUG, "Receiving updates")
.setBody(simple("INSERT INTO raw (qfor, self, data) VALUES (:?qfor, :?self, :?data::jsonb)"))
.to("jdbc:ccindexer?useHeadersAsParameters=true");
最佳答案
Afaik,我认为在当前版本的 Camel 中这是不可能的。
解决方法:
- 配置 websocket 组件以使用您自己的 WebSocketFactory。这家工厂应该延长
DefaultWebSocketFactory
并且应该在“默认”协议(protocol)下注册(即参见WebSocketComponent.setSocketFactory
) - 在
WebSocketFactory
的实现中,返回您自己的WebSocket
的实现,延伸DefaultWebSocket
- 在 WebSocket 的实现中,您可以访问方法
onOpen
以及相关的Connection
(与Connection.sendMessage
)
关于java - 如何让Camel Websocket组件在客户端连接时发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976295/