Interface Handler<M>

Type Parameters:
M - the type of messages this handler supports (usually DeserializingMessage)
All Known Implementing Classes:
DefaultHandler, DocumentHandlerDecorator.DocumentHandler, Handler.DelegatingHandler, HandlerInterceptor.InterceptedHandler, MutableHandler, SocketEndpointHandler, StatefulHandler

public interface Handler<M>
Represents a container for a message handler and the mechanism to resolve a HandlerInvoker for a given message.

A Handler encapsulates a target class and a provider for an instance of that class. It acts as a factory for HandlerInvoker instances that can be used to invoke the appropriate handler method for a given message.

This abstraction allows support for both stateless and stateful handlers:

  • Stateless: A singleton handler instance is reused for every message (e.g., typical application service).
  • Stateful: The handler instance is dynamically retrieved, e.g., from a repository, based on message content (e.g., aggregates or projections).

A handler may or may not be able to process a given message. If it can, it returns a non-empty Optional containing a HandlerInvoker; otherwise, it returns Optional.empty().

Handler Architecture

 ┌────────────────────┐
 │  HandlerInspector  │
 └────────┬───────────┘
          │ inspects target class
          ▼
 ┌────────────────────┐        creates        ┌──────────────────────┐
 │  HandlerMatcher    │──────────────────────▶│     HandlerInvoker   │
 └────────┬───────────┘                       └──────────────────────┘
          │ produces invoker if message
          │ matches a method
          ▼
 ┌────────────────────┐
 │      Handler       │◀───────────── target instance
 └────────────────────┘
 
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Abstract base class for Handler implementations that delegate to another handler.
  • Method Summary

    Modifier and Type
    Method
    Description
    getInvoker(M message)
    Returns a HandlerInvoker capable of processing the given message, if available.
    Returns the class of the handler's target object.
    default Handler<M>
    or(Handler<M> next)
    Creates a composite handler that executes the current handler and then delegates to the specified next handler if the current handler cannot handle the message or does not provide an invoker.
  • Method Details

    • getTargetClass

      Class<?> getTargetClass()
      Returns the class of the handler's target object. This may be used for reflective operations, logging, or framework-level behavior.
      Returns:
      the class of the handler's target
    • getInvoker

      Optional<HandlerInvoker> getInvoker(M message)
      Returns a HandlerInvoker capable of processing the given message, if available.
      Parameters:
      message - the message to be handled
      Returns:
      an optional HandlerInvoker if this handler can handle the message; otherwise Optional.empty()
    • or

      default Handler<M> or(Handler<M> next)
      Creates a composite handler that executes the current handler and then delegates to the specified next handler if the current handler cannot handle the message or does not provide an invoker.
      Parameters:
      next - the next handler to be invoked if this handler does not handle the message
      Returns:
      a new handler combining the current handler and the specified next handler