Class MessageRoutingInterceptor

java.lang.Object
io.fluxcapacitor.javaclient.publishing.routing.MessageRoutingInterceptor
All Implemented Interfaces:
DispatchInterceptor

public class MessageRoutingInterceptor extends Object implements DispatchInterceptor
A DispatchInterceptor that assigns a routing segment to messages prior to dispatch.

This interceptor computes a consistent hash-based segment index for each message using ConsistentHashing.computeSegment(String) and injects it into the serialized message if no segment has already been set.

Flux Platform uses the segment value for consistent message routing and load distribution. It ensures that all messages with the same routing key are handled by the same segment, preserving message affinity and ordering guarantees within that segment.

Behavior

  • If SerializedMessage#getSegment() is null, the interceptor computes the segment from the message's routing key via HasMessage.computeRoutingKey().
  • If a segment is already assigned, the interceptor leaves it unchanged.
  • The message content itself is not modified; only the serialized representation is updated.

This interceptor is typically enabled by default for most gateway clients (e.g., command, event, query, etc.), ensuring that routing behavior is applied uniformly before messages are published to Flux Platform.

See Also:
  • Constructor Details

    • MessageRoutingInterceptor

      public MessageRoutingInterceptor()
  • Method Details

    • interceptDispatch

      public Message interceptDispatch(Message message, MessageType messageType, String topic)
      Returns the unmodified Message as this interceptor only modifies the serialized form.
      Specified by:
      interceptDispatch in interface DispatchInterceptor
      Parameters:
      message - the message to be dispatched
      messageType - the type of the message (e.g., COMMAND, EVENT, etc.)
      topic - the target topic or null if not applicable
      Returns:
      the modified message, the same message, or null to prevent dispatch
    • modifySerializedMessage

      public SerializedMessage modifySerializedMessage(SerializedMessage serializedMessage, Message m, MessageType messageType, String topic)
      Computes and sets the routing segment on the serialized message if not already present.
      Specified by:
      modifySerializedMessage in interface DispatchInterceptor
      Parameters:
      serializedMessage - the message to be sent
      m - the original message object
      messageType - the type of message (e.g., command, event, query)
      topic - the topic to which the message will be published
      Returns:
      the same SerializedMessage instance, possibly updated with a segment