Class DeserializingMessage
- All Implemented Interfaces:
HasMessage
- Direct Known Subclasses:
ChunkedDeserializingMessage, DefaultEntityHelper.DeserializingMessageWithEntity, StatefulHandler.StatefulHandlerSearchMessage, StatefulHandler.StatefulMemberMessage
Message that supports lazy deserialization, context caching, type adaptation, and batch-level
execution utilities.
DeserializingMessage combines a SerializedMessage with deserialization and routing logic while
maintaining the original message context (type, topic, metadata, and payload).
Key Features
- Supports on-demand deserialization of a
Message - Provides thread-local access to the message that is currently being handled
- Allows attaching resources to the current message or message batch/li>
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic MessageFormatterThe formatter used to produce a human-readable representation of this message, primarily for logging or debugging.Fields inherited from interface HasMessage
warnedAboutMissingProperty -
Constructor Summary
ConstructorsModifierConstructorDescriptionDeserializingMessage(io.fluxzero.common.api.SerializedMessage message, Function<Type, Object> payload, io.fluxzero.common.MessageType messageType, String topic, Serializer serializer) DeserializingMessage(@NonNull Message message, io.fluxzero.common.MessageType messageType, Serializer serializer) DeserializingMessage(@NonNull Message message, io.fluxzero.common.MessageType messageType, String topic, Serializer serializer) protectedDeserializingMessage(@NonNull DeserializingMessage input) DeserializingMessage(DeserializingObject<byte[], io.fluxzero.common.api.SerializedMessage> delegate, io.fluxzero.common.MessageType messageType, String topic, Serializer serializer) -
Method Summary
Modifier and TypeMethodDescription<T> Tapply(Function<DeserializingMessage, T> action) Captures the complete request context with this message bound as the current message.default booleanchunked()static voidcompleteLocalBatch(Throwable error) Runs and clears batch-completion callbacks and resources after a payload-first local invocation.<T> TcomputeContextIfAbsent(Class<T> contextKey, Function<DeserializingMessage, ? extends T> provider) Returns a message-scoped context value, computing and storing it when it is absent.static <K,V> V computeForBatch(K key, BiFunction<? super K, ? super V, ? extends V> function) static <K,V> V computeForBatchIfAbsent(K key, Function<? super K, ? extends V> function) static <K,V> V computeForMessageBatchIfAbsent(K key, Function<? super K, ? extends V> function) Returns one resource shared by the complete current message batch, creating it when necessary.booleancontainsMetadata(String key) Checks one metadata key without forcing an opaque serialized message to materialize its metadata wrapper.default booleanstatic voidforEachInBatch(Iterable<DeserializingMessage> batch, Consumer<? super DeserializingMessage> action) Processes messages while exposing each message throughgetCurrent()and completing batch-scoped callbacks after the last message.static <V> VgetBatchResource(Object key) static <V> VgetBatchResourceOrDefault(Object key, V defaultValue) <T> Optional<T> getContext(Class<T> contextKey) Returns a message-scoped context value.static DeserializingMessageReturns the currentDeserializingMessagebeing processed in this thread, ornullif none is set.getIndex()static intReturns the zero-based position of the current message in its tracking batch, or-1outside a batch.static <V> VReturns a previously created asynchronous message-batch resource, ornulloutside that batch.static intReturns the routing segment of the current message-batch position, or-1outside a message batch.static intReturns the known message count of the current batch, or-1when its source did not expose a size.Returns the unique ID of the underlying message.io.fluxzero.common.api.MetadatagetMetadataValue(String key) Reads one metadata value without forcing an opaque serialized message to materialize its metadata wrapper.static Optional<DeserializingMessage> Returns the currentDeserializingMessagebeing processed in this thread, if available.<V> VRetrieves the message payload, deserializing if necessary, cast to the expected type.<R> RgetPayloadAs(Type type) Retrieves the message payload, deserializing if necessary and optionally converted to the given type.Class<?> Returns the runtime class of the payload object, orVoid.classif the payload isnull.io.fluxzero.common.api.SerializedMessageio.fluxzero.common.api.SerializedMessagegetSerializedObject(Serializer fallbackSerializer) Returns the existing serialized representation or lazily creates one using the supplied fallback serializer.Returns the timestamp at which the message was created or published.getType()static Stream<DeserializingMessage> handleBatch(Stream<DeserializingMessage> batch) static booleanReturns whether this thread already has a regular deserializing-message context.booleandefault booleanputContext(Class<T> contextKey, T value) Stores a message-scoped context value.voidrun(Consumer<DeserializingMessage> task) Returns the underlyingMessagerepresentation of this object.toString()static voidwhenBatchCompletes(io.fluxzero.common.ThrowingConsumer<Throwable> executable) withData(io.fluxzero.common.api.Data<byte[]> data) withMessage(Message message) Replaces the complete logical message while retaining this wrapper's type, topic, serializer, and context.withMetadata(io.fluxzero.common.api.Metadata metadata) withoutContext(Class<?> contextKey) Returns this message after removing one framework context value using copy-on-write semantics.withPayload(Object payload) withRestoredPayload(Object payload) Exposes a restored, same-type payload to handling code while retaining this message's serialized envelope.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface HasMessage
computeRoutingKey, getRoutingKey, getRoutingKey
-
Field Details
-
messageFormatter
The formatter used to produce a human-readable representation of this message, primarily for logging or debugging. By default, this usesMessageFormatter.DEFAULT.In advanced scenarios, users may replace this field with a custom
MessageFormatterimplementation to modify how deserializing messages are rendered (e.g., to include metadata or correlation IDs). -
FIRST_CHUNK
- See Also:
-
FINAL_CHUNK
- See Also:
-
CHUNK_INDEX
- See Also:
-
-
Constructor Details
-
DeserializingMessage
public DeserializingMessage(io.fluxzero.common.api.SerializedMessage message, Function<Type, Object> payload, io.fluxzero.common.MessageType messageType, String topic, Serializer serializer) -
DeserializingMessage
public DeserializingMessage(DeserializingObject<byte[], io.fluxzero.common.api.SerializedMessage> delegate, io.fluxzero.common.MessageType messageType, String topic, Serializer serializer) -
DeserializingMessage
public DeserializingMessage(@NonNull @NonNull Message message, io.fluxzero.common.MessageType messageType, Serializer serializer) -
DeserializingMessage
public DeserializingMessage(@NonNull @NonNull Message message, io.fluxzero.common.MessageType messageType, String topic, Serializer serializer) -
DeserializingMessage
-
-
Method Details
-
run
-
apply
-
captureContext
Captures the complete request context with this message bound as the current message.This also works for framework paths that prepare an asynchronous callback outside a surrounding
apply(Function)call. The context that was active before capturing is restored immediately. -
toMessage
Description copied from interface:HasMessageReturns the underlyingMessagerepresentation of this object.- Specified by:
toMessagein interfaceHasMessage- Returns:
- the
Messagebacking this instance
-
getMetadata
public io.fluxzero.common.api.Metadata getMetadata() -
containsMetadata
Checks one metadata key without forcing an opaque serialized message to materialize its metadata wrapper. -
getMetadataValue
-
withMetadata
-
withPayload
-
withRestoredPayload
Exposes a restored, same-type payload to handling code while retaining this message's serialized envelope.This is intended for values restored from private storage, not for changing the event. Metadata-only copies retain the original data, revision and index. A subsequent
withPayload(Object)orwithMessage(Message)is a real replacement and uses ordinary serialization again.- Parameters:
payload- the restored logical payload- Returns:
- a view with the restored payload and the unchanged serialized source
-
withMessage
Replaces the complete logical message while retaining this wrapper's type, topic, serializer, and context.Unlike
withPayload(Object), this method deliberately retains the supplied message identity. It is used when one handled message expands into several independently publishable model updates. -
getMessageId
Description copied from interface:HasMessageReturns the unique ID of the underlying message.- Specified by:
getMessageIdin interfaceHasMessage- Returns:
- the message ID
-
getIndex
-
getTimestamp
Description copied from interface:HasMessageReturns the timestamp at which the message was created or published.- Specified by:
getTimestampin interfaceHasMessage- Returns:
- the message timestamp
-
isDeserialized
public boolean isDeserialized() -
getPayload
public <V> V getPayload()Description copied from interface:HasMessageRetrieves the message payload, deserializing if necessary, cast to the expected type.By default, this delegates to
toMessage().getPayload().- Specified by:
getPayloadin interfaceHasMessage- Type Parameters:
V- the expected payload type- Returns:
- the deserialized payload
-
getPayloadAs
Description copied from interface:HasMessageRetrieves the message payload, deserializing if necessary and optionally converted to the given type.By default, this performs a conversion of the payload using
JsonUtils.- Specified by:
getPayloadAsin interfaceHasMessage- Type Parameters:
R- the expected payload type- Returns:
- the payload converted to the given type
-
getPayloadClass
Description copied from interface:HasMessageReturns the runtime class of the payload object, orVoid.classif the payload isnull.- Specified by:
getPayloadClassin interfaceHasMessage- Returns:
- the payload's class
-
getType
-
getSerializedObject
public io.fluxzero.common.api.SerializedMessage getSerializedObject() -
getSerializedObject
Returns the existing serialized representation or lazily creates one using the supplied fallback serializer. The fallback is only used for programmatically created messages that were not constructed with a serializer. -
withData
-
computeContextIfAbsent
public <T> T computeContextIfAbsent(Class<T> contextKey, Function<DeserializingMessage, ? extends T> provider) Returns a message-scoped context value, computing and storing it when it is absent.- Parameters:
contextKey- the type key for the context valueprovider- the provider used to compute the value when absent- Returns:
- the existing or computed context value
-
getContext
Returns a message-scoped context value.- Parameters:
contextKey- the type key for the context value- Returns:
- the context value, or
Optional.empty()when absent
-
putContext
Stores a message-scoped context value.- Parameters:
contextKey- the type key for the context valuevalue- the context value to store- Returns:
- this message
-
withoutContext
Returns this message after removing one framework context value using copy-on-write semantics. Other messages derived from the same source retain their context. -
getCurrent
Returns the currentDeserializingMessagebeing processed in this thread, ornullif none is set.This method provides direct (nullable) access to the thread-local message context. Prefer
getOptionally()when you want to safely handle absence of context.Note: This method should typically be called only inside handler code or interceptors where a
DeserializingMessageis known to be active.- Returns:
- the current message or
nullif no message is being processed - See Also:
-
hasThreadLocalContext
public static boolean hasThreadLocalContext()Returns whether this thread already has a regular deserializing-message context.This method does not count a payload-first local execution that can be exposed through
getCurrent(). It is intended for infrastructure that must avoid nesting a regular message context.- Returns:
trueif a regular message context is bound to the current thread
-
getOptionally
Returns the currentDeserializingMessagebeing processed in this thread, if available.This method is safe to call in any thread and will return
Optional.empty()if no message is currently being handled. It is particularly useful for utility classes or exception handlers that want to conditionally access message metadata.Example
Optional<DeserializingMessage> message = DeserializingMessage.getOptionally(); message.map(DeserializingMessage::getPayloadType) .ifPresent(type -> log.debug("Handling message of type {}", type));- Returns:
- an
Optionalcontaining the current message or empty if none is set - See Also:
-
toString
-
handleBatch
-
forEachInBatch
public static void forEachInBatch(Iterable<DeserializingMessage> batch, Consumer<? super DeserializingMessage> action) Processes messages while exposing each message throughgetCurrent()and completing batch-scoped callbacks after the last message. This is the lower-allocation counterpart tohandleBatch(Stream)for callers that already have an iterable batch. -
whenBatchCompletes
-
computeForBatch
public static <K,V> V computeForBatch(K key, BiFunction<? super K, ? super V, ? extends V> function) -
computeForBatchIfAbsent
-
computeForMessageBatchIfAbsent
public static <K,V> V computeForMessageBatchIfAbsent(K key, Function<? super K, ? extends V> function) Returns one resource shared by the complete current message batch, creating it when necessary.Unlike
computeForBatchIfAbsent(Object, Function), this resource participates in captured request context and is therefore visible to asynchronous handler workers. The mapping function and returned value must consequently be safe for concurrent use. Outside message-batch handling this method returnsnull. -
getMessageBatchResource
Returns a previously created asynchronous message-batch resource, ornulloutside that batch. -
getMessageBatchIndex
public static int getMessageBatchIndex()Returns the zero-based position of the current message in its tracking batch, or-1outside a batch. -
getMessageBatchSegment
public static int getMessageBatchSegment()Returns the routing segment of the current message-batch position, or-1outside a message batch. -
getMessageBatchSize
public static int getMessageBatchSize()Returns the known message count of the current batch, or-1when its source did not expose a size. -
getBatchResource
-
getBatchResourceOrDefault
-
completeLocalBatch
Runs and clears batch-completion callbacks and resources after a payload-first local invocation.This method is intended for Fluxzero handling infrastructure. Applications normally register callbacks with
whenBatchCompletes(ThrowingConsumer)instead of completing a batch directly.- Parameters:
error- the error that ended handling, ornullafter successful handling
-
chunked
default boolean chunked() -
lastChunk
default boolean lastChunk() -
firstChunk
default boolean firstChunk()
-