Class MessageDecorator

java.lang.Object
org.axonframework.messaging.core.MessageDecorator
All Implemented Interfaces:
Message
Direct Known Subclasses:
GenericCommandMessage, GenericEventMessage, GenericQueryMessage, GenericResetContext, GenericResultMessage

public abstract class MessageDecorator extends Object implements Message
Abstract implementation of a Message that delegates to an existing message.

Extend this decorator class to extend the message with additional features.

Since:
3.0.0
Author:
Steven van Beelen, Rene de Waele
  • Constructor Details

    • MessageDecorator

      protected MessageDecorator(@Nonnull Message delegate)
      Initializes a new decorator with given delegate Message.

      The decorator delegates to the delegate for the message's identifier, type, payload, and metadata.

      Parameters:
      delegate - The Message delegate.
  • Method Details

    • identifier

      @Nonnull public String identifier()
      Description copied from interface: Message
      Returns the identifier of this Message.

      Two messages with the same identifiers should be interpreted as different representations of the same conceptual message. In such case, the metadata may be different for both representations. The payload may be identical.

      Specified by:
      identifier in interface Message
      Returns:
      The unique identifier of this Message.
    • type

      @Nonnull public MessageType type()
      Description copied from interface: Message
      Returns the message type of this Message.
      Specified by:
      type in interface Message
      Returns:
      The message type of this Message.
    • payload

      @Nullable public Object payload()
      Description copied from interface: Message
      Returns the payload of this Message.

      The payload is the application-specific information.

      Specified by:
      payload in interface Message
      Returns:
      The payload of this Message.
    • payloadAs

      @Nullable public <T> T payloadAs(@Nonnull Type type, @Nullable Converter converter)
      Description copied from interface: Message
      Returns the payload of this Message, converted to the given type by the given converter.

      If the given type is an instance of Class and Message.payloadType() is assignable from that Class, Message.payload() may be invoked instead of using the given converter.

      Implementers of this operation may optimize by storing the converted payloads, thus saving a Converter.convert(Object, Class) invocation in the process. Only when this optimization is in place will a null converter result in a successful invocation of this method.

      Specified by:
      payloadAs in interface Message
      Type Parameters:
      T - The generic type to convert this Message's payload to.
      Parameters:
      type - The type to convert this Message's payload to.
      converter - The converter to convert this Message's payload with.
      Returns:
      The payload of this Message, converted to the given type.
    • payloadType

      @Nonnull public Class<?> payloadType()
      Description copied from interface: Message
      Returns the type of the payload.

      Is semantically equal to getPayload().getClass(), but allows implementations to optimize by using lazy loading or deserialization.

      Specified by:
      payloadType in interface Message
      Returns:
      The type of payload.
    • metadata

      @Nonnull public Metadata metadata()
      Description copied from interface: Message
      Returns the Metadata for this Message.

      The Metadata is a collection of key-value pairs, where both the key and values are Strings.

      Specified by:
      metadata in interface Message
      Returns:
      The Metadata for this Message.
    • withConvertedPayload

      @Nonnull public Message withConvertedPayload(@Nonnull Type type, @Nonnull Converter converter)
      Description copied from interface: Message
      Returns a new Message implementation with its Message.payload() converted to the given type by the given converter. This new Message is effectively a copy of this Message with a renewed payload and Message.payloadType().

      Will return the this instance if the payload type is assignable from the converted result.

      Specified by:
      withConvertedPayload in interface Message
      Parameters:
      type - The type to convert the Message.payload() to.
      converter - The converter to convert the Message.payload() with.
      Returns:
      A new Message implementation with its Message.payload() converted to the given type by the given converter.
    • delegate

      @Nonnull protected Message delegate()
      Returns the wrapped Message delegated by this decorator.
      Returns:
      The wrapped Message delegated by this decorator.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • describeTo

      protected void describeTo(StringBuilder stringBuilder)
      Describe the message specific properties to the given stringBuilder. Subclasses should override this method, calling the super method and appending their own properties to the end (or beginning).

      As convention, String values should be enclosed in single quotes, Objects in curly brackets and numeric values may be appended without enclosing. All properties should be preceded by a comma when appending, or finish with a comma when prefixing values.

      Parameters:
      stringBuilder - The builder to append data to.
    • describeType

      protected String describeType()
      Describe the type of message, used in toString().

      Defaults to the simple class name of the actual instance.

      Returns:
      The type of the message.