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

The following examples show how to use org.eclipse.microprofile.reactive.messaging.Message#of() . 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: MetadataTest.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetObject() {
    Person person = new Person();
    person.name = "mark";
    byte[] bytes = new byte[] { 1, 2, 3, 4 };

    Metadata metadata = Metadata.of(person, bytes);
    Message<String> message = Message.of("ignored", metadata);

    Person p = message.getMetadata(Person.class).orElseThrow(() -> new AssertionError("Metadata expected"));
    assertThat(p).isNotNull();
    assertThat(p.name).isEqualTo("mark");

    byte[] b = message.getMetadata(byte[].class).orElseThrow(() -> new AssertionError("Metadata expected"));
    assertThat(b).isNotNull().hasSize(4).containsExactly(1, 2, 3, 4);
}
 
Example 2
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 3
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 4
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> processAndProduceNewMessage(Message<Integer> in) {
    // The acknowledgement is forwarded, when the consumer
    // acknowledges the message, `in` will be acknowledged
    return Message.of(in.getPayload() + 1,
        () -> {
            // Called when the consumer acknowledges the message
            // ...
            // Don't forget to acknowledge the incoming message:
            return in.ack();
        });
}
 
Example 5
Source File: ConsumptionBeanUsingRawMessage.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("data")
@Outgoing("sink")
@Acknowledgment(Acknowledgment.Strategy.MANUAL)
public Message<Integer> process(Message<Integer> input) {
    kafka.add(input);
    return Message.of(input.getPayload() + 1, input::ack);
}
 
Example 6
Source File: MessageExamples.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public void creation() {
    Price price = new Price(20.5);
    // tag::creation[]

    // Create a simple message wrapping a payload
    Message<Price> m1 = Message.of(price);

    // Create a message with metadata
    Message<Price> m2 = Message.of(price, Metadata.of(new PriceMetadata()));

    // Create a message with several metadata
    Message<Price> m3 = Message.of(price,
        Metadata.of(new PriceMetadata(), new MyMetadata()));

    // Create a message with an acknowledgement callback
    Message<Price> m4 = Message.of(price, () -> {
        // Called when the message is acknowledged by the next consumer.
        return CompletableFuture.completedFuture(null);
    });

    // Create a message with both metadata and acknowledgement callback
    Message<Price> m5 = Message.of(price,
        Metadata.of(new PriceMetadata()),
        () -> {
            // Called when the message is acknowledged by the next consumer.
            return CompletableFuture.completedFuture(null);
        });
    // end::creation[]
}
 
Example 7
Source File: BeanReturningMessages.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Blocking
@Outgoing("infinite-producer")
public Message<Integer> create() {
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    threads.add(Thread.currentThread().getName());
    return Message.of(count.incrementAndGet());
}
 
Example 8
Source File: BeanWithProcessorsManipulatingMessages.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming(NO_ACKNOWLEDGMENT)
@Acknowledgment(Acknowledgment.Strategy.NONE)
@Outgoing("sink")
public Message<String> processorWithNoAck(Message<String> input) {
    processed(NO_ACKNOWLEDGMENT, input);
    return Message.of(input.getPayload() + "1");
}
 
Example 9
Source File: BeanConsumingItemsAndProducingMessages.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Blocking
@Incoming("count")
@Outgoing("sink")
public Message<String> process(int value) {
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    threads.add(Thread.currentThread().getName());
    return Message.of(Integer.toString(value + 1));
}
 
Example 10
Source File: MessageTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageCreation() {
    Message<String> message = Message.of("hello");
    assertThat(message.getPayload()).isEqualTo("hello");
    assertThat(message.getMetadata()).isEmpty();
    assertThat(message.ack()).isCompleted();

    Supplier<CompletionStage<Void>> supplier = () -> CompletableFuture.completedFuture(null);
    message = Message.of("hello", supplier);
    assertThat(message.getPayload()).isEqualTo("hello");
    assertThat(message.getMetadata()).isEmpty();
    assertThat(message.getAck()).isEqualTo(supplier);
    assertThat(message.ack()).isCompleted();

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

    message = Message.of("hello", Metadata.of(new MyMetadata<>("v")), supplier);
    assertThat(message.getPayload()).isEqualTo("hello");
    assertThat(message.getMetadata()).hasSize(1);
    assertThat(message.getMetadata(MyMetadata.class).map(m -> m.v)).hasValue("v");
    assertThat(message.getAck()).isEqualTo(supplier);
    assertThat(message.ack()).isCompleted();

    message = Message.of("hello", Metadata.of(new MyMetadata<>("v")), null);
    assertThat(message.getPayload()).isEqualTo("hello");
    assertThat(message.getMetadata()).hasSize(1);
    assertThat(message.getMetadata(MyMetadata.class).map(m -> m.v)).hasValue("v");
    assertThat(message.getAck()).isNotEqualTo(supplier);
    assertThat(message.ack()).isCompleted();
}
 
