Class AdhocDispatchInterceptor

java.lang.Object
io.fluxzero.sdk.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 FluxzeroBuilder.disableAdhocDispatchInterceptor() during Fluxzero client setup. This is useful in highly constrained environments or when performance is critical.
See Also:
  • Constructor Details

    • AdhocDispatchInterceptor

      public AdhocDispatchInterceptor()
  • 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
    • 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.
    • captureAdhocInterceptors

      public static Map<MessageType, DispatchInterceptor> captureAdhocInterceptors()
      Captures the current thread-local ad hoc interceptors so they can be restored on another worker thread.
      Returns:
      immutable snapshot of the current ad hoc interceptors
    • runWithAdhocInterceptors

      public static <T> T runWithAdhocInterceptors(Callable<T> task, Map<MessageType, DispatchInterceptor> adhocInterceptors)
      Executes the given task with a previously captured ad hoc interceptor context.
      Type Parameters:
      T - the return type of the task
      Parameters:
      task - the task to run
      adhocInterceptors - captured ad hoc interceptors
      Returns:
      the task result
    • 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
    • interceptDispatch

      public Message interceptDispatch(Message message, MessageType messageType, String topic, String namespace)
      Description copied from interface: DispatchInterceptor
      Intercepts a dispatch to a specific namespace.

      The default implementation delegates to the namespace-agnostic method so existing interceptors retain their behavior. Interceptors that access namespace-sensitive resources can override this method.

      Specified by:
      interceptDispatch in interface DispatchInterceptor
      Parameters:
      message - the message to be dispatched
      messageType - the type of the message
      topic - the target topic or null
      namespace - the target namespace or null for the application namespace
      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, String namespace, boolean request)
      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)
      namespace - the namespace to which the message is dispatched (can be null)
      request - whether the sender expects a response to this dispatch