Class MessageRoutingInterceptor
- All Implemented Interfaces:
DispatchInterceptor
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()isnull, the interceptor computes the segment from the message's routing key viaHasMessage.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:
-
Field Summary
Fields inherited from interface DispatchInterceptor
defaultInterceptors, noOp -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioninterceptDispatch(Message message, MessageType messageType, String topic) Returns the unmodifiedMessageas this interceptor only modifies the serialized form.modifySerializedMessage(SerializedMessage serializedMessage, Message m, MessageType messageType, String topic) Computes and sets the routing segment on the serialized message if not already present.prepareLocalDispatch(LocalDispatchDescriptor descriptor) Prepares this interceptor for repeated local dispatches with the same static message characteristics.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DispatchInterceptor
andThen, interceptDispatch, monitorDispatch, withNamespace
-
Constructor Details
-
MessageRoutingInterceptor
public MessageRoutingInterceptor()
-
-
Method Details
-
prepareLocalDispatch
Description copied from interface:DispatchInterceptorPrepares 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
Messagefirst. Implementations must preserve the observable behavior ofDispatchInterceptor.interceptDispatch(Message, MessageType, String). Returnnullwhen 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:
prepareLocalDispatchin interfaceDispatchInterceptor- Parameters:
descriptor- payload class, message type, and topic shared by the local dispatches- Returns:
- a thread-safe prepared policy, or
nullto use regular message-based dispatch
-
interceptDispatch
Returns the unmodifiedMessageas this interceptor only modifies the serialized form.- Specified by:
interceptDispatchin interfaceDispatchInterceptor- Parameters:
message- the message to be dispatchedmessageType- 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
nullto 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:
modifySerializedMessagein interfaceDispatchInterceptor- Parameters:
serializedMessage- the message to be sentm- the original message objectmessageType- the type of message (e.g., command, event, query)topic- the topic to which the message will be published- Returns:
- the same
SerializedMessageinstance, possibly updated with a segment
-