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:
CommandHandlingMember<T>, EventHandlingMember<T>, MessageInterceptingMember<T>, QueryHandlingMember<T>
All Known Implementing Classes:
MethodInvokingMessageHandlingMember, WrappedMessageHandlingMember

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

    Modifier and Type
    Method
    Description
    default <R> Optional<R>
    attribute(String attributeKey)
    Retrieve a single attributes for the given attributeKey.
    boolean
    canHandle(Message message, ProcessingContext context)
    Checks if this handler is capable of handling the given message.
    boolean
    canHandleMessageType(Class<? extends Message> messageType)
    Checks if this handler is capable of handling Message implementations of the given messageType.
    default boolean
    canHandleType(Class<?> payloadType)
    Checks if this handler is capable of handling messages with the given payloadType.
    default Class<?>
    Gets the declaring class of this Message Handling Member.
    handle(Message message, ProcessingContext context, T target)
    Handles the given message within the given context by invoking the appropriate method on given target.
    handleSync(Message message, ProcessingContext context, T target)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Returns the payload type of messages that can be processed by this handler.
    default int
    Returns a number representing the priority of this handler over other handlers capable of processing the same message.
    default String
    Returns the signature of the member.
    <HT> Optional<HT>
    unwrap(Class<HT> handlerType)
    Returns the wrapped handler object if its type is an instance of the given handlerType.
  • 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, @Nonnull ProcessingContext context)
      Checks if this handler is capable of handling the given message.
      Parameters:
      message - The message that is to be handled.
      context - The context in which the message is being 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 handler 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.
    • handleSync

      @Internal @Deprecated(forRemoval=true, since="5.2.0") Object handleSync(@Nonnull Message message, @Nonnull ProcessingContext context, @Nullable T target) throws Exception
      Deprecated, for removal: This API element is subject to removal in a future version.
      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.
      context - The context in which the message is being handled.
      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.
    • handle

      MessageStream<?> handle(@Nonnull Message message, @Nonnull ProcessingContext context, @Nullable T target)
      Handles the given message within the given context 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. These exceptions should be caught and passed along in a failed MessageStream.

      Parameters:
      message - The message to handle.
      context - The context in which the message is being handled.
      target - The target to handle the message.
      Returns:
      The MessageStream containing the response(s), if any, from handling the given message.
    • 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
    • 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.

      Type Parameters:
      R - The value type of the returned attribute.
      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.