Search in sources :

Example 1 with AmqpMessageImpl

use of io.vertx.amqp.impl.AmqpMessageImpl 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 2 with AmqpMessageImpl

use of io.vertx.amqp.impl.AmqpMessageImpl in project smallrye-reactive-messaging by smallrye.

the class AmqpCreditBasedSender method send.

private Uni<Message<?>> send(AmqpSender sender, Message<?> msg, boolean durable, long ttl, String configuredAddress, boolean isAnonymousSender) {
    io.vertx.mutiny.amqp.AmqpMessage amqp;
    OutgoingCloudEventMetadata<?> ceMetadata = msg.getMetadata(OutgoingCloudEventMetadata.class).orElse(null);
    if (msg instanceof AmqpMessage) {
        amqp = ((AmqpMessage<?>) msg).getAmqpMessage();
    } else if (msg.getPayload() instanceof io.vertx.mutiny.amqp.AmqpMessage) {
        amqp = (io.vertx.mutiny.amqp.AmqpMessage) msg.getPayload();
    } else if (msg.getPayload() instanceof io.vertx.amqp.AmqpMessage) {
        amqp = new io.vertx.mutiny.amqp.AmqpMessage((io.vertx.amqp.AmqpMessage) msg.getPayload());
    } else if (msg.getPayload() instanceof org.apache.qpid.proton.message.Message) {
        org.apache.qpid.proton.message.Message message = (org.apache.qpid.proton.message.Message) msg.getPayload();
        AmqpMessageImpl vertxMessage = new AmqpMessageImpl(message);
        amqp = new io.vertx.mutiny.amqp.AmqpMessage(vertxMessage);
    } else {
        amqp = AmqpMessageConverter.convertToAmqpMessage(msg, durable, ttl);
    }
    if (writeCloudEvents && (ceMetadata != null || mandatoryCloudEventAttributeSet)) {
        // - or if the message does not contain this metadata, the type and source are configured on the channel
        if (writeAsBinaryCloudEvent) {
            amqp = AmqpCloudEventHelper.createBinaryCloudEventMessage(amqp, ceMetadata, this.configuration);
        } else {
            amqp = AmqpCloudEventHelper.createStructuredEventMessage(amqp, ceMetadata, this.configuration);
        }
    }
    String actualAddress = getActualAddress(msg, amqp, configuredAddress, isAnonymousSender);
    if (connector.getClients().isEmpty()) {
        log.messageNoSend(actualAddress);
        return Uni.createFrom().item(msg);
    }
    if (!actualAddress.equals(amqp.address())) {
        amqp.getDelegate().unwrap().setAddress(actualAddress);
    }
    createOutgoingTrace(msg, amqp);
    log.sendingMessageToAddress(actualAddress);
    return sender.sendWithAck(amqp).onFailure().retry().withBackOff(ofSeconds(1), ofSeconds(retryInterval)).atMost(retryAttempts).onItemOrFailure().transformToUni((success, failure) -> {
        if (failure != null) {
            return Uni.createFrom().completionStage(msg.nack(failure));
        } else {
            return Uni.createFrom().completionStage(msg.ack());
        }
    }).onItem().transform(x -> msg);
}
Also used : OutgoingCloudEventMetadata(io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata) Message(org.eclipse.microprofile.reactive.messaging.Message) AmqpMessageImpl(io.vertx.amqp.impl.AmqpMessageImpl)

Example 3 with AmqpMessageImpl

use of io.vertx.amqp.impl.AmqpMessageImpl in project smallrye-reactive-messaging by smallrye.

the class AmqpUsage method produce.

/**
 * Use the supplied function to asynchronously produce messages and write them to the host.
 *
 * @param topic the topic, must not be null
 * @param messageCount the number of messages to produce; must be positive
 * @param messageSupplier the function to produce messages; may not be null
 */
public void produce(String topic, int messageCount, Supplier<Object> messageSupplier) {
    CountDownLatch done = new CountDownLatch(messageCount);
    client.createSender(topic).subscribe().with(sender -> {
        Thread t = new Thread(() -> {
            LOGGER.infof("Starting AMQP sender to write %s messages", messageCount);
            try {
                for (int i = 0; i != messageCount; ++i) {
                    Object payload = messageSupplier.get();
                    AmqpMessage msg;
                    if (payload instanceof AmqpMessage) {
                        msg = (AmqpMessage) payload;
                    } else if (payload instanceof io.vertx.amqp.AmqpMessage) {
                        msg = new AmqpMessage((io.vertx.amqp.AmqpMessage) payload);
                    } else if (payload instanceof Section) {
                        Message m = ProtonHelper.message();
                        m.setBody((Section) payload);
                        msg = new AmqpMessage(new AmqpMessageImpl(m));
                    } else {
                        AmqpMessageBuilder builder = io.vertx.mutiny.amqp.AmqpMessage.create().durable(false).ttl(10000);
                        if (payload instanceof Integer) {
                            builder.withIntegerAsBody((Integer) payload);
                        } else {
                            builder.withBody(payload.toString());
                        }
                        msg = builder.build();
                    }
                    sender.sendWithAck(msg).subscribe().with(x -> {
                        LOGGER.infof("Producer sent message %s", payload);
                        done.countDown();
                    }, Throwable::printStackTrace);
                }
            } catch (Exception e) {
                LOGGER.error("Unable to send message", e);
            }
        });
        t.setName(topic + "-thread");
        t.start();
    }, Throwable::printStackTrace);
    try {
        done.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    // Ignore me
    }
}
Also used : AmqpMessageBuilder(io.vertx.mutiny.amqp.AmqpMessageBuilder) Message(org.apache.qpid.proton.message.Message) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpMessage(io.vertx.mutiny.amqp.AmqpMessage) AmqpMessageImpl(io.vertx.amqp.impl.AmqpMessageImpl)

