Interface Message
- All Known Subinterfaces:
CommandMessage,CommandResultMessage,EventMessage,QueryMessage,QueryResponseMessage,ResetContext,ResultMessage,SubscriptionQueryUpdateMessage
- All Known Implementing Classes:
AbstractMessage,GenericCommandMessage,GenericCommandResultMessage,GenericEventMessage,GenericMessage,GenericQueryMessage,GenericQueryResponseMessage,GenericResetContext,GenericResultMessage,GenericSubscriptionQueryUpdateMessage,MessageDecorator,TerminalEventMessage
Message, containing a type, payload, and
Metadata.
Typical examples of a Messages are commands,
events, and
queries.
Instead of implementing Message directly, consider implementing CommandMessage, EventMessage
or QueryMessage instead.
- Since:
- 2.0.0
- Author:
- Allard Buijze, Steven van Beelen
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic ProcessingContextaddToContext(ProcessingContext context, Message message) andMetadata(Map<String, String> metadata) static MessagefromContext(ProcessingContext context) Returns the identifier of thisMessage.metadata()Returns theMetadatafor thisMessage.payload()Returns the payload of thisMessage.default <T> TReturns the payload of thisMessagecasted to the given type ifpayloadType()isassignable fromthe giventype, otherwise throws aConversionException.default <T> TReturns the payload of thisMessage, converted to the giventypeby the givenconverter.<T> TReturns the payload of thisMessage, converted to the giventypeby the givenconverter.default <T> TpayloadAs(TypeReference<T> type) Returns the payload of thisMessagecasted to the given type ifpayloadType()isassignable fromthe giventype, otherwise throws aConversionException.default <T> TpayloadAs(TypeReference<T> type, Converter converter) Returns the payload of thisMessage, converted to the giventypeby the givenconverter.Class<?> Returns the type of the payload.type()Returns the messagetypeof thisMessage.default MessagewithConvertedPayload(Class<?> type, Converter converter) Returns a newMessageimplementation with itspayload()converted to the giventypeby the givenconverter.withConvertedPayload(Type type, Converter converter) Returns a newMessageimplementation with itspayload()converted to the giventypeby the givenconverter.default MessagewithConvertedPayload(TypeReference<?> type, Converter converter) Returns a newMessageimplementation with itspayload()converted to the giventypeby the givenconverter.withMetadata(Map<String, String> metadata) Returns a copy of thisMessage(implementation) with the givenmetadata.
-
Field Details
-
RESOURCE_KEY
TheContext.ResourceKeyused to store and retrieve theMessagefrom theProcessingContext. Should always be the message for which a handler is being called. For example, if an event handler is called within the context of a command, the message should be the event message.
-
-
Method Details
-
addToContext
@Nonnull static ProcessingContext addToContext(@Nonnull ProcessingContext context, @Nonnull Message message) Adds the givenmessageto the givencontextunder theRESOURCE_KEY. This allows retrieving the message from the context later on, for example in a message handler.Note that as the
ProcessingContextmight not be mutable in all implementations, this method returns a newProcessingContextinstance which should be used in place of the original.- Parameters:
context- TheProcessingContextto which themessageshould be added.message- TheMessageto add to thecontext.- Returns:
- The updated
ProcessingContextwith themessageadded under theRESOURCE_KEY.
-
fromContext
- Parameters:
context- TheProcessingContextfrom which to retrieve theMessage.- Returns:
- The
Messagestored in thecontextunder theRESOURCE_KEY, ornullif not found.
-
identifier
Returns the identifier of thisMessage.Two messages with the same identifiers should be interpreted as different representations of the same conceptual message. In such case, the
metadatamay be different for both representations. Thepayloadmay be identical.- Returns:
- The unique identifier of this
Message.
-
type
Returns the messagetypeof thisMessage.- Returns:
- The message
typeof thisMessage.
-
payload
Returns the payload of thisMessage.The payload is the application-specific information.
- Returns:
- The payload of this
Message.
-
payloadAs
Returns the payload of thisMessage, converted to the giventypeby the givenconverter.If
payloadType()isassignable fromthe giventype,payload()may be invoked instead of using the givenconverter.Implementers of this operation may optimize by storing the converted payloads, thus saving a
Converter.convert(Object, Class)invocation in the process. Only when this optimization is in place will anull converterresult in a successful invocation of this method.- Type Parameters:
T- The generic type to convert thisMessage'spayload to.- Parameters:
type- The type to convert thisMessage'spayload to.converter- The converter to convert thisMessage'spayload with.- Returns:
- The payload of this
Message, converted to the giventype. - Throws:
ConversionException- Whenconversionis mandatory but noconverteris given.
-
payloadAs
Returns the payload of thisMessagecasted to the given type ifpayloadType()isassignable fromthe giventype, otherwise throws aConversionException.- Type Parameters:
T- The generic type to convert thisMessage'spayload to.- Parameters:
type- The type to convert thisMessage'spayload to.- Returns:
- The payload of this
Message, converted to the giventype. - Throws:
ConversionException- When the given type is not compatible with the payload type.
-
payloadAs
Returns the payload of thisMessage, converted to the giventypeby the givenconverter.If
payloadType()isassignable fromthe givenTypeReference.getType(),payload()may be invoked instead of using the givenconverter.Implementers of this operation may optimize by storing the converted payloads, thus saving a
Converter.convert(Object, Class)invocation in the process. Only when this optimization is in place will anull converterresult in a successful invocation of this method.- Type Parameters:
T- The generic type to convert thisMessage'spayload to.- Parameters:
type- The type to convert thisMessage'spayload to.converter- The converter to convert thisMessage'spayload with.- Returns:
- The payload of this
Message, converted to the giventype. - Throws:
ConversionException- Whenconversionis mandatory but noconverteris given.
-
payloadAs
Returns the payload of thisMessagecasted to the given type ifpayloadType()isassignable fromthe giventype, otherwise throws aConversionException.- Type Parameters:
T- The generic type to convert thisMessage'spayload to.- Parameters:
type- The type to convert thisMessage'spayload to.- Returns:
- The payload of this
Message, converted to the giventype. - Throws:
ConversionException- When the given type is not compatible with the payload type.
-
payloadAs
Returns the payload of thisMessage, converted to the giventypeby the givenconverter.If the given
typeis an instance ofClassandpayloadType()isassignable fromthatClass,payload()may be invoked instead of using the givenconverter.Implementers of this operation may optimize by storing the converted payloads, thus saving a
Converter.convert(Object, Class)invocation in the process. Only when this optimization is in place will anull converterresult in a successful invocation of this method.- Type Parameters:
T- The generic type to convert thisMessage'spayload to.- Parameters:
type- The type to convert thisMessage'spayload to.converter- The converter to convert thisMessage'spayload with.- Returns:
- The payload of this
Message, converted to the giventype. - Throws:
ConversionException- Whenconversionis mandatory but noconverteris given.
-
payloadType
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.
-
metadata
Returns theMetadatafor thisMessage.The
Metadatais a collection of key-value pairs, where both the key and values areStrings.- Returns:
- The
Metadatafor thisMessage.
-
withMetadata
Returns a copy of thisMessage(implementation) with the givenmetadata.All other fields, like for example the
payload(), remain unchanged.While the implementation returned may be different from the implementation of
this, implementations must take special care in returning the same type ofMessageto prevent errors further downstream.- Parameters:
metadata- The new metadata for theMessage.- Returns:
- A copy of
this Message (implementation)with the givenmetadata.
-
andMetadata
Returns a copy of thisMessage(implementation) with itsmetadatamerged with the givenmetadata.All other fields, like for example the
payload(), remain unchanged.- Parameters:
metadata- The metadata to merge with.- Returns:
- A copy of
this Message (implementation)with the givenmetadata.
-
withConvertedPayload
Returns a newMessageimplementation with itspayload()converted to the giventypeby the givenconverter. This newMessageis effectively a copy ofthis Messagewith a renewed payload andpayloadType().Will return the
thisinstance if thepayload typeisassignable fromthe converted result.- Parameters:
type- The type to convert thepayload()to.converter- The converter to convert thepayload()with.- Returns:
- A new
Messageimplementation with itspayload()converted to the giventypeby the givenconverter. - Throws:
ConversionException- Whenconversionis mandatory but noconverteris given.
-
withConvertedPayload
@Nonnull default Message withConvertedPayload(@Nonnull TypeReference<?> type, @Nonnull Converter converter) Returns a newMessageimplementation with itspayload()converted to the giventypeby the givenconverter. This newMessageis effectively a copy ofthis Messagewith a renewed payload andpayloadType().Will return the
thisinstance if thepayload typeisassignable fromthe converted result.- Parameters:
type- The type to convert thepayload()to.converter- The converter to convert thepayload()with.- Returns:
- A new
Messageimplementation with itspayload()converted to the giventypeby the givenconverter. - Throws:
ConversionException- Whenconversionis mandatory but noconverteris given.
-
withConvertedPayload
Returns a newMessageimplementation with itspayload()converted to the giventypeby the givenconverter. This newMessageis effectively a copy ofthis Messagewith a renewed payload andpayloadType().Will return the
thisinstance if thepayload typeisassignable fromthe converted result.- Parameters:
type- The type to convert thepayload()to.converter- The converter to convert thepayload()with.- Returns:
- A new
Messageimplementation with itspayload()converted to the giventypeby the givenconverter. - Throws:
ConversionException- Whenconversionis mandatory but noconverteris given.
-