org.springframework.messaging.simp.stomp

## Class StompBrokerRelayMessageHandler

- java.lang.Object
- - org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler
  - - org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler

- All Implemented Interfaces:
    Aware, ApplicationEventPublisherAware, Lifecycle, Phased, SmartLifecycle, MessageHandler

      public class StompBrokerRelayMessageHandler
      extends AbstractBrokerMessageHandler

  A `MessageHandler` that handles messages by forwarding them to a STOMP broker.

  For each new `CONNECT` message, an independent TCP connection to the broker is opened and used exclusively for all messages from the client that originated the CONNECT message. Messages from the same client are identified through the session id message header. Reversely, when the STOMP broker sends messages back on the TCP connection, those messages are enriched with the session id of the client and sent back downstream through the `MessageChannel` provided to the constructor.

  This class also automatically opens a default "system" TCP connection to the message broker that is used for sending messages that originate from the server application (as opposed to from a client). Such messages are not associated with any client and therefore do not have a session id header. The "system" connection is effectively shared and cannot be used to receive messages. Several properties are provided to configure the "system" connection including:

  - `setSystemLogin(java.lang.String)`
  - `setSystemPasscode(java.lang.String)`
  - `setSystemHeartbeatSendInterval(long)`
  - `setSystemHeartbeatReceiveInterval(long)`
  Since:
    4.0
  Author:
    Rossen Stoyanchev, Andy Wilkinson

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `StompBrokerRelayMessageHandler.Stats` Contract for access to session counters.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `SYSTEM_SESSION_ID` The system session ID.

    - ### Fields inherited from class org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler

      `logger`

    - ### Fields inherited from interface org.springframework.context.SmartLifecycle

      `DEFAULT_PHASE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `StompBrokerRelayMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel, Collection<String> destinationPrefixes)` Create a StompBrokerRelayMessageHandler instance with the given message channels and destination prefixes.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `getClientLogin()` Return the configured login to use for connections to the STOMP broker on behalf of connected clients.
    `String` | `getClientPasscode()` Return the configured passcode to use for connections to the STOMP broker on behalf of connected clients.
    `int` | `getConnectionCount()` Return the current count of TCP connection to the broker.
    `MessageHeaderInitializer` | `getHeaderInitializer()` Return the configured header initializer.
    `String` | `getRelayHost()` Return the STOMP message broker host.
    `int` | `getRelayPort()` Return the STOMP message broker port.
    `StompBrokerRelayMessageHandler.Stats` | `getStats()` Return a structured object with internal state and counters.
    `String` | `getStatsInfo()` Return a String describing internal state and counters.
    `long` | `getSystemHeartbeatReceiveInterval()` Return the interval, in milliseconds, at which the "system" connection expects to receive heartbeats from the STOMP broker.
    `long` | `getSystemHeartbeatSendInterval()` Return the interval, in milliseconds, at which the "system" connection will send heartbeats to the STOMP broker.
    `String` | `getSystemLogin()` Return the login used for the shared "system" connection to the STOMP broker.
    `String` | `getSystemPasscode()` Return the passcode used for the shared "system" connection to the STOMP broker.
    `Map<String,MessageHandler>` | `getSystemSubscriptions()` Return the configured map with subscriptions on the "system" connection.
    `TaskScheduler` | `getTaskScheduler()`
    `TcpOperations<byte[]>` | `getTcpClient()` Get the configured TCP client (never `null` unless not configured invoked and this method is invoked before the handler is started and hence a default implementation initialized).
    `String` | `getVirtualHost()` Return the configured virtual host value.
    `protected void` | `handleMessageInternal(Message<?> message)`
    `void` | `setClientLogin(String clientLogin)` Set the login to use when creating connections to the STOMP broker on behalf of connected clients.
    `void` | `setClientPasscode(String clientPasscode)` Set the client passcode to use to create connections to the STOMP broker on behalf of connected clients.
    `void` | `setHeaderInitializer(MessageHeaderInitializer headerInitializer)` Configure a `MessageHeaderInitializer` to apply to the headers of all messages created through the `StompBrokerRelayMessageHandler` that are sent to the client outbound message channel.
    `void` | `setRelayHost(String relayHost)` Set the STOMP message broker host.
    `void` | `setRelayPort(int relayPort)` Set the STOMP message broker port.
    `void` | `setSystemHeartbeatReceiveInterval(long heartbeatReceiveInterval)` Set the maximum interval, in milliseconds, at which the "system" connection expects, in the absence of any other data, to receive a heartbeat from the STOMP broker.
    `void` | `setSystemHeartbeatSendInterval(long systemHeartbeatSendInterval)` Set the interval, in milliseconds, at which the "system" connection will, in the absence of any other data being sent, send a heartbeat to the STOMP broker.
    `void` | `setSystemLogin(String systemLogin)` Set the login for the shared "system" connection used to send messages to the STOMP broker from within the application, i.e.
    `void` | `setSystemPasscode(String systemPasscode)` Set the passcode for the shared "system" connection used to send messages to the STOMP broker from within the application, i.e.
    `void` | `setSystemSubscriptions(Map<String,MessageHandler> subscriptions)` Configure one more destinations to subscribe to on the shared "system" connection along with MessageHandler's to handle received messages.
    `void` | `setTaskScheduler(TaskScheduler taskScheduler)` Configure the `TaskScheduler` to use to reset client-to-broker message count in the current heartbeat period.
    `void` | `setTcpClient(TcpOperations<byte[]> tcpClient)` Configure a TCP client for managing TCP connections to the STOMP broker.
    `void` | `setVirtualHost(String virtualHost)` Set the value of the "host" header to use in STOMP CONNECT frames.
    `protected void` | `startInternal()`
    `protected void` | `stopInternal()`
    `String` | `toString()`

    - ### Methods inherited from class org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler

      `checkDestinationPrefix, getApplicationEventPublisher, getBrokerChannel, getClientInboundChannel, getClientOutboundChannel, getClientOutboundChannelForSession, getDestinationPrefixes, handleMessage, isAutoStartup, isBrokerAvailable, isPreservePublishOrder, isRunning, publishBrokerAvailableEvent, publishBrokerUnavailableEvent, setApplicationEventPublisher, setAutoStartup, setPreservePublishOrder, setUserDestinationPredicate, start, stop, stop`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.context.SmartLifecycle

      `getPhase`

- - ### Field Detail

    - #### SYSTEM_SESSION_ID

          public static final String SYSTEM_SESSION_ID

      The system session ID.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### StompBrokerRelayMessageHandler

          public StompBrokerRelayMessageHandler(SubscribableChannel inboundChannel,
                                                MessageChannel outboundChannel,
                                                SubscribableChannel brokerChannel,
                                                Collection<String> destinationPrefixes)

      Create a StompBrokerRelayMessageHandler instance with the given message channels and destination prefixes.
      Parameters:
        `inboundChannel` - the channel for receiving messages from clients (e.g. WebSocket clients)
        `outboundChannel` - the channel for sending messages to clients (e.g. WebSocket clients)
        `brokerChannel` - the channel for the application to send messages to the broker
        `destinationPrefixes` - the broker supported destination prefixes; destinations that do not match the given prefix are ignored.

  - ### Method Detail

    - #### setRelayHost

          public void setRelayHost(String relayHost)

      Set the STOMP message broker host.

    - #### getRelayHost

          public String getRelayHost()

      Return the STOMP message broker host.

    - #### setRelayPort

          public void setRelayPort(int relayPort)

      Set the STOMP message broker port.

    - #### getRelayPort

          public int getRelayPort()

      Return the STOMP message broker port.

    - #### setClientLogin

          public void setClientLogin(String clientLogin)

      Set the login to use when creating connections to the STOMP broker on behalf of connected clients.

      By default this is set to "guest".

      See Also:
        `setSystemLogin(String)`

    - #### getClientLogin

          public String getClientLogin()

      Return the configured login to use for connections to the STOMP broker on behalf of connected clients.
      See Also:
        `getSystemLogin()`

    - #### setClientPasscode

          public void setClientPasscode(String clientPasscode)

      Set the client passcode to use to create connections to the STOMP broker on behalf of connected clients.

      By default this is set to "guest".

      See Also:
        `setSystemPasscode(java.lang.String)`

    - #### getClientPasscode

          public String getClientPasscode()

      Return the configured passcode to use for connections to the STOMP broker on behalf of connected clients.
      See Also:
        `getSystemPasscode()`

    - #### setSystemLogin

          public void setSystemLogin(String systemLogin)

      Set the login for the shared "system" connection used to send messages to the STOMP broker from within the application, i.e. messages not associated with a specific client session (e.g. REST/HTTP request handling method).

      By default this is set to "guest".

    - #### getSystemLogin

          public String getSystemLogin()

      Return the login used for the shared "system" connection to the STOMP broker.

    - #### setSystemPasscode

          public void setSystemPasscode(String systemPasscode)

      Set the passcode for the shared "system" connection used to send messages to the STOMP broker from within the application, i.e. messages not associated with a specific client session (e.g. REST/HTTP request handling method).

      By default this is set to "guest".

    - #### getSystemPasscode

          public String getSystemPasscode()

      Return the passcode used for the shared "system" connection to the STOMP broker.

    - #### setSystemHeartbeatSendInterval

          public void setSystemHeartbeatSendInterval(long systemHeartbeatSendInterval)

      Set the interval, in milliseconds, at which the "system" connection will, in the absence of any other data being sent, send a heartbeat to the STOMP broker. A value of zero will prevent heartbeats from being sent to the broker.

      The default value is 10000.

      See class-level documentation for more information on the "system" connection.

    - #### getSystemHeartbeatSendInterval

          public long getSystemHeartbeatSendInterval()

      Return the interval, in milliseconds, at which the "system" connection will send heartbeats to the STOMP broker.

    - #### setSystemHeartbeatReceiveInterval

          public void setSystemHeartbeatReceiveInterval(long heartbeatReceiveInterval)

      Set the maximum interval, in milliseconds, at which the "system" connection expects, in the absence of any other data, to receive a heartbeat from the STOMP broker. A value of zero will configure the connection to expect not to receive heartbeats from the broker.

      The default value is 10000.

      See class-level documentation for more information on the "system" connection.

    - #### getSystemHeartbeatReceiveInterval

          public long getSystemHeartbeatReceiveInterval()

      Return the interval, in milliseconds, at which the "system" connection expects to receive heartbeats from the STOMP broker.

    - #### setSystemSubscriptions

          public void setSystemSubscriptions(@Nullable
                                             Map<String,MessageHandler> subscriptions)

      Configure one more destinations to subscribe to on the shared "system" connection along with MessageHandler's to handle received messages.

      This is for internal use in a multi-application server scenario where servers forward messages to each other (e.g. unresolved user destinations).

      Parameters:
        `subscriptions` - the destinations to subscribe to.

    - #### getSystemSubscriptions

          public Map<String,MessageHandler> getSystemSubscriptions()

      Return the configured map with subscriptions on the "system" connection.

    - #### setVirtualHost

          public void setVirtualHost(@Nullable
                                     String virtualHost)

      Set the value of the "host" header to use in STOMP CONNECT frames. When this property is configured, a "host" header will be added to every STOMP frame sent to the STOMP broker. This may be useful for example in a cloud environment where the actual host to which the TCP connection is established is different from the host providing the cloud-based STOMP service.

      By default this property is not set.

    - #### getVirtualHost

          @Nullable
          public String getVirtualHost()

      Return the configured virtual host value.

    - #### setTcpClient

          public void setTcpClient(@Nullable
                                   TcpOperations<byte[]> tcpClient)

      Configure a TCP client for managing TCP connections to the STOMP broker.

      By default `ReactorNettyTcpClient` is used.

      Note: when this property is used, any `host` or `port` specified are effectively ignored.

    - #### getTcpClient

          @Nullable
          public TcpOperations<byte[]> getTcpClient()

      Get the configured TCP client (never `null` unless not configured invoked and this method is invoked before the handler is started and hence a default implementation initialized).

    - #### setHeaderInitializer

          public void setHeaderInitializer(@Nullable
                                           MessageHeaderInitializer headerInitializer)

      Configure a `MessageHeaderInitializer` to apply to the headers of all messages created through the `StompBrokerRelayMessageHandler` that are sent to the client outbound message channel.

      By default this property is not set.

    - #### getHeaderInitializer

          @Nullable
          public MessageHeaderInitializer getHeaderInitializer()

      Return the configured header initializer.

    - #### getStatsInfo

          public String getStatsInfo()

      Return a String describing internal state and counters. Effectively `toString()` on `getStats()`.

    - #### getStats

          public StompBrokerRelayMessageHandler.Stats getStats()

      Return a structured object with internal state and counters.
      Since:
        5.2

    - #### getConnectionCount

          public int getConnectionCount()

      Return the current count of TCP connection to the broker.

    - #### setTaskScheduler

          public void setTaskScheduler(@Nullable
                                       TaskScheduler taskScheduler)

      Configure the `TaskScheduler` to use to reset client-to-broker message count in the current heartbeat period. For more details, see `StompBrokerRelayRegistration.setTaskScheduler(TaskScheduler)`.
      Parameters:
        `taskScheduler` - the scheduler to use
      Since:
        5.3

    - #### getTaskScheduler

          @Nullable
          public TaskScheduler getTaskScheduler()

    - #### startInternal

          protected void startInternal()

      Overrides:
        `startInternal` in class `AbstractBrokerMessageHandler`

    - #### stopInternal

          protected void stopInternal()

      Overrides:
        `stopInternal` in class `AbstractBrokerMessageHandler`

    - #### handleMessageInternal

          protected void handleMessageInternal(Message<?> message)

      Specified by:
        `handleMessageInternal` in class `AbstractBrokerMessageHandler`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`