Example 4 with AmqpMessageImpl

use of io.vertx.amqp.impl.AmqpMessageImpl in project smallrye-reactive-messaging by smallrye.

the class AmqpMessageTest method testMessageAttributes.

@Test
public void testMessageAttributes() {
    Map<String, Object> props = new LinkedHashMap<>();
    props.put("hello", "world");
    props.put("some", "content");
    Message message = message();
    message.setTtl(1);
    message.setDurable(true);
    message.setReplyTo("reply");
    ApplicationProperties apps = new ApplicationProperties(props);
    message.setApplicationProperties(apps);
    message.setContentType("text/plain");
    message.setContentEncoding("encoding");
    message.setUserId("user".getBytes());
    message.setCorrelationId("1234");
    message.setDeliveryCount(2);
    message.setExpiryTime(10000);
    message.setFooter(new Footer(props));
    message.setGroupId("some-group");
    message.setAddress("address");
    message.setCreationTime(System.currentTimeMillis());
    message.setSubject("subject");
    message.setUserId("username".getBytes());
    message.setPriority((short) 2);
    message.setReplyTo("rep");
    message.setReplyToGroupId("rep-id");
    message.setBody(new AmqpValue("hello"));
    message.setMessageId("4321");
    AmqpMessage<?> msg = new AmqpMessage<>(new AmqpMessageImpl(message), null, null, false, false);
    assertThat(msg.getAddress()).isEqualTo("address");
    assertThat(msg.getApplicationProperties()).contains(entry("hello", "world"), entry("some", "content"));
    assertThat(msg.getContentType()).isEqualTo("text/plain");
    assertThat(msg.getContentEncoding()).isEqualTo("encoding");
    assertThat(msg.unwrap().getUserId()).isNotNull();
    assertThat(msg.unwrap().isFirstAcquirer()).isFalse();
    assertThat(msg.unwrap().getReplyTo()).isEqualTo("rep");
    assertThat(msg.unwrap().getReplyToGroupId()).isEqualTo("rep-id");
    assertThat(msg.getCreationTime()).isNotZero();
    assertThat(msg.getDeliveryCount()).isEqualTo(2);
    assertThat(msg.getExpiryTime()).isEqualTo(10000);
    assertThat(msg.getGroupId()).isEqualTo("some-group");
    assertThat(msg.getTtl()).isEqualTo(1);
    assertThat(msg.getSubject()).isEqualTo("subject");
    assertThat(msg.getPriority()).isEqualTo((short) 2);
    assertThat(((AmqpValue) msg.getBody()).getValue()).isEqualTo("hello");
    assertThat(msg.getCorrelationId()).isEqualTo("1234");
    assertThat(msg.getMessageId()).isEqualTo("4321");
    assertThat(msg.isDurable()).isTrue();
    assertThat(msg.getError().name()).isEqualTo("OK");
    assertThat(msg.getGroupSequence()).isZero();
}
Also used : AmqpMessageImpl(io.vertx.amqp.impl.AmqpMessageImpl) Message(org.apache.qpid.proton.message.Message) Footer(org.apache.qpid.proton.amqp.messaging.Footer) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) JsonObject(io.vertx.core.json.JsonObject) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

AmqpMessageImpl (io.vertx.amqp.impl.AmqpMessageImpl)4 JsonObject (io.vertx.core.json.JsonObject)2 AmqpMessage (io.vertx.mutiny.amqp.AmqpMessage)2 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)2 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)2 Footer (org.apache.qpid.proton.amqp.messaging.Footer)2 Message (org.apache.qpid.proton.message.Message)2 OutgoingCloudEventMetadata (io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata)1 JsonArray (io.vertx.core.json.JsonArray)1 AmqpMessageBuilder (io.vertx.mutiny.amqp.AmqpMessageBuilder)1 Buffer (io.vertx.mutiny.core.buffer.Buffer)1 Instant (java.time.Instant)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Binary (org.apache.qpid.proton.amqp.Binary)1 Data (org.apache.qpid.proton.amqp.messaging.Data)1 DeliveryAnnotations (org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations)1