Class SerializedMessage<T>

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

public class SerializedMessage<T> extends AbstractMessage<T>
A message containing serialized payload data and metadata. A SerializedMessage will deserialize the payload or metadata on demand when getPayload() or getMetaData() is called.

The SerializedMessage guarantees that the payload and metadata will not be deserialized more than once. Messages of this type will not be serialized more than once by the same serializer.

Author:
Rene de Waele
See Also:
  • Constructor Details

    • SerializedMessage

      public SerializedMessage(String identifier, SerializedObject<?> serializedPayload, SerializedObject<?> serializedMetaData, Serializer serializer)
      Initializes a SerializedMessage with given identifier from the given serialized payload and metadata. The given serializer will be used to deserialize the data.
      Parameters:
      identifier - the message identifier
      serializedPayload - the serialized message payload
      serializedMetaData - the serialized message metadata
      serializer - the serializer required when the data needs to be deserialized
    • SerializedMessage

      public SerializedMessage(String identifier, LazyDeserializingObject<T> payload, LazyDeserializingObject<MetaData> metaData)
      Initializes a SerializedMessage with given identifier from the given lazily deserializing payload and metadata.
      Parameters:
      identifier - the message identifier
      payload - serialized payload that can be deserialized on demand and never more than once
      metaData - serialized metadata that can be deserialized on demand and never more than once
  • Method Details

    • 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
    • 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
    • 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 SerializedMessage<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
    • withMetaData

      public SerializedMessage<T> withMetaData(@Nonnull Map<String,?> metaData)
      Description copied from interface: Message
      Returns a copy of this Message with the given metaData. The payload remains unchanged.

      While the implementation returned may be different than the implementation of this, implementations must take special care in returning the same type of Message (e.g. EventMessage, DomainEventMessage) to prevent errors further downstream.

      Specified by:
      withMetaData in interface Message<T>
      Overrides:
      withMetaData in class AbstractMessage<T>
      Parameters:
      metaData - The new MetaData for the Message
      Returns:
      a copy of this message with the given MetaData
    • andMetaData

      public SerializedMessage<T> andMetaData(@Nonnull Map<String,?> metaData)
      Description copied from interface: Message
      Returns a copy of this Message with it MetaData merged with the given metaData. The payload remains unchanged.
      Specified by:
      andMetaData in interface Message<T>
      Overrides:
      andMetaData in class AbstractMessage<T>
      Parameters:
      metaData - The MetaData to merge with
      Returns:
      a copy of this message with the 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
    • isPayloadDeserialized

      public boolean isPayloadDeserialized()
      Indicates whether the payload of this message has already been deserialized.
      Returns:
      true if the payload is deserialized, otherwise false
    • isMetaDataDeserialized

      public boolean isMetaDataDeserialized()
      Indicates whether the metaData of this message has already been deserialized.
      Returns:
      true if the metaData is deserialized, otherwise false
    • writeReplace

      protected Object writeReplace()
      Java Serialization API Method that provides a replacement to serialize, as the fields contained in this instance are not serializable themselves.
      Returns:
      the GenericMessage to use as a replacement when serializing