Class StoredDeadlineConverter

java.lang.Object
org.axonframework.deadline.StoredDeadlineConverter

@Internal public final class StoredDeadlineConverter extends Object
Converts the parts of a deadline that the persistent deadline managers store, its payload, metadata and scope descriptor, to and from their stored form, keeping the layout Axon Framework 4.13 wrote.

The stored form holds the payload's type name next to the payload, and the class name next to the scope descriptor. The type name "empty" marks a deadline without a payload, as in Axon Framework 4. Any other type name is resolved to a class, and the stored data is converted into it with the configured Converter. A type name that resolves to no class yields an UnknownDeadlinePayload instead of a failure, as Axon Framework 4's UnknownSerializedType did.

With an XStreamConverter, a type name is first resolved through the mapper of its XStream instance, as Axon Framework 4's XStreamSerializer did. That serializer stored a type's XStream alias instead of its class name, for XStream's built-in aliases such as uuid, int or long, and for aliases the application registered. Such a payload or scope descriptor resolves when the converter's XStream instance has the same aliases as the one Axon Framework 4 used.

A String or byte[] payload is stored the way Axon Framework 4's serializers stored it: as a JSON string, Base64-encoded for a byte[], or as an XStream <string> or <byte-array> element. A Converter treats a String or byte[] as content that is already in its stored form, so it would store such a payload as is. Which of the two forms to write follows from the configured converter's format, which is detected once, by converting a probe value. The JSON string is written and read with Jackson's streaming JSON writer and parser, and the XStream element with XStream itself, so that both match what Axon Framework 4 wrote. A converter that writes neither JSON nor XStream XML cannot store such a payload, and scheduling one fails. Reading follows the stored form; the type names string and byte-array, under which Axon Framework 4's XStreamSerializer stored these payloads, are XStream aliases and resolve as described above.

Metadata is read untyped, into a Map<String, Object>, and each value is turned into a String: Axon Framework 4 metadata could hold any value, while Axon Framework 5 metadata holds strings only. Strings stay as they are, numbers and booleans become their string value, and maps and lists are rendered as JSON by Jackson's streaming JSON writer, the same for every converter that reads them as maps and lists. The XStreamConverter already turns every metadata value into its string value, so a nested value stored with XStream arrives as its toString(). Nested metadata is not expected there, as that converter serves Saga and deadline payloads with plain metadata values.

This class is internal, as it only serves the deadline managers of this module, which share the stored layout.

Since:
5.4.0
Author:
Jakob Hatzl
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The stored type name of a deadline without a payload.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a StoredDeadlineConverter converting with the given converter.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    fromStored(Object data, Class<T> type)
    Converts the given stored data into the given type, for stored structures that are converted as a whole, such as JobRunr's DeadlineDetails.
    messageTypeOf(@Nullable Object payload)
    Returns the MessageType of a deadline carrying the given payload, derived from the payload's class, as the stored form keeps no message type.
    metadata(@Nullable Object data)
    Converts the given stored metadata into Metadata.
    @Nullable Object
    payload(String typeName, @Nullable String revision, @Nullable Object data)
    Converts the given stored payload into the class named by the given typeName.
    <T> @Nullable T
    payloadToStored(@Nullable Object payload, Class<T> representation)
    Converts the given deadline payload into the given stored representation, byte[] or String.
    scope(String className, Object data)
    Converts the given stored scope descriptor into the class named by the given className.
    <T> @Nullable T
    toStored(@Nullable Object value, Class<T> representation)
    Converts the given value into the given stored representation, such as byte[] or String.
    static String
    typeNameOf(@Nullable Object payload)
    Returns the type name to store for the given payload: its class name, or "empty" if there is no payload.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY_TYPE

      public static final String EMPTY_TYPE
      The stored type name of a deadline without a payload. Axon Framework 4 nodes read it back as a null payload without loading a class.
      See Also:
  • Constructor Details

    • StoredDeadlineConverter

      public StoredDeadlineConverter(Converter converter)
      Creates a StoredDeadlineConverter converting with the given converter.
      Parameters:
      converter - the converter matching the stored form of the deadlines
  • Method Details

    • typeNameOf

      public static String typeNameOf(@Nullable Object payload)
      Returns the type name to store for the given payload: its class name, or "empty" if there is no payload.
      Parameters:
      payload - the payload of the deadline to store
      Returns:
      the type name to store with the payload
    • messageTypeOf

      public static MessageType messageTypeOf(@Nullable Object payload)
      Returns the MessageType of a deadline carrying the given payload, derived from the payload's class, as the stored form keeps no message type.
      Parameters:
      payload - the payload of the deadline
      Returns:
      the message type derived from the payload's class
    • payloadToStored

      public <T> @Nullable T payloadToStored(@Nullable Object payload, Class<T> representation)
      Converts the given deadline payload into the given stored representation, byte[] or String. Without a payload, this is the serialized null, the JSON null, as Axon Framework 4's JacksonSerializer wrote it: Axon Framework 4 nodes do not read the data stored under the type name "empty", but reject a job whose data is missing. A String or byte[] payload is stored as Axon Framework 4 stored it in the configured converter's format.
      Type Parameters:
      T - the stored representation
      Parameters:
      payload - the payload of the deadline to store, if any
      representation - the stored representation, byte[] or String
      Returns:
      the stored form of the given payload
      Throws:
      DeadlineException - if the payload is a String or byte[], and the configured converter writes neither JSON nor XStream XML
    • toStored

      public <T> @Nullable T toStored(@Nullable Object value, Class<T> representation)
      Converts the given value into the given stored representation, such as byte[] or String.
      Type Parameters:
      T - the stored representation
      Parameters:
      value - the payload, metadata or scope descriptor to store
      representation - the stored representation
      Returns:
      the stored form of the given value, or null if it is null
    • fromStored

      public <T> T fromStored(Object data, Class<T> type)
      Converts the given stored data into the given type, for stored structures that are converted as a whole, such as JobRunr's DeadlineDetails.
      Type Parameters:
      T - the type to convert the stored data into
      Parameters:
      data - the stored data
      type - the type to convert the stored data into
      Returns:
      the converted data
      Throws:
      DeadlineException - if the data converts to null
    • payload

      public @Nullable Object payload(String typeName, @Nullable String revision, @Nullable Object data)
      Converts the given stored payload into the class named by the given typeName.
      Parameters:
      typeName - the stored type name of the payload
      revision - the stored revision of the payload type, read but not used for the conversion
      data - the stored payload
      Returns:
      null for the type name "empty", an UnknownDeadlinePayload if the type name resolves to no class, and the converted payload otherwise
      Throws:
      DeadlineException - if a String or byte[] payload is stored in neither form Axon Framework 4 stored it in
    • scope

      public ScopeDescriptor scope(String className, Object data)
      Converts the given stored scope descriptor into the class named by the given className.
      Parameters:
      className - the stored class name of the scope descriptor
      data - the stored scope descriptor
      Returns:
      the converted scope descriptor
      Throws:
      DeadlineException - if the class name resolves to no ScopeDescriptor class, or the data converts to no scope descriptor
    • metadata

      public Metadata metadata(@Nullable Object data)
      Converts the given stored metadata into Metadata. Each value becomes a String: strings stay as they are, numbers and booleans become their string value, and maps and lists are rendered as JSON. A nested value read through an XStreamConverter arrives as its toString(), as that converter already turns every metadata value into a string.
      Parameters:
      data - the stored metadata, or null if none was stored
      Returns:
      the converted metadata, empty if none was stored