Search in sources :

Example 1 with AmqpMessage

use of io.vertx.mutiny.amqp.AmqpMessage in project quarkus-quickstarts by quarkusio.

the class QuoteProcessorTest method testProcessor.

@Test
void testProcessor() {
    AmqpConnection connection = client.connectAndAwait();
    AmqpReceiver quotes = connection.createReceiverAndAwait("quotes");
    AssertSubscriber<AmqpMessage> subscriber = quotes.toMulti().subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    AmqpSender sender = connection.createSenderAndAwait("quote-requests");
    UUID quoteId = UUID.randomUUID();
    sender.sendWithAckAndAwait(AmqpMessage.create().address("quote-requests").withBody(quoteId.toString()).build());
    subscriber.awaitItems(1);
    AmqpMessage received = subscriber.getItems().get(0);
    assertEquals(received.bodyAsJsonObject().getString("id"), quoteId.toString());
}
Also used : AmqpConnection(io.vertx.mutiny.amqp.AmqpConnection) AmqpReceiver(io.vertx.mutiny.amqp.AmqpReceiver) AmqpSender(io.vertx.mutiny.amqp.AmqpSender) UUID(java.util.UUID) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 2 with AmqpMessage

use of io.vertx.mutiny.amqp.AmqpMessage in project smallrye-reactive-messaging by smallrye.

the class AmqpMessageConverter method convertToAmqpMessage.

static io.vertx.mutiny.amqp.AmqpMessage convertToAmqpMessage(Message<?> message, boolean durable, long ttl) {
    Object payload = message.getPayload();
    OutgoingAmqpMetadata metadata = message.getMetadata(OutgoingAmqpMetadata.class).orElse(new OutgoingAmqpMetadata());
    org.apache.qpid.proton.message.Message output = org.apache.qpid.proton.message.Message.Factory.create();
    // Header
    if (metadata.isDurable()) {
        output.setDurable(true);
    } else {
        output.setDurable(durable);
    }
    output.setPriority(metadata.getPriority());
    if (metadata.getTtl() > 0) {
        output.setTtl(metadata.getTtl());
    } else if (ttl > 0) {
        output.setTtl(ttl);
    }
    // Annotations
    DeliveryAnnotations deliveryAnnotations = metadata.getDeliveryAnnotations();
    MessageAnnotations messageAnnotations = metadata.getMessageAnnotations();
    if (!deliveryAnnotations.getValue().isEmpty()) {
        output.setDeliveryAnnotations(deliveryAnnotations);
    }
    if (!messageAnnotations.getValue().isEmpty()) {
        output.setMessageAnnotations(messageAnnotations);
    }
    // Properties
    output.setMessageId(metadata.getMessageId());
    output.setUserId(metadata.getUserId() != null ? metadata.getUserId().getBytes() : null);
    output.setAddress(metadata.getAddress());
    output.setSubject(metadata.getSubject());
    output.setReplyTo(metadata.getReplyTo());
    output.setCorrelationId(metadata.getCorrelationId());
    output.setContentType(metadata.getContentType());
    output.setContentEncoding(metadata.getContentEncoding());
    output.setExpiryTime(metadata.getExpiryTime());
    output.setCreationTime(metadata.getCreationTime());
    output.setGroupId(metadata.getGroupId());
    output.setGroupSequence(metadata.getGroupSequence());
    output.setReplyToGroupId(metadata.getReplyToGroupId());
    if (!metadata.getProperties().isEmpty()) {
        output.setApplicationProperties(new ApplicationProperties(metadata.getProperties().getMap()));
    }
    // Application data section:
    if (payload instanceof String || isPrimitive(payload.getClass()) || payload instanceof UUID) {
        output.setBody(new AmqpValue(payload));
    } else if (payload instanceof Buffer) {
        output.setBody(new Data(new Binary(((Buffer) payload).getBytes())));
        if (output.getContentType() == null) {
            output.setContentType(BINARY_CONTENT_TYPE);
        }
    } else if (payload instanceof io.vertx.core.buffer.Buffer) {
        output.setBody(new Data(new Binary(((io.vertx.core.buffer.Buffer) payload).getBytes())));
        if (output.getContentType() == null) {
            output.setContentType(BINARY_CONTENT_TYPE);
        }
    } else if (payload instanceof Instant) {
        output.setBody(new AmqpValue(Date.from((Instant) payload)));
    } else if (payload instanceof JsonArray) {
        byte[] bytes = ((JsonArray) payload).toBuffer().getBytes();
        output.setBody(new Data(new Binary(bytes)));
        if (output.getContentType() == null) {
            output.setContentType(JSON_CONTENT_TYPE);
        }
    } else if (payload instanceof JsonObject) {
        byte[] bytes = ((JsonObject) payload).toBuffer().getBytes();
        output.setBody(new Data(new Binary(bytes)));
        if (output.getContentType() == null) {
            output.setContentType(JSON_CONTENT_TYPE);
        }
    } else if (payload instanceof byte[]) {
        output.setBody(new Data(new Binary(((byte[]) payload))));
        if (output.getContentType() == null) {
            output.setContentType(BINARY_CONTENT_TYPE);
        }
    } else if (payload instanceof Map || payload instanceof List) {
        // This branch must be after the JSON Object and JSON Array checks
        output.setBody(new AmqpValue(payload));
    } else {
        byte[] bytes = Json.encodeToBuffer(payload).getBytes();
        output.setBody(new Data(new Binary(bytes)));
        if (output.getContentType() == null) {
            output.setContentType(JSON_CONTENT_TYPE);
        }
    }
    // Footer
    Footer footer = metadata.getFooter();
    if (!footer.getValue().isEmpty()) {
        output.setFooter(footer);
    }
    return new AmqpMessage(new AmqpMessageImpl(output));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) AmqpMessageImpl(io.vertx.amqp.impl.AmqpMessageImpl) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) List(java.util.List) UUID(java.util.UUID) Buffer(io.vertx.mutiny.core.buffer.Buffer) Instant(java.time.Instant) DeliveryAnnotations(org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations) Data(org.apache.qpid.proton.amqp.messaging.Data) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) JsonArray(io.vertx.core.json.JsonArray) Footer(org.apache.qpid.proton.amqp.messaging.Footer) JsonObject(io.vertx.core.json.JsonObject) Binary(org.apache.qpid.proton.amqp.Binary) Map(java.util.Map)

