Class MessageRoutingInterceptor

java.lang.Object
io.fluxzero.sdk.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.

Fluxzero Runtime 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 Fluxzero Runtime.

See Also:
  • Constructor Details

    • MessageRoutingInterceptor

      public MessageRoutingInterceptor()
  • Method Details

    • prepareLocalDispatch

      public PreparedLocalDispatch prepareLocalDispatch(LocalDispatchDescriptor descriptor)
      Description copied from interface: DispatchInterceptor
      Prepares this interceptor for repeated local dispatches with the same static message characteristics.

      This optional method lets local handling apply an interceptor without creating a complete Message first. Implementations must preserve the observable behavior of DispatchInterceptor.interceptDispatch(Message, MessageType, String). Return null when that is not possible; Fluxzero will then use the regular dispatch path for this payload type.

      The returned policy may be cached and used concurrently. State belonging to a single dispatch must be stored in the supplied LocalExecution, not in the policy itself.

      Specified by:
      prepareLocalDispatch in interface DispatchInterceptor
      Parameters:
      descriptor - payload class, message type, and topic shared by the local dispatches
      Returns:
      a thread-safe prepared policy, or null to use regular message-based dispatch
    • 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