简体   繁体   English

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

[英]Spring-boot app displaying JMS messages on WebPage via WebSocket

I need a simple web-app in spring-boot that listens for messages on a JMS queue and when arriving it should appear on a webpage via WebSocket. 我在spring-boot中需要一个简单的Web应用程序,它可以侦听JMS队列上的消息,并且到达时应通过WebSocket出现在网页上。

I have searched for examples and found several individual; 我搜索了一些例子,发现了几个个体。 either WebSocket or JMS which I have tested on their own but have not succeeded in wiring it together. 我自己测试过的WebSocket或JMS,但没有成功将它们连接在一起。

I have searched for an example but not found any and in my mind I think it should be pretty easy since it's a very basic requirement. 我已经搜索了一个示例,但没有找到任何示例,我认为这应该很容易,因为这是一个非常基本的要求。

Do you know about any example with JMS and HTML display via WebSocket that you can share or can give some hints or help for me to solve it? 您是否知道任何可以通过WebSocket共享的JMS和HTML显示示例,或者可以为我提供一些提示或帮助来解决该示例?

The Spring Integration comes to the rescue. Spring Integration可以提供帮助。

You can write <int-jms:message-driven-channel-adapter> to read messages from JMS queue and forward them to the <int-websocket:outbound-channel-adapter> . 您可以编写<int-jms:message-driven-channel-adapter>来从JMS队列中读取消息,并将其转发到<int-websocket:outbound-channel-adapter> Where the last one just sends messages to the connected WebSocket session(s). 最后一个只是向连接的WebSocket会话发送消息的地方。

See these Spring Integration samples on the matter: 有关此问题,请参见这些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/jms

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

UPDATE UPDATE

To send the message to all subscribed WebSocket session you should do something like this: 要将消息发送到所有订阅的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>

With this Groovy script I retrieve session ids from the serverWebSocketContainer (all those connected clients), iterate over them to build messages to send them over their websocket. 使用此Groovy脚本,我从serverWebSocketContainer (所有那些连接的客户端)中检索session ids ,对其进行迭代以构建消息以通过WebSocket发送它们。 And split finally, to send to the <int-websocket:outbound-channel-adapter> one by one. 最后split ,一一发送到<int-websocket:outbound-channel-adapter>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM