Class AdhocDispatchInterceptor

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

public class AdhocDispatchInterceptor extends Object implements DispatchInterceptor
A 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 via DisableMetrics 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 calling FluxCapacitorBuilder.disableAdhocDispatchInterceptor() during Flux client setup. This is useful in highly constrained environments or when performance is critical.
See Also:
  • Constructor Details

    • AdhocDispatchInterceptor

      public AdhocDispatchInterceptor()
  • Method Details

    • getAdhocInterceptor

      public static Optional<? extends DispatchInterceptor> getAdhocInterceptor(MessageType messageType)
      Returns the current thread-local ad hoc interceptor for the given MessageType, 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 given Callable 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 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 given Runnable 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 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

      public Message interceptDispatch(Message message, MessageType messageType, String topic)
      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 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 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 interface DispatchInterceptor
      Parameters:
      serializedMessage - the serialized form of the message
      message - the deserialized message object
      messageType - the message type
      topic - the target topic
      Returns:
      the modified or original SerializedMessage
    • monitorDispatch

      public void monitorDispatch(Message message, MessageType messageType, String topic)
      Optionally monitors a dispatched message using any registered ad hoc interceptor for the current thread.
      Specified by:
      monitorDispatch in interface DispatchInterceptor
      Parameters:
      message - the final message about to be handled or published
      messageType - the type of the message
      topic - the topic to which the message is dispatched (can be null)