Example 3 with AmqpMessage

use of io.vertx.mutiny.amqp.AmqpMessage in project smallrye-reactive-messaging by smallrye.

the class CloudEventProductionTest method testWithCloudEventDisabled.

@Test
public void testWithCloudEventDisabled() {
    String address = UUID.randomUUID().toString();
    new MapBasedConfig().with("mp.messaging.outgoing.amqp.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.outgoing.amqp.address", address).with("mp.messaging.outgoing.amqp.host", host).with("mp.messaging.outgoing.amqp.port", port).with("mp.messaging.outgoing.amqp.tracing-enabled", false).with("mp.messaging.outgoing.amqp.cloud-events", false).with("amqp-username", username).with("amqp-password", password).write();
    weld.addBeanClass(AmqpSender.class);
    container = weld.initialize();
    AmqpSender bean = container.getBeanManager().createInstance().select(AmqpSender.class).get();
    Emitter<JsonObject> emitter = bean.get();
    List<io.vertx.mutiny.amqp.AmqpMessage> list = new ArrayList<>();
    usage.consume(address, list::add);
    Message<JsonObject> msg = Message.of(new JsonObject().put("message", "hello")).addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withId("some id").build());
    emitter.send(msg);
    await().until(() -> list.size() == 1);
    AmqpMessage message = list.get(0);
    assertThat(message.address()).isEqualTo(address);
    assertThat(message.contentType()).isEqualTo("application/json");
    JsonObject app = message.applicationProperties();
    assertThat(app).isNull();
    JsonObject body = message.bodyAsJsonObject();
    assertThat(body.getString("message")).isEqualTo("hello");
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) Test(org.junit.jupiter.api.Test)

Example 4 with AmqpMessage

use of io.vertx.mutiny.amqp.AmqpMessage in project smallrye-reactive-messaging by smallrye.

the class CloudEventProductionTest method testSendingStructuredCloudEvents.

