Skip to content

Message interceptors

Fluxzero offers a flexible and extensible interceptor model to hook into key stages of the message lifecycle:

Interceptor typeTarget phaseTypical use cases
DispatchBefore publishing/handling a messageMutate, block, enrich, or observe outgoing messages
HandlerAround handler executionValidation, logging, authentication, result decoration
BatchAround batch processingTracing, retries, context injection, metrics

All interceptors are pluggable, and can be configured via:

  • FluxzeroBuilder for global registration
  • @Consumer(handlerInterceptors = ...)
  • @Consumer(batchInterceptors = ...)

A DispatchInterceptor hooks into the message dispatch phase—just before the message is published to Fluxzero or handled locally.

public class LoggingInterceptor implements DispatchInterceptor {
@Override
public Message interceptDispatch(Message message, MessageType type, String topic) {
log.info("Dispatching: {} to topic {}", type, topic);
return message;
}
}

Capabilities:

  • interceptDispatch(...): Modify, block, or inspect outgoing messages
  • modifySerializedMessage(...): Mutate message after serialization but before transmission
  • monitorDispatch(...): Observe the final message as it’s sent

Register globally: Assuming you are configuring a FluxzeroBuilder builder:

builder
.addDispatchInterceptor(new LoggingInterceptor(), MessageType.COMMAND, MessageType.EVENT);

A HandlerInterceptor allows wrapping the execution of handler methods, ideal for:

  • Authorization and access control — prevent unauthorized commands or queries based on the current user
  • Auditing and logging — log incoming messages, handler invocations, or emitted results
  • Validation hooks — perform extra validation before or after handler execution
  • Result transformation — enrich or reformat results before they’re published or returned
  • Thread context propagation — populate thread-local state like correlation IDs or security principals
public class AuthorizationInterceptor implements HandlerInterceptor {
@Override
public Function<DeserializingMessage, Object> interceptHandling(
Function<DeserializingMessage, Object> next, HandlerInvoker invoker) {
return message -> {
if (!isAuthorized(message)) {
throw new UnauthorizedException();
}
return next.apply(message);
};
}
}

Register via annotation or builder:

@Consumer(handlerInterceptors = AuthorizationInterceptor.class)
public class SecureCommandHandler { ... }
builder
.addHandlerInterceptor(new AuthorizationInterceptor(), true, MessageType.COMMAND);

Wraps around the processing of a full message batch by a single consumer, ideal for:

  • Structured logging
  • Performance instrumentation
  • Scoped resources (e.g. transactions)
public class LoggingBatchInterceptor implements BatchInterceptor {
@Override
public Consumer<MessageBatch> intercept(Consumer<MessageBatch> consumer, Tracker tracker) {
return batch -> {
log.info("Start processing {} messages", batch.size());
consumer.accept(batch);
};
}
}

Global install: Assuming you are configuring a FluxzeroBuilder builder:

builder
.addBatchInterceptor(new LoggingBatchInterceptor(), MessageType.EVENT);

This specialization of BatchInterceptor can rewrite or filter the batch itself:

MappingBatchInterceptor filterTestMessages = (batch, tracker) -> {
var filtered = batch.getMessages().stream()
.filter(m -> !m.getMetadata().containsKey("testOnly"))
.toList();
return batch.withMessages(filtered);
};

Install globally: Assuming you are configuring a FluxzeroBuilder builder:

builder
.addBatchInterceptor(filterTestMessages, MessageType.QUERY);

Interceptors are a central way to add cross-cutting behavior across all stages of message flow, from dispatch to handling and batching — empowering modular, observable, and policy-driven systems.


InterceptorRuns atPurpose
DispatchInterceptorBefore a message is published or handled locallyMutate, block, enrich, or log outgoing messages
HandlerInterceptorAround handler method executionAuthorization, validation, logging, result transformation
BatchInterceptorAround an entire batch of messagesTracing, retries, metrics, resource scoping
MappingBatchInterceptorSpecialized batch interceptorRewrite or filter whole batches before processing

© 2026 Fluxzero