Interface Message<T>
-
- Type Parameters:
T
- The type of the message payload.
- All Known Subinterfaces:
CloudEventMessage<T>
,KafkaMessage<K,T>
,KafkaRecord<K,T>
,MqttMessage<T>
- All Known Implementing Classes:
AmqpMessage
,CamelMessage
,DefaultCloudEventMessage
,EventBusMessage
,HttpMessage
,IncomingJmsMessage
,IncomingKafkaRecord
,MqttMessage
,OutgoingAmqpMessage
,OutgoingKafkaRecord
,PubSubMessage
,ReceivingMqttMessage
,SendingMqttMessage
,SnsMessage
public interface Message<T>
A message envelope.A message contains a non-
null
payload, an acknowledgement function and a set of metadata. Metadata are indexed using the class name of the values.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default CompletionStage<Void>
ack()
Acknowledge this message.default Message<T>
addMetadata(Object metadata)
Creates a new instance ofMessage
with the current metadata, plus the given one.default Supplier<CompletionStage<Void>>
getAck()
default Metadata
getMetadata()
default <M> Optional<M>
getMetadata(Class<? extends M> clazz)
Retrieves the metadata associated with the given class.T
getPayload()
static <T> Message<T>
of(T payload)
Create a message with the given payload.static <T> Message<T>
of(T payload, Iterable<Object> metadata)
Create a message with the given payload and metadata.static <T> Message<T>
of(T payload, Iterable<Object> metadata, Supplier<CompletionStage<Void>> ack)
Create a message with the given payload, metadata and ack function.static <T> Message<T>
of(T payload, Supplier<CompletionStage<Void>> ack)
Create a message with the given payload and ack function.static <T> Message<T>
of(T payload, Metadata metadata)
Create a message with the given payload and metadata.static <T> Message<T>
of(T payload, Metadata metadata, Supplier<CompletionStage<Void>> ack)
Create a message with the given payload, metadata and ack function.default <C> C
unwrap(Class<C> unwrapType)
Returns an object of the specified type to allow access to the connector-specificMessage
implementation, and other classes.default Message<T>
withAck(Supplier<CompletionStage<Void>> supplier)
Creates a new instance ofMessage
with the given acknowledgement supplier.default Message<T>
withMetadata(Iterable<Object> metadata)
Creates a new instance ofMessage
with the specified metadata.default Message<T>
withMetadata(Metadata metadata)
Creates a new instance ofMessage
with the specified metadata.default <P> Message<P>
withPayload(P payload)
Creates a new instance ofMessage
with the specified payload.
-
-
-
Method Detail
-
of
static <T> Message<T> of(T payload)
Create a message with the given payload. No metadata are associated with the message, the acknowledgement is immediate.- Type Parameters:
T
- The type of payload- Parameters:
payload
- The payload.- Returns:
- A message with the given payload, no metadata, and a no-op ack function.
-
of
static <T> Message<T> of(T payload, Metadata metadata)
Create a message with the given payload and metadata. The acknowledgement is immediate.- Type Parameters:
T
- The type of payload- Parameters:
payload
- The payload, must not benull
.metadata
- The metadata, ifnull
an empty set of metadata is used.- Returns:
- A message with the given payload, metadata and a no-op ack function.
-
of
static <T> Message<T> of(T payload, Iterable<Object> metadata)
Create a message with the given payload and metadata. The acknowledgement is immediate.- Type Parameters:
T
- The type of payload- Parameters:
payload
- The payload, must not benull
metadata
- The metadata, must not benull
, must not containnull
values, can be empty- Returns:
- A message with the given payload, metadata and a no-op ack function.
-
of
static <T> Message<T> of(T payload, Supplier<CompletionStage<Void>> ack)
Create a message with the given payload and ack function. No metadata are associated with the message.- Type Parameters:
T
- the type of payload- Parameters:
payload
- The payload, must not benull
.ack
- The ack function, this will be invoked when the returned messagesack()
method is invoked.- Returns:
- A message with the given payload, no metadata and ack function.
-
of
static <T> Message<T> of(T payload, Metadata metadata, Supplier<CompletionStage<Void>> ack)
Create a message with the given payload, metadata and ack function.- Type Parameters:
T
- the type of payload- Parameters:
payload
- The payload, must not benull
.metadata
- the metadata, ifnull
, empty metadata are used.ack
- The ack function, this will be invoked when the returned messagesack()
method is invoked.- Returns:
- A message with the given payload and ack function.
-
of
static <T> Message<T> of(T payload, Iterable<Object> metadata, Supplier<CompletionStage<Void>> ack)
Create a message with the given payload, metadata and ack function.- Type Parameters:
T
- the type of payload- Parameters:
payload
- The payload.metadata
- the metadata, must not benull
, must not containnull
values.ack
- The ack function, this will be invoked when the returned messagesack()
method is invoked.- Returns:
- A message with the given payload and ack function.
-
withPayload
default <P> Message<P> withPayload(P payload)
Creates a new instance ofMessage
with the specified payload. The metadata and acknowledgment function are taken from the currentMessage
.- Type Parameters:
P
- the type of the new payload- Parameters:
payload
- the new payload.- Returns:
- the new instance of
Message
-
withMetadata
default Message<T> withMetadata(Iterable<Object> metadata)
Creates a new instance ofMessage
with the specified metadata. The payload and acknowledgment function are taken from the currentMessage
.- Parameters:
metadata
- the metadata, must not benull
, must not containsnull
.- Returns:
- the new instance of
Message
-
withMetadata
default Message<T> withMetadata(Metadata metadata)
Creates a new instance ofMessage
with the specified metadata. The payload and acknowledgment function are taken from the currentMessage
.- Parameters:
metadata
- the metadata, must not benull
.- Returns:
- the new instance of
Message
-
withAck
default Message<T> withAck(Supplier<CompletionStage<Void>> supplier)
Creates a new instance ofMessage
with the given acknowledgement supplier. The payload and metadata are taken from the currentMessage
.- Parameters:
supplier
- the acknowledgement supplier- Returns:
- the new instance of
Message
-
getPayload
T getPayload()
- Returns:
- The payload for this message.
-
getMetadata
default Metadata getMetadata()
- Returns:
- The set of metadata attached to this message, potentially empty.
-
getMetadata
default <M> Optional<M> getMetadata(Class<? extends M> clazz)
Retrieves the metadata associated with the given class.- Parameters:
clazz
- the class of the metadata to retrieve, must not benull
- Returns:
- an
Optional
containing the associated metadata, empty if none.
-
getAck
default Supplier<CompletionStage<Void>> getAck()
- Returns:
- the supplier used to retrieve the acknowledgement
CompletionStage
.
-
ack
default CompletionStage<Void> ack()
Acknowledge this message.- Returns:
- a completion stage completed when the message is acknowledged. If the acknowledgement fails, the completion stage propagates the failure.
-
unwrap
default <C> C unwrap(Class<C> unwrapType)
Returns an object of the specified type to allow access to the connector-specificMessage
implementation, and other classes. For example, a Kafka connector could implement this method to allow unwrapping to a specific Kafka message implementation, or toConsumerRecord
andProducerRecord
. If theMessage
implementation does not support the target class, anIllegalArgumentException
should be raised.The default implementation tries to cast the current
Message
instance to the target class. When a connector provides its ownMessage
implementation, it should override this method to support specific types.- Type Parameters:
C
- the target type- Parameters:
unwrapType
- the class of the object to be returned, must not benull
- Returns:
- an instance of the specified class
- Throws:
IllegalArgumentException
- if the currentMessage
instance does not support the call
-
addMetadata
default Message<T> addMetadata(Object metadata)
Creates a new instance ofMessage
with the current metadata, plus the given one. The payload and acknowledgment function are taken from the currentMessage
.- Parameters:
metadata
- the metadata, must not benull
.- Returns:
- the new instance of
Message
-
-