-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce GenericPayload primarily for sending outgoing payloads with…
… additional metadata
- Loading branch information
1 parent
d2b5012
commit ddd895d
Showing
20 changed files
with
570 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
api/src/main/java/io/smallrye/reactive/messaging/GenericPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package io.smallrye.reactive.messaging; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Message; | ||
import org.eclipse.microprofile.reactive.messaging.Metadata; | ||
|
||
/** | ||
* A generic payload that can be used to wrap a payload with metadata. | ||
* Allows associating a payload with metadata to be sent as a message, | ||
* without using signatures supporting {@code Message<T>}. | ||
* | ||
* @param <T> the type of the payload | ||
*/ | ||
public class GenericPayload<T> { | ||
|
||
/** | ||
* Creates a new payload with the given payload and empty metadata. | ||
* | ||
* @param payload the payload | ||
* @param <T> the type of the payload | ||
* @return the payload | ||
*/ | ||
public static <T> GenericPayload<T> of(T payload) { | ||
return new GenericPayload<>(payload, Metadata.empty()); | ||
} | ||
|
||
/** | ||
* Creates a new payload with the given payload and metadata. | ||
* | ||
* @param payload the payload | ||
* @param metadata the metadata | ||
* @param <T> the type of the payload | ||
* @return the payload | ||
*/ | ||
public static <T> GenericPayload<T> of(T payload, Metadata metadata) { | ||
return new GenericPayload<>(payload, metadata); | ||
} | ||
|
||
/** | ||
* Creates a new payload from the given message. | ||
* | ||
* @param message the message | ||
* @param <T> the type of the payload | ||
* @return the payload | ||
*/ | ||
public static <T> GenericPayload<T> from(Message<T> message) { | ||
return new GenericPayload<>(message.getPayload(), message.getMetadata()); | ||
} | ||
|
||
private final T payload; | ||
private final Metadata metadata; | ||
|
||
public GenericPayload(T payload, Metadata metadata) { | ||
this.payload = payload; | ||
this.metadata = metadata; | ||
} | ||
|
||
/** | ||
* Gets the payload associated with this payload. | ||
* | ||
* @return the payload | ||
*/ | ||
public T getPayload() { | ||
return payload; | ||
} | ||
|
||
/** | ||
* Gets the metadata associated with this payload. | ||
* | ||
* @return the metadata | ||
*/ | ||
public Metadata getMetadata() { | ||
return metadata; | ||
} | ||
|
||
/** | ||
* Adds metadata to this payload. | ||
* | ||
* @param metadata the metadata to add | ||
* @return a new payload with the added metadata | ||
*/ | ||
public GenericPayload<T> withMetadata(Metadata metadata) { | ||
return GenericPayload.of(this.payload, metadata); | ||
} | ||
|
||
/** | ||
* Adds metadata to this payload. | ||
* | ||
* @param payload the payload to add | ||
* @return a new payload with the added metadata | ||
*/ | ||
public <R> GenericPayload<R> withPayload(R payload) { | ||
return GenericPayload.of(payload, this.metadata); | ||
} | ||
|
||
/** | ||
* Converts this payload to a message. | ||
* | ||
* @return the message with the payload and metadata | ||
*/ | ||
public Message<T> toMessage() { | ||
return Message.of(payload, metadata); | ||
} | ||
|
||
/** | ||
* Converts this payload to a message, merging the metadata with the given message. | ||
* | ||
* @param message the message to merge the metadata with | ||
* @return the message with the payload and merged metadata | ||
*/ | ||
public Message<T> toMessage(Message<?> message) { | ||
Metadata merged = Messages.merge(message.getMetadata(), this.metadata); | ||
return message.withPayload(payload).withMetadata(merged); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generic Payloads | ||
|
||
!!!warning "Experimental" | ||
Generic payloads are an experimental feature and the API is subject to change. | ||
|
||
When using reactive messaging, `Message` flow in your system | ||
each message has a payload but can also contain _metadata_, as explained in [Messages, Payload, Metadata](concepts.md#messages-payload-metadata). | ||
The metadata can hold information, for example in an outgoing channel, additional properties of the outgoing message to be sent to the broker. | ||
|
||
It is sometimes preferable to continue using the [payload signatures](model.md#messages-vs-payloads), | ||
and also being able to attach metadata. | ||
Using `GenericPayload` allows customizing metadata when handling payloads in SmallRye Reactive Messaging `@Incoming` and `@Outgoing` methods. | ||
`GenericPayload` is a wrapper type, like the `Message`, containing a payload and metadata, | ||
without requiring handling acknowledgments manually. | ||
|
||
``` java | ||
{{ insert('genericpayload/GenericPayloadExample.java', 'code') }} | ||
``` | ||
|
||
You can combine generic payloads with [metadata injection](incoming-metadata-injection.md) : | ||
|
||
|
||
``` java | ||
{{ insert('genericpayload/GenericPayloadExample.java', 'injection') }} | ||
``` | ||
|
||
Note that the metadata provided with the outgoing generic payload is merged with the incoming message metadata. | ||
|
||
!!! warning "Limitations" | ||
There are several limitations for the use of `GenericPayload`: | ||
`GenericPayload` is not supported in emitters, as normal outgoing `Message` can be used for that purpose. | ||
While `GenericPayload<T>` can be used as an incoming payload type, | ||
[message converters](converters.md) are not applied to the payload type `T`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
documentation/src/main/java/genericpayload/GenericPayloadExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package genericpayload; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Incoming; | ||
import org.eclipse.microprofile.reactive.messaging.Metadata; | ||
import org.eclipse.microprofile.reactive.messaging.Outgoing; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.reactive.messaging.GenericPayload; | ||
import messages.MyMetadata; | ||
|
||
@ApplicationScoped | ||
public class GenericPayloadExample { | ||
|
||
// <code> | ||
@Outgoing("out") | ||
Multi<GenericPayload<String>> produce() { | ||
return Multi.createFrom().range(0, 100) | ||
.map(i -> GenericPayload.of(">> " + i, Metadata.of(new MyMetadata()))); | ||
} | ||
// </code> | ||
|
||
// <injection> | ||
@Incoming("in") | ||
@Outgoing("out") | ||
GenericPayload<String> process(int payload, MyMetadata metadata) { | ||
// use the injected metadata | ||
String id = metadata.getId(); | ||
return GenericPayload.of(">> " + payload + " " + id, | ||
Metadata.of(metadata, new MyMetadata("Bob", "Alice"))); | ||
} | ||
// </injection> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.