【问题标题】:Spring-boot app displaying JMS messages on WebPage via WebSocketSpring-boot 应用程序通过 WebSocket 在 WebPage 上显示 JMS 消息
【发布时间】:2015-05-06 18:56:01
【问题描述】:

我需要一个简单的 web 应用程序在 spring-boot 中侦听 JMS 队列上的消息,当到达时它应该通过 WebSocket 出现在网页上。

我搜索了一些例子,找到了几个人;我自己测试过的 WebSocket 或 JMS,但没有成功地将它们连接在一起。

我已经搜索了一个示例,但没有找到任何示例,我认为这应该很容易,因为这是一个非常基本的要求。

您是否知道任何通过 WebSocket 显示 JMS 和 HTML 的示例,您可以分享或者可以为我提供一些提示或帮助来解决它?

【问题讨论】:

    标签: websocket jms spring-boot stomp sockjs


    【解决方案1】:

    Spring Integration 前来救援。

    您可以编写<int-jms:message-driven-channel-adapter> 来从JMS 队列中读取消息并将它们转发到<int-websocket:outbound-channel-adapter>。最后一个只是向连接的 WebSocket 会话发送消息。

    请参阅这些 Spring Integration 示例:

    https://github.com/spring-projects/spring-integration-samples/tree/master/basic/jms

    https://github.com/spring-projects/spring-integration-samples/tree/master/basic/web-sockets

    更新

    要将消息发送到所有订阅的 WebSocket 会话,您应该执行以下操作:

    <int:splitter input-channel="enricheMessage" output-channel="sendMessage" apply-sequence="false">
        <int-groovy:script>
            @serverWebSocketContainer.sessions.keySet().collect {
                org.springframework.integration.support.MessageBuilder.withPayload(payload)
                        .copyHeaders(headers)
                        .setHeader('simpSessionId', it)
                        .build()
            }
        </int-groovy:script>
    </int:splitter>
    

    使用这个 Groovy 脚本,我从 serverWebSocketContainer(所有连接的客户端)中检索 session ids,遍历它们以构建消息以通过其 websocket 发送它们。最后还有split,一一发送到&lt;int-websocket:outbound-channel-adapter&gt;

    【讨论】:

    • 谢谢,这似乎是正确的方式,但不幸的是我没有得到它的工作。我已经将这两个示例合并在一起并且它可以工作,并且我还能够将 JMS 消息有效负载路由到 WebSocket bean(例如 headerEnricherChannel),但它抱怨错误的会话 ID,并且它似乎使用有效负载作为会话 ID。我想向所有客户广播相同的内容。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2018-07-06
    • 2022-11-09
    • 2016-10-20
    • 2021-06-03
    相关资源
    最近更新 更多