Interface Handler<M>
- Type Parameters:
M- the type of messages this handler supports (usuallyDeserializingMessage)
- All Known Implementing Classes:
DefaultHandler, DocumentHandlerDecorator.DocumentHandler, Handler.DelegatingHandler, HandlerInterceptor.InterceptedHandler, MutableHandler, SocketEndpointHandler, StatefulHandler
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 ClassesModifier and TypeInterfaceDescriptionstatic classAbstract base class forHandlerimplementations that delegate to another handler. -
Method Summary
Modifier and TypeMethodDescriptiondefault HandlerMethod<M> getHandlerMethodOrNull(M message) Returns a reusableHandlerMethodcapable of processing the given message, ornullwhen unavailable.getInvoker(M message) Returns aHandlerInvokercapable of processing the given message, if available.default HandlerInvokergetInvokerOrNull(M message) Returns aHandlerInvokercapable of processing the given message, ornullwhen unavailable.Class<?> Returns the class of the handler's target object.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
Returns aHandlerInvokercapable of processing the given message, if available.- Parameters:
message- the message to be handled- Returns:
- an optional
HandlerInvokerif this handler can handle the message; otherwiseOptional.empty()
-
getInvokerOrNull
Returns aHandlerInvokercapable of processing the given message, ornullwhen unavailable.This is a lower-allocation counterpart to
getInvoker(Object)for internal hot paths. Implementations that can resolve an invoker without creating anOptionalshould override this method.- Parameters:
message- the message to be handled- Returns:
- an invoker if this handler can handle the message;
nullotherwise
-
getHandlerMethodOrNull
Returns a reusableHandlerMethodcapable of processing the given message, ornullwhen unavailable.This is an optional lower-allocation path for handlers whose target and method plan can be reused across messages. Implementations that cannot expose a stable method should return
nulland rely ongetInvokerOrNull(Object).- Parameters:
message- the message to be handled- Returns:
- a handler method if this handler can handle the message through a reusable method;
nullotherwise
-
or
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
-