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());
}
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));
}
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");
}
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");
}
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());
}
Aggregations