Class AdhocDispatchInterceptor
- All Implemented Interfaces:
DispatchInterceptor
DispatchInterceptor that enables thread-local, dynamically scoped interceptors during message dispatch.
This class allows you to temporarily override dispatch behavior for one or more MessageTypes within
a specific scope (such as during the invocation of a handler or message batch). It is particularly useful for use
cases where message output needs to be suppressed, enriched, rerouted, or filtered conditionally.
Adhoc interceptors are applied using
runWithAdhocInterceptor(Runnable, DispatchInterceptor, MessageType...) or
runWithAdhocInterceptor(Callable, DispatchInterceptor, MessageType...).
Typical Use Case: Disabling Metrics
A common use case is disabling outbound metric messages for specific message consumers viaDisableMetrics
which internally uses this behavior:
@Consumer(batchInterceptors = DisableMetrics.class)
public class OrderHandler {
@HandleEvent
public void on(OrderPlaced event) {
// This handler will execute with METRICS messages disabled
}
}
Disabling All Adhoc Interceptors
This behavior can be disabled globally by callingFluxzeroBuilder.disableAdhocDispatchInterceptor() during Fluxzero
client setup. This is useful in highly constrained environments or when performance is critical.- See Also:
-
Field Summary
Fields inherited from interface DispatchInterceptor
noOp -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<? extends DispatchInterceptor> getAdhocInterceptor(MessageType messageType) Returns the current thread-local ad hoc interceptor for the givenMessageType, if present.interceptDispatch(Message message, MessageType messageType, String topic) Intercepts a message before dispatch for the given message type and topic.modifySerializedMessage(SerializedMessage serializedMessage, Message message, MessageType messageType, String topic) Optionally modifies the serialized message before dispatch, delegating to any registered ad hoc interceptor for the current thread.voidmonitorDispatch(Message message, MessageType messageType, String topic, String namespace) Optionally monitors a dispatched message using any registered ad hoc interceptor for the current thread.static voidrunWithAdhocInterceptor(Runnable task, DispatchInterceptor adhocInterceptor, MessageType... messageTypes) Executes the givenRunnablewhile temporarily enabling the provided interceptor for the specified message types.static <T> TrunWithAdhocInterceptor(Callable<T> task, DispatchInterceptor adhocInterceptor, MessageType... messageTypes) Executes the givenCallablewhile temporarily enabling the provided interceptor for the specified message types.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DispatchInterceptor
andThen
-
Constructor Details
-
AdhocDispatchInterceptor
public AdhocDispatchInterceptor()
-
-
Method Details
-
getAdhocInterceptor
Returns the current thread-local ad hoc interceptor for the givenMessageType, if present.- Parameters:
messageType- The message type to look up.- Returns:
- An optional interceptor for the specified message type.
-
runWithAdhocInterceptor
public static <T> T runWithAdhocInterceptor(Callable<T> task, DispatchInterceptor adhocInterceptor, MessageType... messageTypes) Executes the givenCallablewhile temporarily enabling the provided interceptor for the specified message types.After the task completes (or throws), the previous interceptors are automatically restored. If no message types are specified, the interceptor is applied to all
MessageTypes.- Type Parameters:
T- The return type of the task.- Parameters:
task- The task to run.adhocInterceptor- The interceptor to apply during execution.messageTypes- The message types for which to apply the interceptor. If empty, all types are used.- Returns:
- The result from the task.
-
runWithAdhocInterceptor
public static void runWithAdhocInterceptor(Runnable task, DispatchInterceptor adhocInterceptor, MessageType... messageTypes) Executes the givenRunnablewhile temporarily enabling the provided interceptor for the specified message types.After the task completes (or throws), the previous interceptors are automatically restored. If no message types are specified, the interceptor is applied to all
MessageTypes.- Parameters:
task- The task to run.adhocInterceptor- The interceptor to apply during execution.messageTypes- The message types for which to apply the interceptor. If empty, all types are used.
-
interceptDispatch
Intercepts a message before dispatch for the given message type and topic. Delegates to any registered ad hoc interceptor for the current thread.- 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 message, MessageType messageType, String topic) Optionally modifies the serialized message before dispatch, delegating to any registered ad hoc interceptor for the current thread.- Specified by:
modifySerializedMessagein interfaceDispatchInterceptor- Parameters:
serializedMessage- the serialized form of the messagemessage- the deserialized message objectmessageType- the message typetopic- the target topic- Returns:
- the modified or original
SerializedMessage
-
monitorDispatch
public void monitorDispatch(Message message, MessageType messageType, String topic, String namespace) Optionally monitors a dispatched message using any registered ad hoc interceptor for the current thread.- Specified by:
monitorDispatchin interfaceDispatchInterceptor- Parameters:
message- the final message about to be handled or publishedmessageType- the type of the messagetopic- the topic to which the message is dispatched (can be null)namespace- the namespace to which the message is dispatched (can be null)
-