Raphael do Vale
Raphael do Vale

Reputation: 961

Spring Websocket with Stomp full featured broker and failover

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?

Upvotes: 18

Views: 1420

Answers (1)

lucifer
lucifer

Reputation: 435

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.

Upvotes: 1

Related Questions