Spring-boot应用程序通过WebSocket在WebPage上显示JMS消息

时间:2015-05-06 18:56:01

标签: websocket jms spring-boot stomp sockjs

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

我搜索了一些例子,发现了几个人; WebSocket或JMS,我已经自己测试但没有成功连接它们。

我搜索过一个例子,但没有找到任何例子,在我看来,我认为它应该很简单,因为它是一个非常基本的要求。

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

1 个答案:

答案 0 :(得分: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脚本,我从session ids(所有这些连接的客户端)检索serverWebSocketContainer,迭代它们以构建消息以通过其websocket发送它们。最后,split逐个发送给<int-websocket:outbound-channel-adapter>