I have a Spring web app with Stomp WS, Spring v. 6.0.9, Windows environment, Tomcat 10.1, JRE 17, I'm currently using SimpleBroker but I need to clusterize the server and then I have to move to an external AMQP server, I deced to use RabbitMQ and Spring StompBrokerRelay.
This is the steps I did:
1) Change the MessageBroker config to StompBrokerRelay
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/topic");
registry.enableStompBrokerRelay("/topic")
.setRelayHost("localhost")
.setRelayPort(61613)
.setClientLogin("guest")
.setClientPasscode("guest")
.setAutoStartup(true);
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/myws").setAllowedOriginPatterns("*");
}
}
2) Install RabbitMQ and Stomp plugin
I'm sure it is listening because I found this ports on rabbitmq management page:
3) Finally I tried to run server and client
The js client code was not changed from previous simplebroker version.
Server side I see this logs:
[DEBUG] 2023-05-30 12:41:50.499 [clientOutboundChannel-11] NativeWebSocketSession - Closing StandardWebSocketSession[id=113fcf11-773c-8eaf-8fb9-eec01b0204f3, uri=ws://127.0.0.1:8080/myws]
[DEBUG] 2023-05-30 12:41:50.500 [clientOutboundChannel-11] LoggingWebSocketHandlerDecorator - StandardWebSocketSession[id=113fcf11-773c-8eaf-8fb9-eec01b0204f3, uri=ws://127.0.0.1:8080/myws] closed with CloseStatus[code=1002, reason=null]
[DEBUG] 2023-05-30 12:41:50.500 [clientOutboundChannel-11] SubProtocolWebSocketHandler - Clearing session 113fcf11-773c-8eaf-8fb9-eec01b0204f3
[DEBUG] 2023-05-30 12:41:50.501 [clientOutboundChannel-12] SubProtocolWebSocketHandler - No session for GenericMessage [payload=byte[0], headers={simpMessageType=OTHER, stompCommand=ERROR, nativeHeaders={message=[Broker not available.]}, simpSessionId=113fcf11-773c-8eaf-8fb9-eec01b0204f3}]
[DEBUG] 2023-05-30 12:41:50.501 [http-nio-8080-exec-8] DispatcherServlet - GET "/myws", parameters={}
[DEBUG] 2023-05-30 12:41:50.502 [http-nio-8080-exec-8] WebSocketHandlerMapping - Mapped to org.springframework.web.socket.server.support.WebSocketHttpRequestHandler@16e9324
[DEBUG] 2023-05-30 12:41:50.502 [http-nio-8080-exec-8] WebSocketHttpRequestHandler - GET /myws
[DEBUG] 2023-05-30 12:41:50.503 [http-nio-8080-exec-8] DispatcherServlet - Completed 101 SWITCHING_PROTOCOLS
[DEBUG] 2023-05-30 12:41:50.504 [http-nio-8080-exec-8] LoggingWebSocketHandlerDecorator - New StandardWebSocketSession[id=4d7e4bd4-e3dc-031f-177a-55a13fc27b2b, uri=ws://127.0.0.1:8080/myws]
In the client log I see HTTP 101 request with this response:
In rabbitMQ management I don't see any connection.
An interesting tip I got from StompBrokerRelayMessageHandler logs, is that the "system" connection to Rabbit seems to continously be opened and closed.
2023-06-01 15:02:36.156 DEBUG 15784 --- [ent-scheduler-2] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:36.190 TRACE 15784 --- [ent-scheduler-2] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:36.235 DEBUG 15784 --- [ent-scheduler-3] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:36.235 DEBUG 15784 --- [ent-scheduler-3] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
2023-06-01 15:02:40.654 DEBUG 15784 --- [ent-scheduler-5] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:40.657 TRACE 15784 --- [ent-scheduler-5] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:40.667 DEBUG 15784 --- [ent-scheduler-6] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:40.667 DEBUG 15784 --- [ent-scheduler-6] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
2023-06-01 15:02:41.248 DEBUG 15784 --- [ent-scheduler-8] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:41.250 TRACE 15784 --- [ent-scheduler-8] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:41.255 DEBUG 15784 --- [ent-scheduler-9] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:41.255 DEBUG 15784 --- [ent-scheduler-9] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
2023-06-01 15:02:45.690 DEBUG 15784 --- [nt-scheduler-11] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:45.694 TRACE 15784 --- [nt-scheduler-11] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:45.706 DEBUG 15784 --- [nt-scheduler-12] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:45.706 DEBUG 15784 --- [nt-scheduler-12] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
2023-06-01 15:02:46.262 DEBUG 15784 --- [ent-scheduler-2] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:46.263 TRACE 15784 --- [ent-scheduler-2] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:46.268 DEBUG 15784 --- [ent-scheduler-3] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:46.269 DEBUG 15784 --- [ent-scheduler-3] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
2023-06-01 15:02:50.711 DEBUG 15784 --- [ent-scheduler-5] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:50.711 TRACE 15784 --- [ent-scheduler-5] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:50.716 DEBUG 15784 --- [ent-scheduler-6] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:50.717 DEBUG 15784 --- [ent-scheduler-6] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
2023-06-01 15:02:51.273 DEBUG 15784 --- [ent-scheduler-8] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection opened in session=_system_
2023-06-01 15:02:51.274 TRACE 15784 --- [ent-scheduler-8] o.s.messaging.simp.stomp.StompEncoder : Encoding STOMP CONNECT, headers={accept-version=[1.1,1.2], login=[guest], passcode=[PROTECTED], heart-beat=[10000,10000]}
2023-06-01 15:02:51.278 DEBUG 15784 --- [ent-scheduler-9] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection to broker closed in session _system_
2023-06-01 15:02:51.279 DEBUG 15784 --- [ent-scheduler-9] o.s.m.s.s.StompBrokerRelayMessageHandler : Cleaning up connection state for session _system_
To be sure this is not something related to my application, I also checked out this demo project: https://github.com/djselzlein/spring-websockets-rabbitmq-demo/ But it has the same problem.
Can you help to understand what I'm missing?
A fresh, updated, installation of RabbitMQ on windows has solved my issue. It was not related to Spring.