Java Code Examples for org.eclipse.microprofile.reactive.messaging.Message#withPayload()

The following examples show how to use org.eclipse.microprofile.reactive.messaging.Message#withPayload() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageExamples.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
public void copy() {
    Price price = new Price(20.5);
    Message<Price> message = Message.of(price);
    // tag::copy[]

    // Create a new message with a new payload but with the same metadata
    Message<Price> m1 = message.withPayload(new Price(12.4));

    // Create a new message with a new payload and add another metadata
    Message<Price> m2 = message
        .withPayload(new Price(15.0))
        .withMetadata(Metadata.of(new PriceMetadata()));

    // Create a new message with a new payload and a custom acknowledgement
    Message<Price> m3 = message
        .withPayload(new Price(15.0))
        .withAck(() ->
            // acknowledge the incoming message
            message.ack()
                .thenAccept(x -> {
                    // do something
                }));
    // end::copy[]
}
 
Example 2
Source File: SingleSkip.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("in")
@Outgoing("out")
public Message<String> processMessage(Message<String> m) {
    String s = m.getPayload();
    if (s.equalsIgnoreCase("skip")) {
        return null;
    }
    return m.withPayload(s.toUpperCase());
}
 
Example 3
Source File: MessageAckExamples.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("in")
@Outgoing("out")
public Message<Integer> process(Message<Integer> in) {
    // The acknowledgement is forwarded, when the consumer
    // acknowledges the message, `in` will be acknowledged
    return in.withPayload(in.getPayload() + 1);
}
 
Example 4
Source File: MessageProcessingBean.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("consumed-channel")
@Outgoing("populated-channel")
public Message<String> process(Message<String> in) {
    // Process the payload
    String payload = in.getPayload().toUpperCase();
    // Create a new message from `in` and just update the payload
    return in.withPayload(payload);
}
 
Example 5
Source File: SingleSkipTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("in")
@Outgoing("out-2")
public Message<String> processMessage(Message<String> m) {
    String s = m.getPayload();
    if (s.equalsIgnoreCase("skip")) {
        return null;
    }
    return m.withPayload(s.toUpperCase());
}
 
Example 6
Source File: MessageTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testFrom() {
    Message<String> message = Message.of("hello");
    assertThat(message.getPayload()).isEqualTo("hello");
    assertThat(message.getMetadata()).isEmpty();
    assertThat(message.ack()).isCompleted();

    Message<String> message2 = message.withMetadata(Metadata.of(new MyMetadata<>("bar")));
    assertThat(message2.getPayload()).isEqualTo("hello");
    assertThat(message2.getMetadata()).hasSize(1);
    assertThat(message2.getMetadata(MyMetadata.class).map(m -> m.v)).hasValue("bar");
    assertThat(message2.ack()).isCompleted();
    assertThat(message).isNotEqualTo(message2);

    Message<String> message3 = message2.withAck(CompletableFuture::new);
    assertThat(message3.getPayload()).isEqualTo("hello");
    assertThat(message3.getMetadata()).hasSize(1);
    assertThat(message3.getMetadata(MyMetadata.class).map(m -> m.v)).hasValue("bar");
    assertThat(message3.ack()).isNotCompleted();
    assertThat(message3).isNotEqualTo(message2).isNotEqualTo(message);

    Message<List<String>> message4 = message3.withPayload(Collections.singletonList("foo"));
    assertThat(message4.getPayload()).containsExactly("foo");
    assertThat(message4.getMetadata()).hasSize(1);
    assertThat(message4.getMetadata(MyMetadata.class).map(m -> m.v)).hasValue("bar");
    assertThat(message4.ack()).isNotCompleted();
    assertThat(message4).isNotEqualTo(message2).isNotEqualTo(message3).isNotEqualTo(message);
}
 
Example 7
Source File: KafkaNackPropagationTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("output")
@Outgoing("kafka")
public Message<Integer> handle(Message<Integer> record) {
    if (record.getPayload().equals(6)) {
        record.nack(new IllegalArgumentException()).toCompletableFuture().join();
        return null;
    } else {
        received.add(record.getPayload());
        record.ack().toCompletableFuture().join();
    }
    return record.withPayload(record.getPayload() + 1);
}
 
Example 8
Source File: KafkaNackPropagationTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("output")
@Outgoing("kafka")
public Message<Integer> handle(Message<Integer> record) {
    if (record.getPayload().equals(3)) {
        record.nack(new IllegalArgumentException()).toCompletableFuture().join();
        return null;
    } else {
        received.add(record.getPayload());
        record.ack().toCompletableFuture().join();
    }
    return record.withPayload(record.getPayload() + 1);
}
 
Example 9
Source File: InMemoryConnectorTest.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("foo")
@Outgoing("bar")
public Message<String> process(Message<String> s) {
    return s.withPayload(s.getPayload().toUpperCase());
}
 
Example 10
Source File: ProcessingExamples.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("in")
@Outgoing("out")
public Message<String> processMessage(Message<Integer> in) {
    return in.withPayload(Integer.toString(in.getPayload()));
}
 
Example 11
Source File: MetadataWithMessageChainExamples.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("source")
@Outgoing("sink")
public Message<Integer> increment(Message<Integer> in) {
    return in.withPayload(in.getPayload() + 1);
}
 
Example 12
Source File: BeanWithProcessorsManipulatingMessages.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming(DEFAULT_ACKNOWLEDGMENT)
@Outgoing("sink")
public Message<String> processorWithDefaultAck(Message<String> input) {
    processed(DEFAULT_ACKNOWLEDGMENT, input);
    return input.withPayload(input.getPayload() + "1");
}
 
Example 13
Source File: AckChainTest.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("B")
@Outgoing("C")
public Message<String> process(Message<Integer> m) {
    return m.withPayload(Integer.toString(m.getPayload()));
}