简体   繁体   中英

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.

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.

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?

The Spring Integration comes to the rescue.

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> . Where the last one just sends messages to the connected WebSocket session(s).

See these Spring Integration samples on the matter:

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

UPDATE

To send the message to all subscribed WebSocket session you should do something like this:

<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. And split finally, to send to the <int-websocket:outbound-channel-adapter> one by one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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