We have a requirement to evenly distribute the load say if we have two stomp brokers both should share the load. We have created a RoundRobin to dynamically give a different host name each time. It is working fine when a websocket client tries to connect to StompBrokerRelay. But when the application tries to send message to the broker or to the client(using convertAndSendToUser()) it is always taking the first tcpClient hostname which is registered. Below I have added the code snippet. From the websocket docs i could read system connection comes in when a application tries to send messages to the stomp broker. Can't we override hostname here as we did for websocket connection? Or am I missing something here. Could you please advice.

public WebSocketConfig() {
    List<String> hostArray = new ArrayList();
    String amqStompUrl = config.getString(AMQ_STOMP_URL).orElse(LOCALHOST);
    Stream.of(amqStompUrl.split(COMMA_SEPARATE)).forEach(hostArray::add);
    stringHostRoundRobin = new HostRoundRobin(hostArray);
}

configureMessageBroker:

brokerRegistry.enableStompBrokerRelay(DESTINATION_PREFIX)
          .setUserDestinationBroadcast(UN_RESOLVED_USER_DESTINATION)
          .setUserRegistryBroadcast("/topic/simp-user-registry")
          .setClientLogin(clientLogin)
          .setClientPasscode(clientPasscode)
          .setSystemLogin(systemLogin)
          .setSystemPasscode(systemPasscode)
          .setTcpClient(createTcpClient());

Supplier<SocketAddress> socketAddressSupplier() {
    int amqStompPort = config.getInt(AMQ_STOMP_PORT).orElse(AMQ_STOMP_PORT_DEFAULT);
    String nextHost = getNextHostName();
    Supplier<SocketAddress> socketAddressSupplier = () -> new InetSocketAddress(nextHost, amqStompPort);
    return socketAddressSupplier;
}

private String getNextHostName() {
    return stringHostRoundRobin.iterator().next();
}

private ReactorNettyTcpClient<byte[]> createTcpClient() {
    return new ReactorNettyTcpClient<>(client -> client.addressSupplier(() -> socketAddressSupplier().get()), new StompReactorNettyCodec());
}

Comment From: rstoyanchev

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Comment From: rstoyanchev

The system connection is established once on startup and it is maintained open until the application shuts down. In other words the connection is not re-established on every message sent through the SimpMessagingTemplate. The only time the system connection may be re-established is if the connection is lost due to a network issue, broker restart, etc. So this is expected behavior.

Comment From: sharmilay2k

Thanks for the reply @rstoyanchev . Is there any work around to achieve this?

Comment From: rstoyanchev

No, the StompBrokerRelayMessageHandler supports one "system" connection only for broadcasting messages from the server side. Short of setting up a broker cluster, the only other thing you can do for individual WebSocket clients.