Class GenericMessage<T>

java.lang.Object
org.axonframework.messaging.AbstractMessage<T>
org.axonframework.messaging.GenericMessage<T>
All Implemented Interfaces:
Serializable, Message<T>

public class GenericMessage<T> extends AbstractMessage<T>
Generic implementation of a Message that contains the payload and metadata as unserialized values.

If a GenericMessage is created while a UnitOfWork is active it copies over the correlation data of the UnitOfWork to the created message.

Since:
2.0
Author:
Allard Buijze
See Also:
  • Constructor Details

    • GenericMessage

      public GenericMessage(T payload)
      Constructs a Message for the given payload using the correlation data of the current Unit of Work, if present.
      Parameters:
      payload - The payload for the message
    • GenericMessage

      public GenericMessage(T payload, Map<String,?> metaData)
      Constructs a Message for the given payload and meta data. 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:
      payload - The payload for the message as a generic T
      metaData - The meta data Map for the message
    • GenericMessage

      public GenericMessage(Class<T> declaredPayloadType, T payload, Map<String,?> metaData)
      Constructs a Message for the given payload and meta data. The given metaData is merged with the MetaData from the correlation data of the current unit of work, if present.
      Parameters:
      declaredPayloadType - The declared type of message payload
      payload - The payload for the message
      metaData - The meta data for the message
    • GenericMessage

      public GenericMessage(String identifier, T payload, Map<String,?> metaData)
      Constructor to reconstruct a Message using existing data. Note that no correlation data from a UnitOfWork is attached when using this constructor. If you're constructing a new Message, use GenericMessage(Object, Map) instead.
      Parameters:
      identifier - The identifier of the Message
      payload - The payload of the message
      metaData - The meta data of the message
      Throws:
      NullPointerException - when the given payload is null.
    • GenericMessage

      public GenericMessage(String identifier, Class<T> declaredPayloadType, T payload, Map<String,?> metaData)
      Constructor to reconstruct a Message using existing data. Note that no correlation data from a UnitOfWork is attached when using this constructor. If you're constructing a new Message, use GenericMessage(Object, Map) instead
      Parameters:
      identifier - The identifier of the Message
      declaredPayloadType - The declared type of message payload
      payload - The payload for the message
      metaData - The meta data for the message
  • Method Details

    • asMessage

      public static Message<?> asMessage(Object payloadOrMessage)
      Returns a Message representing the given payloadOrMessage, either by wrapping it or by returning it as-is. If the given payloadOrMessage already implements Message, it is returned as-is, otherwise a Message is returned with the parameter as its payload.
      Parameters:
      payloadOrMessage - The payload to wrap or message to return
      Returns:
      a Message with the given payload or the message
    • getMetaData

      public MetaData getMetaData()
      Description copied from interface: Message
      Returns the meta data for this message. This meta data is a collection of key-value pairs, where the key is a String, and the value is a serializable object.
      Returns:
      the meta data for this message
    • getPayload

      public T getPayload()
      Description copied from interface: Message
      Returns the payload of this message. The payload is the application-specific information.
      Returns:
      the payload of this message
    • getPayloadType

      public Class<T> getPayloadType()
      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.
    • withMetaData

      protected Message<T> 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<T>
      Parameters:
      metaData - The metadata in the new message
      Returns:
      a copy of this instance with given metadata
    • serializePayload

      public <R> SerializedObject<R> serializePayload(Serializer serializer, Class<R> expectedRepresentation)
      Description copied from interface: Message
      Serialize the payload of this message to the expectedRepresentation using given serializer. This method should return the same SerializedObject instance when invoked multiple times using the same serializer.
      Type Parameters:
      R - The type of the serialized data
      Parameters:
      serializer - The serializer to serialize payload with
      expectedRepresentation - The type of data to serialize to
      Returns:
      a SerializedObject containing the serialized representation of the message's payload
    • serializeMetaData

      public <R> SerializedObject<R> serializeMetaData(Serializer serializer, Class<R> expectedRepresentation)
      Description copied from interface: Message
      Serialize the meta data of this message to the expectedRepresentation using given serializer. This method should return the same SerializedObject instance when invoked multiple times using the same serializer.
      Type Parameters:
      R - The type of the serialized data
      Parameters:
      serializer - The serializer to serialize meta data with
      expectedRepresentation - The type of data to serialize to
      Returns:
      a SerializedObject containing the serialized representation of the message's meta data