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 is capable of maintaining a converter suitable for its payload set via withConverter(Converter) and 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(MessageType type, @Nullable Object payload)
      Constructs a GenericMessage for the given type and payload.
      Parameters:
      type - The type for this Message.
      payload - The payload for this Message.
    • GenericMessage

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

      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(MessageType type, @Nullable P payload, Class<P> declaredPayloadType, Map<String,String> metadata)
      Constructs a GenericMessage for the given type, payload, declaredPayloadType, and metadata.
      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(String identifier, MessageType type, @Nullable Object payload, Map<String,String> metadata)
      Constructs a GenericMessage for the given identifier, type, payload, and metadata, intended to reconstruct another Message.
      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(String identifier, MessageType type, @Nullable P payload, Class<P> declaredPayloadType, Map<String,String> metadata)
      Constructs a GenericMessage for the given identifier, type, payload, and metadata, intended to reconstruct another Message.
      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

      public @Nullable 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

      public <T> @Nullable T payloadAs(Class<T> type)
      Description copied from interface: Message
      Returns the payload of this Message casted to the given type if Message.payloadType() is assignable from the given type, otherwise throws a ConversionException.
      Type Parameters:
      T - The generic type to convert this Message's payload to.
      Parameters:
      type - The type to convert this Message's payload to.
      Returns:
      The payload of this Message, converted to the given type.
    • payloadAs

      public <T> @Nullable T payloadAs(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

      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

      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

      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
    • withConverter

      public GenericMessage withConverter(@Nullable Converter converter)
      Returns a new GenericMessage instance with the same payload and properties as this message and the given converter to be used for payload conversion.

      Note: if called from a subtype, the message will lose subtype information because this method creates a new instance of GenericMessage.

      Parameters:
      converter - the converter for the new message
      Returns:
      a copy of this instance with the converter
    • withConvertedPayload

      public Message withConvertedPayload(Type type, 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.