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.public interface MatsSerializer<Z>
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.
Modifier and Type | Interface and Description |
---|---|
static interface |
MatsSerializer.DeserializedMatsTrace<Z> |
static class |
MatsSerializer.SerializationException
The methods in this interface shall throw this RuntimeException if they encounter problems.
|
static interface |
MatsSerializer.SerializedMatsTrace |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
META_KEY_POSTFIX
The key postfix that should be used for the "meta" key on which the
meta
value from serializeMatsTrace(MatsTrace) should be stored. |
Modifier and Type | Method and Description |
---|---|
MatsTrace<Z> |
createNewMatsTrace(java.lang.String traceId,
java.lang.String flowId,
MatsTrace.KeepMatsTrace keepMatsTrace,
boolean nonPersistent,
boolean interactive,
long ttlMillis,
boolean noAudit)
Used when initiating a new Mats flow.
|
MatsSerializer.DeserializedMatsTrace<Z> |
deserializeMatsTrace(byte[] serialized,
int offset,
int len,
java.lang.String meta)
Used for deserializing a byte array into a
MatsTrace - this includes offset and length. |
MatsSerializer.DeserializedMatsTrace<Z> |
deserializeMatsTrace(byte[] serialized,
java.lang.String meta)
Used for deserializing a byte array into a
MatsTrace - this uses the entire byte array. |
<T> T |
deserializeObject(Z serialized,
java.lang.Class<T> type)
Used for deserializing type Z (typically
String ) to STOs and DTOs. |
default boolean |
handlesMeta(java.lang.String meta)
Whether this implementation of MatsSerializer handles the specified
"meta" . |
<T> T |
newInstance(java.lang.Class<T> type)
Will return a new instance of the requested type.
|
MatsSerializer.SerializedMatsTrace |
serializeMatsTrace(MatsTrace<Z> matsTrace)
Used for serializing the
MatsTrace to a byte array. |
Z |
serializeObject(java.lang.Object object)
Used for serializing STOs and DTOs into type Z, typically
String . |
int |
sizeOfSerialized(Z z) |
static final java.lang.String META_KEY_POSTFIX
meta
value from serializeMatsTrace(MatsTrace)
should be stored. The meta value needs to be provided back when
invoking deserializeMatsTrace(byte[], String)
.default boolean handlesMeta(java.lang.String meta)
"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.
MatsTrace<Z> createNewMatsTrace(java.lang.String traceId, java.lang.String flowId, MatsTrace.KeepMatsTrace keepMatsTrace, boolean nonPersistent, boolean interactive, long ttlMillis, boolean noAudit)
MatsTrace
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. A MatsTrace.Call
must be added before it is good to be sent.traceId
- the Trace Id of this new MatsTrace
.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 be
MatsTrace.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 be false
, 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 be false
.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.MatsTrace
implementation.MatsSerializer.SerializedMatsTrace serializeMatsTrace(MatsTrace<Z> matsTrace)
MatsTrace
to a byte array.matsTrace
- the MatsTrace
instance to serialize.MatsTrace
.META_KEY_POSTFIX
MatsSerializer.DeserializedMatsTrace<Z> deserializeMatsTrace(byte[] serialized, int offset, int len, java.lang.String meta)
MatsTrace
- this includes offset and length.serialized
- the byte array from which to reconstitute the MatsTrace
.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 back from the
serialization process
.MatsTrace
.META_KEY_POSTFIX
MatsSerializer.DeserializedMatsTrace<Z> deserializeMatsTrace(byte[] serialized, java.lang.String meta)
MatsTrace
- this uses the entire byte array.serialized
- the byte array from which to reconstitute the MatsTrace
.meta
- some meta information that the deserialized needs back from the
serialization process
.MatsTrace
.META_KEY_POSTFIX
Z serializeObject(java.lang.Object object)
String
.
If null
is provided as the Object parameter, then null
shall be returned.
object
- the object to serialize. If null
is provided, then null
shall be returned.null
if null was provided as 'object'.int sizeOfSerialized(Z z)
null
. This
is meant for metrics, NOT for determining an absolute byte size for a storage array or anything to this
effect.<T> T deserializeObject(Z serialized, java.lang.Class<T> type)
String
) to STOs and DTOs.
If null
is provided as the 'Z serialized' parameter, then null
shall be returned.
serialized
- the value of type T that should be deserialized into an object of Class T. If null
is
provided, then null
shall be returned.type
- the Class that the supplied value of type Z is thought to represent (i.e. the STO or DTO class).null
if null was provided as 'serialized'.<T> T newInstance(java.lang.Class<T> type)
T
- the type of that class.type
- Which class you want an object of.