Interface HandlerDecorator

All Known Subinterfaces:
HandlerInterceptor
All Known Implementing Classes:
AuthenticatingInterceptor, DataProtectionInterceptor, DisableMetrics, DocumentHandlerDecorator, ErrorReportingInterceptor, HandlerDecorator.MergedDecorator, HandlerMonitor, SchedulingInterceptor, ValidatingInterceptor, WebsocketHandlerDecorator
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface HandlerDecorator
Functional interface for decorating Handler instances that process DeserializingMessage objects.

A HandlerDecorator is used to wrap a handler with additional behavior. This provides a flexible mechanism for implementing cross-cutting concerns such as authorization, metrics, context propagation, or logging.

Unlike HandlerInterceptor, which focuses on message-level interception, a HandlerDecorator wraps the entire handler object and can be applied at a higher level—for example, before handler resolution occurs.

Composition

Decorators can be chained using andThen(HandlerDecorator), allowing multiple layers of behavior to be composed in a defined order.

Example:


 public class MetricsDecorator implements HandlerDecorator {
     @Override
     public Handler<DeserializingMessage> wrap(Handler<DeserializingMessage> handler) {
         return new Handler<>() {
             @Override
             public Optional<HandlerInvoker> getInvoker(DeserializingMessage message) {
                 return handler.getInvoker(message);
             }

             @Override
             public Class<?> getTargetClass() {
                 return handler.getTargetClass();
             }

             @Override
             public Object invoke(BiFunction<Object, Object, Object> combiner) {
                 long start = System.nanoTime();
                 Object result = handler.getInvoker(message).orElseThrow().invoke(combiner);
                 metrics.recordLatency(System.nanoTime() - start);
                 return result;
             }
         };
     }
 }
 
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    A composite decorator that merges two decorators into one.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final HandlerDecorator
    A no-op decorator that returns the original handler unmodified.
  • Method Summary

    Modifier and Type
    Method
    Description
    Chains this decorator with another, producing a composite decorator.
    io.fluxzero.common.handling.Handler<DeserializingMessage>
    wrap(io.fluxzero.common.handling.Handler<DeserializingMessage> handler)
    Wraps the given handler with additional behavior.
  • Field Details

    • noOp

      static final HandlerDecorator noOp
      A no-op decorator that returns the original handler unmodified.
  • Method Details

    • wrap

      io.fluxzero.common.handling.Handler<DeserializingMessage> wrap(io.fluxzero.common.handling.Handler<DeserializingMessage> handler)
      Wraps the given handler with additional behavior.
      Parameters:
      handler - the original handler to be wrapped
      Returns:
      a decorated handler
    • andThen

      default HandlerDecorator andThen(HandlerDecorator next)
      Chains this decorator with another, producing a composite decorator.

      The next decorator is applied first, followed by this decorator.

      Parameters:
      next - the decorator to apply before this one
      Returns:
      a combined HandlerDecorator