Spring Websocket with Stomp full featured broker and failover
Asked Answered
B

1

18

I have this configuration for Spring and a full feature stomp broker (ActiveMQ):

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    private static Logger LOG = org.slf4j.LoggerFactory.getLogger(WebsocketConfig.class);

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableStompBrokerRelay("/topic/", "/queue/");
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/socket").withSockJS();
    }
}

I thought Spring would use my current ActiveMQ configuration, but in reality it tries to connect into a server located in localhost with a default stomp port. I discovered that is possible to change this configuration by typing:

config.enableStompBrokerRelay("/topic/", "/queue/")
            .setRelayHost("activeMQHOST")
            .setRelayPort(9999);

Thats fine, but currently I have a failover setup with two brokers. How can I configure such setup for the stomp broker relay?

If not possible, I thought in the following solutions:

  1. Use the simple (in memory) broker, which isn't advisable.
  2. The ActiveMQ is used for several operations (not restricted to WebSockets), and I don't know if it is recommended to mix Stomp/WebSockets queues with other feature's queues. Thinking on it, I can create another ActiveMQ instance (maybe using the VM transport).

The second option is advisable?

Brozak answered 7/10, 2016 at 18:36 Comment(1)
Did the answer here address your question? This is the highest voted question tagged with activemq that does not have a "correct" answer. It would be good to mark the current answer as correct if it actually is to help other users who have this same problem. If it's not correct it would be helpful to explain why. Thanks!Telegonus
A
0

Here's an example configuration for a failover setup with two brokers:

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    private static Logger LOG = org.slf4j.LoggerFactory.getLogger(WebsocketConfig.class);

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        StompBrokerRelayRegistration relayRegistration = config.enableStompBrokerRelay("/topic/", "/queue/");
        relayRegistration.setRelayHost("tcp://activeMQHOST1:61616,tcp://activeMQHOST2:61616");
        relayRegistration.setClientLogin("USERNAME");
        relayRegistration.setClientPasscode("PASSWORD");
        relayRegistration.setSystemLogin("USERNAME");
        relayRegistration.setSystemPasscode("PASSWORD");
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/socket").withSockJS();
    }
}

Here you can use setRelayHost method takes a comma-separated list of the two broker's addresses, and the setClientLogin, setClientPasscode, setSystemLogin, and setSystemPasscode methods specify the login credentials for the clients and the system.

Aril answered 30/4, 2023 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.