@Test
public void testSendingStructuredCloudEvents() {
    String address = UUID.randomUUID().toString();
    new MapBasedConfig().with("mp.messaging.outgoing.amqp.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.outgoing.amqp.address", address).with("mp.messaging.outgoing.amqp.host", host).with("mp.messaging.outgoing.amqp.port", port).with("mp.messaging.outgoing.amqp.tracing-enabled", false).with("mp.messaging.outgoing.amqp.cloud-events-mode", "structured").with("amqp-username", username).with("amqp-password", password).write();
    weld.addBeanClass(AmqpSender.class);
    container = weld.initialize();
    AmqpSender bean = container.getBeanManager().createInstance().select(AmqpSender.class).get();
    Emitter<JsonObject> emitter = bean.get();
    List<io.vertx.mutiny.amqp.AmqpMessage> list = new ArrayList<>();
    usage.consume(address, list::add);
    Message<JsonObject> msg = Message.of(new JsonObject().put("message", "hello")).addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").withId("some id").build());
    emitter.send(msg);
    await().until(() -> list.size() == 1);
    AmqpMessage message = list.get(0);
    assertThat(message.address()).isEqualTo(address);
    assertThat(message.contentType()).isEqualTo(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE);
    JsonObject json = message.bodyAsJsonObject();
    assertThat(json.getString("specversion")).isEqualTo("1.0");
    assertThat(json.getString("type")).isEqualTo("type");
    assertThat(json.getString("source")).isEqualTo("test://test");
    assertThat(json.getString("id")).isEqualTo("some id");
    assertThat(json.getJsonObject("data").getString("message")).isEqualTo("hello");
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) Test(org.junit.jupiter.api.Test)

Example 5 with AmqpMessage

use of io.vertx.mutiny.amqp.AmqpMessage in project smallrye-reactive-messaging by smallrye.

the class CloudEventProductionTest method testSendingStructuredCloudEventsWithTimestampAndSubject.

@Test
public void testSendingStructuredCloudEventsWithTimestampAndSubject() {
    String address = UUID.randomUUID().toString();
    new MapBasedConfig().with("mp.messaging.outgoing.amqp.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.outgoing.amqp.address", address).with("mp.messaging.outgoing.amqp.host", host).with("mp.messaging.outgoing.amqp.port", port).with("mp.messaging.outgoing.amqp.tracing-enabled", false).with("mp.messaging.outgoing.amqp.cloud-events-mode", "structured").with("amqp-username", username).with("amqp-password", password).write();
    weld.addBeanClass(AmqpSender.class);
    container = weld.initialize();
    AmqpSender bean = container.getBeanManager().createInstance().select(AmqpSender.class).get();
    Emitter<JsonObject> emitter = bean.get();
    List<io.vertx.mutiny.amqp.AmqpMessage> list = new ArrayList<>();
    usage.consume(address, list::add);
    ZonedDateTime time = ZonedDateTime.now();
    Message<JsonObject> msg = Message.of(new JsonObject().put("message", "hello")).addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").withId("some id").withTimestamp(time).withSubject("subject").build());
    emitter.send(msg);
    await().until(() -> list.size() == 1);
    AmqpMessage message = list.get(0);
    assertThat(message.address()).isEqualTo(address);
    assertThat(message.contentType()).isEqualTo(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE);
    JsonObject json = message.bodyAsJsonObject();
    assertThat(json.getString("specversion")).isEqualTo("1.0");
    assertThat(json.getString("type")).isEqualTo("type");
    assertThat(json.getString("source")).isEqualTo("test://test");
    assertThat(json.getString("id")).isEqualTo("some id");
    assertThat(json.getJsonObject("data").getString("message")).isEqualTo("hello");
    assertThat(json.getString("subject")).isEqualTo("subject");
    assertThat(json.getInstant("time")).isNotNull();
    assertThat(json.getInstant("time").getEpochSecond()).isEqualTo(time.toEpochSecond());
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) ZonedDateTime(java.time.ZonedDateTime) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

Aggregations

AmqpMessage (io.vertx.mutiny.amqp.AmqpMessage)12 JsonObject (io.vertx.core.json.JsonObject)10 Test (org.junit.jupiter.api.Test)10 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)9 ArrayList (java.util.ArrayList)9 AmqpMessageImpl (io.vertx.amqp.impl.AmqpMessageImpl)2 UUID (java.util.UUID)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 JsonArray (io.vertx.core.json.JsonArray)1 AmqpConnection (io.vertx.mutiny.amqp.AmqpConnection)1 AmqpMessageBuilder (io.vertx.mutiny.amqp.AmqpMessageBuilder)1 AmqpReceiver (io.vertx.mutiny.amqp.AmqpReceiver)1 AmqpSender (io.vertx.mutiny.amqp.AmqpSender)1 Buffer (io.vertx.mutiny.core.buffer.Buffer)1 Instant (java.time.Instant)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Binary (org.apache.qpid.proton.amqp.Binary)1