Example 11
Source File: ConsumptionBean.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("data")
@Outgoing("sink")
@Acknowledgment(Acknowledgment.Strategy.MANUAL)
public Message<Integer> process(AmqpMessage<Integer> input) {
    int value = input.getPayload();
    return Message.of(value + 1, input::ack);
}
 
Example 12
Source File: MessagingManager.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public void sendWithAcknowledgement(MockPayload payload) {
    System.out.println("Sending with ACK " + payload);
    AtomicReference<Message<MockPayload>> reference = new AtomicReference<>();
    Message<MockPayload> msg = Message.of(payload, () -> {
        System.out.println("Acknowledging " + payload);
        inflights.remove(reference.get());
        return CompletableFuture.completedFuture(null);
    });
    reference.set(msg);
    send(msg);
}
 
Example 13
Source File: JmsSinkTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropagation() throws JMSException {
    MapBasedConfig config = new MapBasedConfig.Builder()
            .put("destination", "queue-one")
            .put("channel-name", "jms")
            .put("ttl", 10000L)
            .build();
    JmsSink sink = new JmsSink(jms, new JmsConnectorOutgoingConfiguration(config), json, executor);
    MyJmsClient client = new MyJmsClient(jms.createQueue("queue-one"));
    subscriber = sink.getSink().build();
    subscriber.onSubscribe(new Subscriptions.EmptySubscription());
    AtomicBoolean acked = new AtomicBoolean();
    Message<String> hello = Message.of("hello",
            () -> CompletableFuture.runAsync(() -> acked.set(true)));

    Destination rt = jms.createQueue("reply-to");

    OutgoingJmsMessageMetadata metadata = OutgoingJmsMessageMetadata.builder()
            .withCorrelationId("my-correlation-id")
            .withReplyTo(rt)
            .withDeliveryMode(DeliveryMode.PERSISTENT)
            .withType(String.class.getName())
            .build();

    hello = hello.withMetadata(Collections.singleton(metadata));
    subscriber.onNext(hello);

    await().until(() -> client.messages.size() >= 1);
    assertThat(acked).isTrue();
    javax.jms.Message message = client.messages.get(0);
    assertThat(message.getBody(String.class)).isEqualTo("hello");
    assertThat(message.getJMSCorrelationID()).isEqualTo("my-correlation-id");
    assertThat(message.getJMSReplyTo()).isEqualTo(rt);
    assertThat(message.getJMSDeliveryMode()).isEqualTo(2);
    assertThat(message.getJMSType()).isEqualTo(String.class.getName());

}
 
Example 14
Source File: ProducingBean.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("data")
@Outgoing("sink")
@Acknowledgment(Acknowledgment.Strategy.MANUAL)
public Message<Integer> process(Message<Integer> input) {
    return Message.of(input.getPayload() + 1, input::ack);
}
 
Example 15
Source File: ConsumptionBean.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("data")
@Outgoing("sink")
public Message<Integer> process(MqttMessage<byte[]> input) {
    String s = new String(input.getPayload());
    return Message.of(Integer.parseInt(s) + 1);
}
 
Example 16
Source File: DirectProcessorBean.java    From microprofile-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("publisher-synchronous-message")
@Outgoing("synchronous-message")
public Message<String> messageSynchronous(Message<Integer> message) {
  increment("synchronous-message");
  return Message.of(Integer.toString(message.getPayload() + 1));
}
 
Example 17
Source File: BeanConsumingItemsAndProducingMessages.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("count")
@Outgoing("sink")
public Message<String> process(int value) {
    return Message.of(Integer.toString(value + 1));
}
 
Example 18
Source File: HttpSinkTest.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Incoming("numbers")
@Outgoing("http")
public Message<JsonObject> sink(int i) {
    return Message.of(new JsonObject().put("value", i + 1));
}
 
Example 19
Source File: GenerationExamples.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Outgoing("my-channel")
public Message<Integer> generateMessagesSynchronously() {
    return Message.of(counter.getAndIncrement());
}
 
Example 20
Source File: PublisherSignatureTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Outgoing("F")
public Message<Integer> produce() {
    return Message.of(count.getAndIncrement());
}