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 MessageType
s 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 callingFluxCapacitorBuilder.disableAdhocDispatchInterceptor()
during Flux
client setup. This is useful in highly constrained environments or when performance is critical.- See Also:
-
Field Summary
Fields inherited from interface io.fluxcapacitor.javaclient.publishing.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.void
monitorDispatch
(Message message, MessageType messageType, String topic) Optionally monitors a dispatched message using any registered ad hoc interceptor for the current thread.static void
runWithAdhocInterceptor
(Runnable task, DispatchInterceptor adhocInterceptor, MessageType... messageTypes) Executes the givenRunnable
while temporarily enabling the provided interceptor for the specified message types.static <T> T
runWithAdhocInterceptor
(Callable<T> task, DispatchInterceptor adhocInterceptor, MessageType... messageTypes) Executes the givenCallable
while temporarily enabling the provided interceptor for the specified message types.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.publishing.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 givenCallable
while 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
MessageType
s.- 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 givenRunnable
while 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
MessageType
s.- 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:
interceptDispatch
in 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
null
to 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:
modifySerializedMessage
in 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
Optionally monitors a dispatched message using any registered ad hoc interceptor for the current thread.- Specified by:
monitorDispatch
in 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)
-