Interface MessageHandlingMember<T>

Type Parameters:
T - The type of entity to which the message handler will delegate the actual handling of the message
All Known Subinterfaces:
CommandHandlerInterceptorHandlingMember<T>, CommandMessageHandlingMember<T>, CreationPolicyMember<T>, DeadlineHandlingMember<T>, ForwardingCommandMessageHandlingMember<T>, MessageInterceptingMember<T>, QueryHandlingMember<T>
All Known Implementing Classes:
AnnotatedMessageHandlingMember, ChildForwardingCommandMessageHandlingMember, EndSagaMessageHandlerDefinition.EndSageMessageHandlingMember, SagaMethodMessageHandlingMember, WrappedMessageHandlingMember

public interface MessageHandlingMember<T>
Interface describing a handler for specific messages targeting entities of a specific type.
Since:
3.0
Author:
Allard Buijze
  • Method Details

    • payloadType

      Class<?> payloadType()
      Returns the payload type of messages that can be processed by this handler.
      Returns:
      The payload type of messages expected by this handler
    • priority

      default int priority()
      Returns a number representing the priority of this handler over other handlers capable of processing the same message.

      In general, a handler with a higher priority will receive the message before (or instead of) handlers with a lower priority. However, the priority value may not be the only indicator that is used to determine the order of invocation. For instance, a message processor may decide to ignore the priority value if one message handler is a more specific handler of the message than another handler.

      Returns:
      Number indicating the priority of this handler over other handlers
    • canHandle

      boolean canHandle(@Nonnull Message<?> message)
      Checks if this handler is capable of handling the given message.
      Parameters:
      message - The message that is to be handled
      Returns:
      true if the handler is capable of handling the message, false otherwise
    • canHandleType

      default boolean canHandleType(@Nonnull Class<?> payloadType)
      Checks if this handler is capable of handling messages with the given payloadType.
      Parameters:
      payloadType - The payloadType of a message that is to be handled
      Returns:
      true if the handler is capable of handling the message with given type, false otherwise
    • canHandleMessageType

      boolean canHandleMessageType(@Nonnull Class<? extends Message> messageType)
      Checks if this handlers is capable of handling Message implementations of the given messageType.

      It is recommended to suppress the raw type use validation of the messageType parameter when implementing this method, as usage of this method with a Message generic would required reflection or casting otherwise.

      Parameters:
      messageType - the Message's type to check if it can be handled by this handler
      Returns:
      true if this handler can handle the given messageType, false otherwise
    • handle

      Object handle(@Nonnull Message<?> message, @Nullable T target) throws Exception
      Handles the given message by invoking the appropriate method on given target. This may result in an exception if the given target is not capable of handling the message or if an exception is thrown during invocation of the method.
      Parameters:
      message - The message to handle
      target - The target to handle the message
      Returns:
      The message handling result in case the invocation was successful
      Throws:
      Exception - when there was a problem that prevented invocation of the method or if an exception was thrown from the invoked method
    • unwrap

      <HT> Optional<HT> unwrap(Class<HT> handlerType)
      Returns the wrapped handler object if its type is an instance of the given handlerType. For instance, if this method is invoked with Executable and the message is handled by a method of the target entity, then this method will return the method handle as a Method.
      Type Parameters:
      HT - The wrapped handler type
      Parameters:
      handlerType - The expected type of the wrapped handler
      Returns:
      An Optional containing the wrapped handler object or an empty Optional if the handler is not an instance of the given handlerType
    • declaringClass

      default Class<?> declaringClass()
      Gets the declaring class of this Message Handling Member.
      Returns:
      the declaring class of this Message Handling Member
    • signature

      default String signature()
      Returns the signature of the member. This may be used in logging or exceptions to demarcate the actual class member invoked. If this member does not have a signature, "__unknown__" is returned.
      Returns:
      the signature of the handling member
    • hasAnnotation

      @Deprecated boolean hasAnnotation(Class<? extends Annotation> annotationType)
      Deprecated.
      in favor of attribute(String)
      Checks whether the method of the target entity contains the given annotationType.
      Parameters:
      annotationType - Annotation to check for on the target method
      Returns:
      true if the annotation is present on the target method, false otherwise
    • annotationAttributes

      @Deprecated Optional<Map<String,Object>> annotationAttributes(Class<? extends Annotation> annotationType)
      Deprecated.
      in favor of attribute(String)
      Get the attributes of an annotation of given annotationType on the method of the target entity. If the annotation is present on the target method an Optional is returned containing the properties mapped by their name. If the annotation is not present an empty Optional is returned.
      Parameters:
      annotationType - The annotation to check for on the target method
      Returns:
      An optional containing a map of the properties of the annotation, or an empty optional if the annotation is missing on the method
    • attribute

      default <R> Optional<R> attribute(String attributeKey)
      Retrieve a single attributes for the given attributeKey. If this MessageHandlingMember does not hold a value referencing the attributeKey, an Optional.empty() is returned. Otherwise a non-empty Optional containing the attribute will be provided.

      When using the method, consider checking the HandlerAttributes for a list of common handler attributes.

      Parameters:
      attributeKey - the key to retrieve an attribute for
      Returns:
      a non-empty Optional of the attribute referencing the given attributeKey. Otherwise an Optional.empty() will be returned