Interface MatsSerializer<Z>
- Type Parameters:
Z
- The type which STOs and DTOs are serialized into. When employing JSON for the "outer" serialization of MatsTrace, it does not make that much sense to use a binary (Z=byte[]) "inner" representation of the DTOs and STOs, because JSON is terrible at serializing byte arrays.
- All Known Implementing Classes:
MatsSerializerJson
MatsTrace
s to and from byte arrays (e.g. UTF-8
encoded JSON or XML, or some binary serialization protocol), and STOs and DTOs to and from type Z, where Z can e.g.
be byte arrays or Strings. This is separated out from the Mats communication implementation (i.e. JMS or RabbitMQ),
as it is a separate aspect, i.e. both the JMS and RabbitMQ implementation can utilize the same serializer.
There are two levels of serialization needed: For the DTOs and STOs that the Mats API expose to the "end user", and
then the serialization of the MatsTrace itself. There is an implementation of MatsTrace in the impl package called
MatsTraceFieldImpl
which is meant to be serialized by fields (thus the field names are short).
The default implementation in 'mats-serial-json' (MatsSerializerJson
) employs the Jackson JSON library
to serialize to JSON, both for the "inner" DTO-and-STO part, and for the "outer" MatsTrace part.
It is worth pointing out that all the communicating parties needs to be using the same serialization
mechanism, as this constitute the "wire-representation" of the protocol that MatsTrace
represents. There is
however a mechanism to handle different serializations, by means of a metadata
construct
: Along with the serialized bytes, a metadata String must be provided. It is thus possible to construct a
MatsSerializer that holds multiple underlying MatsSerializers, choosing based on the "meta" String. This can then be
used to upgrade from a format to another.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
static class
The methods in this interface shall throw this RuntimeException if they encounter problems.static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The key postfix that should be used for the "meta" key on which themeta
value fromserializeMatsTrace(MatsTrace)
should be stored. -
Method Summary
Modifier and TypeMethodDescriptioncreateNewMatsTrace
(String traceId, String flowId, MatsTrace.KeepMatsTrace keepMatsTrace, boolean nonPersistent, boolean interactive, long ttlMillis, boolean noAudit) Used when initiating a new Mats flow.deserializeMatsTrace
(byte[] serialized, int offset, int len, String meta) Used for deserializing a byte array into aMatsTrace
- this includes offset and length.deserializeMatsTrace
(byte[] serialized, String meta) Used for deserializing a byte array into aMatsTrace
- this uses the entire byte array.<T> T
deserializeObject
(Z serialized, Class<T> type) Used for deserializing type Z (typicallyString
) to STOs and DTOs.default boolean
handlesMeta
(String meta) Whether this implementation of MatsSerializer handles the specified"meta"
.<T> T
newInstance
(Class<T> type) Will return a new instance of the requested type.serializeMatsTrace
(MatsTrace<Z> matsTrace) Used for serializing theMatsTrace
to a byte array.serializeObject
(Object object) Used for serializing STOs and DTOs into type Z, typicallyString
.int
-
Field Details
-
META_KEY_POSTFIX
The key postfix that should be used for the "meta" key on which themeta
value fromserializeMatsTrace(MatsTrace)
should be stored. The meta value needs to be provided back when invokingdeserializeMatsTrace(byte[], String)
.- See Also:
-
-
Method Details
-
handlesMeta
Whether this implementation of MatsSerializer handles the specified"meta"
.This feature can at some point be used to configure up a bunch of serializers, whereby the one that handles the incoming format gets the job to deserialize it into a MatsTrace. One can then also migrate to a newer version in a two (three)-step fashion: First make a revision-change that includes the new serializer version, but still employs the old for serialization. Then, when all parties are upgraded to the new config, you make a new revision or minor change that changes the config to employ the new serializer for serialization. Then, when all parties are up on this version, you can potentially make a third version that removes the old serializer.
-
createNewMatsTrace
MatsTrace<Z> createNewMatsTrace(String traceId, String flowId, MatsTrace.KeepMatsTrace keepMatsTrace, boolean nonPersistent, boolean interactive, long ttlMillis, boolean noAudit) Used when initiating a new Mats flow. Since theMatsTrace
implementation is dependent on the serialization mechanism in use, we need a way provided by the serializer to instantiate new instances of the implementation of MatsTrace. AMatsTrace.Call
must be added before it is good to be sent.- Parameters:
traceId
- the Trace Id of this newMatsTrace
.flowId
- System-defined id for this call flow - guaranteed unique.keepMatsTrace
- to which extent the MatsTrace should "keep trace", i.e. whether all Calls and States should be kept through the entire flow from initiation to terminator - default shall beMatsTrace.KeepMatsTrace.COMPACT
. The only reason for why this exists is for debugging: The implementation cannot depend on this feature. To see the call history, do a toString() on the ProcessContext of the lambda, which should perform a toString() on the corresponding MatsTrace, which should have a human readable trace output.nonPersistent
- whether the message should be JMS-style "non-persistent" - default shall befalse
, i.e. the default is that a message is persistent.interactive
- whether the message should be prioritized in that a human is actively waiting for the reply, default shall befalse
.ttlMillis
- the number of milliseconds the message should live before being time out. 0 means "forever", and is the default.noAudit
- hint to the underlying implementation, or to any monitoring/auditing tooling on the Message Broker, that it does not make much value in auditing this message flow, typically because it is just a "getter" of information to show to some user, or a health-check validating that some service is up and answers in a timely fashion.- Returns:
- a new instance of the underlying
MatsTrace
implementation.
-
serializeMatsTrace
Used for serializing theMatsTrace
to a byte array. -
deserializeMatsTrace
MatsSerializer.DeserializedMatsTrace<Z> deserializeMatsTrace(byte[] serialized, int offset, int len, String meta) Used for deserializing a byte array into aMatsTrace
- this includes offset and length.- Parameters:
serialized
- the byte array from which to reconstitute theMatsTrace
.offset
- from where to start in the byte array.len
- how many bytes to use of the byte array, from the offset.meta
- some meta information that the deserialized needs backfrom the serialization process
.- Returns:
- the reconstituted
MatsTrace
. - See Also:
-
deserializeMatsTrace
Used for deserializing a byte array into aMatsTrace
- this uses the entire byte array.- Parameters:
serialized
- the byte array from which to reconstitute theMatsTrace
.meta
- some meta information that the deserialized needs backfrom the serialization process
.- Returns:
- the reconstituted
MatsTrace
. - See Also:
-
serializeObject
Used for serializing STOs and DTOs into type Z, typicallyString
.If
null
is provided as the Object parameter, thennull
shall be returned.- Parameters:
object
- the object to serialize. Ifnull
is provided, thennull
shall be returned.- Returns:
- a String representation of the provided object, or
null
if null was provided as 'object'.
-
sizeOfSerialized
- Returns:
- the size in bytes or characters of the serialized DTO or STO, shall return 0 for
null
. This is meant for metrics, NOT for determining an absolute byte size for a storage array or anything to this effect.
-
deserializeObject
Used for deserializing type Z (typicallyString
) to STOs and DTOs.If
null
is provided as the 'Z serialized' parameter, thennull
shall be returned.- Parameters:
serialized
- the value of type T that should be deserialized into an object of Class T. Ifnull
is provided, thennull
shall be returned.type
- the Class that the supplied value of type Z is thought to represent (i.e. the STO or DTO class).- Returns:
- the reconstituted Object (STO or DTO), or
null
if null was provided as 'serialized'.
-
newInstance
Will return a new instance of the requested type. This is used to instantiate "empty objects" for Endpoint State objects (STOs), and to test, at Endpoint registration, whether it is possible to get hold of instances of the Request and Reply DTO for an Endpoint (to ensure that it will be possible in runtime). The reason for having this in the MatsSerializer is that it is somewhat dependent on the object serializer in use: GSON allows to instantiate private, missing-no-args-constructor classes, while Jackson does not.- Type Parameters:
T
- the type of that class.- Parameters:
type
- Which class you want an object of.- Returns:
- an "empty" new instance of the class.
-