Class GenericMessage

java.lang.Object
org.axonframework.messaging.core.AbstractMessage
org.axonframework.messaging.core.GenericMessage
All Implemented Interfaces:
Message

public class GenericMessage extends AbstractMessage
Generic implementation of the Message interface containing the payload and metadata in deserialized form.

This Message implementation is "conversion aware," as it maintains any conversion results from payloadAs(Type, Converter) and withConvertedPayload(Type, Converter) (either invoked with a Class, TypeReference, or Type), together with the hash of the given Converter. In doing so, this Message optimizes subsequent payloadAs/withConvertedPayload invocations for the same type-and-converter combination. If this optimization should be disabled, the "AXON_CONVERSION_CACHE_ENABLED" system property can be set to false.

Since:
2.0.0
Author:
Allard Buijze, Steven van Beelen
  • Constructor Details

    • GenericMessage

      public GenericMessage(@Nonnull MessageType type, @Nullable Object payload)
      Constructs a GenericMessage for the given type and payload.

      Uses the correlation data of the current Unit of Work, if present.

      Parameters:
      type - The type for this Message.
      payload - The payload for this Message.
    • GenericMessage

      public GenericMessage(@Nonnull MessageType type, @Nullable Object payload, @Nonnull Map<String,String> metadata)
      Constructs a GenericMessage for the given type, payload, and metadata.

      The given metadata is merged with the Metadata from the correlation data of the current Unit of Work, if present. In case the payload == null, Void will be used as the payloadType.

      Parameters:
      type - The type for this Message.
      payload - The payload for this Message.
      metadata - The metadata for this Message.
    • GenericMessage

      public GenericMessage(@Nonnull MessageType type, @Nullable P payload, @Nonnull Class<P> declaredPayloadType, @Nonnull Map<String,String> metadata)
      Constructs a GenericMessage for the given type, payload, declaredPayloadType, and metadata.

      The given metadata is merged with the Metadata from the correlation data of the current Unit of Work, if present.

      Type Parameters:
      P - The generic type of the expected payload of the resulting object.
      Parameters:
      type - The type for this Message.
      payload - The payload of type P for this Message.
      declaredPayloadType - The declared type of the payload of this Message.
      metadata - The metadata for this Message.
    • GenericMessage

      public GenericMessage(@Nonnull String identifier, @Nonnull MessageType type, @Nullable Object payload, @Nonnull Map<String,String> metadata)
      Constructs a GenericMessage for the given identifier, type, payload, and metadata, intended to reconstruct another Message.

      Unlike the other constructors, this constructor will not attempt to retrieve any correlation data from the Unit of Work. If you in tend to construct a new GenericMessage, please use GenericMessage(MessageType, Object) instead.

      Parameters:
      identifier - The identifier of this Message.
      type - The type for this Message.
      payload - The payload for this Message.
      metadata - The metadata for this Message.
    • GenericMessage

      public GenericMessage(@Nonnull String identifier, @Nonnull MessageType type, @Nullable P payload, @Nonnull Class<P> declaredPayloadType, @Nonnull Map<String,String> metadata)
      Constructs a GenericMessage for the given identifier, type, payload, and metadata, intended to reconstruct another Message.

      Unlike the other constructors, this constructor will not attempt to retrieve any correlation data from the Unit of Work. If you in tend to construct a new GenericMessage, please use GenericMessage(MessageType, Object) instead.

      Type Parameters:
      P - The generic type of the expected payload of the resulting object.
      Parameters:
      identifier - The identifier of this Message.
      type - The type for this Message.
      payload - The payload of type P for this Message.
      declaredPayloadType - The declared type of the payload of this Message.
      metadata - The metadata for this Message.
  • Method Details

    • emptyMessage

      public static Message emptyMessage()
      Construct an empty message.
      Returns:
      A message with null Message.payload(), no Metadata, and a Message.type() of "empty".
    • payload

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

      The payload is the application-specific information.

      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.

      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.

      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.

      Returns:
      The Metadata for this Message.
    • withMetadata

      @Nonnull protected Message withMetadata(Metadata metadata)
      Description copied from class: AbstractMessage
      Returns a new message instance with the same payload and properties as this message but given metadata.
      Specified by:
      withMetadata in class AbstractMessage
      Parameters:
      metadata - The metadata in the new message
      Returns:
      a copy of this instance with given metadata
    • 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.

      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.