use of com.rabbitmq.stream.MessageBuilder in project rabbitmq-stream-java-client by rabbitmq.
the class CodecsTest method codecs.
@ParameterizedTest
@MethodSource("codecsCouples")
void codecs(CodecCouple codecCouple) {
Codec serializer = codecCouple.serializer;
Codec deserializer = codecCouple.deserializer;
Stream<MessageTestConfiguration> messageOperations = Stream.of(test(builder -> builder.properties().messageId(42).messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsLong()).isEqualTo(42)), test(builder -> builder.properties().messageId("foo").messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsString()).isEqualTo("foo")), test(builder -> builder.properties().messageId("bar".getBytes(CHARSET)).messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsBinary()).isEqualTo("bar".getBytes(CHARSET))), test(builder -> builder.properties().messageId(TEST_UUID).messageBuilder(), message -> assertThat(message.getProperties().getMessageIdAsUuid()).isEqualTo(TEST_UUID)), test(builder -> builder.properties().correlationId(42 + 10).messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsLong()).isEqualTo(42 + 10)), test(builder -> builder.properties().correlationId("correlation foo").messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsString()).isEqualTo("correlation foo")), test(builder -> builder.properties().correlationId("correlation bar".getBytes(CHARSET)).messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsBinary()).isEqualTo("correlation bar".getBytes(CHARSET))), test(builder -> builder.properties().correlationId(TEST_UUID).messageBuilder(), message -> assertThat(message.getProperties().getCorrelationIdAsUuid()).isEqualTo(TEST_UUID)), test(builder -> builder, message -> assertThat(message.getProperties().getGroupSequence()).isEqualTo(-1)), test(builder -> builder.properties().groupSequence(10).messageBuilder(), message -> assertThat(message.getProperties().getGroupSequence()).isEqualTo(10)), test(builder -> builder.properties().groupSequence((long) Integer.MAX_VALUE + 10).messageBuilder(), message -> assertThat(message.getProperties().getGroupSequence()).isEqualTo((long) Integer.MAX_VALUE + 10)));
String body = "hello";
String userId = "yoda";
String to = "the to address";
String subject = "the subject";
String replyTo = "the reply to";
String contentType = "text/plain";
String contentEncoding = "gzip";
String groupId = "the group ID";
String replyToGroupId = "the reply to group ID";
long now = new Date().getTime();
UUID uuid = UUID.randomUUID();
byte[] binary = "the binary".getBytes(CHARSET);
String string = "a string";
String symbol = "a symbol";
messageOperations.forEach(messageTestConfiguration -> {
Function<MessageBuilder, MessageBuilder> messageOperation = messageTestConfiguration.messageOperation;
Consumer<Message> messageExpectation = messageTestConfiguration.messageExpectation;
MessageBuilder messageBuilder = codecCouple.messageBuilderSupplier.get();
Message outboundMessage = messageOperation.apply(messageBuilder).addData(body.getBytes(CHARSET)).properties().userId(userId.getBytes(CHARSET)).to(to).subject(subject).replyTo(replyTo).contentType(contentType).contentEncoding(contentEncoding).absoluteExpiryTime(now + 1000).creationTime(now).groupId(groupId).replyToGroupId(replyToGroupId).messageBuilder().applicationProperties().entry("boolean", Boolean.FALSE).entry("byte", (byte) 1).entry("short", (short) 2).entry("int", 3).entry("long", 4l).entryUnsigned("ubyte", (byte) 1).entryUnsigned("ushort", (short) 2).entryUnsigned("uint", 3).entryUnsigned("ulong", 4l).entryUnsigned("large.ubyte", (byte) (Byte.MAX_VALUE + 10)).entryUnsigned("large.ushort", (short) (Short.MAX_VALUE + 10)).entryUnsigned("large.uint", Integer.MAX_VALUE + 10).entryUnsigned("large.ulong", Long.MAX_VALUE + 10).entry("float", 3.14f).entry("double", 6.28).entry("char", 'c').entryTimestamp("timestamp", now).entry("uuid", uuid).entry("binary", binary).entry("string", string).entrySymbol("symbol", symbol).messageBuilder().messageAnnotations().entry("annotations.boolean", Boolean.FALSE).entry("annotations.byte", (byte) 1).entry("annotations.short", (short) 2).entry("annotations.int", 3).entry("annotations.long", 4l).entryUnsigned("annotations.ubyte", (byte) 1).entryUnsigned("annotations.ushort", (short) 2).entryUnsigned("annotations.uint", 3).entryUnsigned("annotations.ulong", 4l).entryUnsigned("annotations.large.ubyte", (byte) (Byte.MAX_VALUE + 10)).entryUnsigned("annotations.large.ushort", (short) (Short.MAX_VALUE + 10)).entryUnsigned("annotations.large.uint", Integer.MAX_VALUE + 10).entryUnsigned("annotations.large.ulong", Long.MAX_VALUE + 10).entry("annotations.float", 3.14f).entry("annotations.double", 6.28).entry("annotations.char", 'c').entryTimestamp("annotations.timestamp", now).entry("annotations.uuid", uuid).entry("annotations.binary", binary).entry("annotations.string", string).entrySymbol("annotations.symbol", symbol).messageBuilder().build();
Codec.EncodedMessage encoded = serializer.encode(outboundMessage);
byte[] encodedData = new byte[encoded.getSize()];
System.arraycopy(encoded.getData(), 0, encodedData, 0, encoded.getSize());
Message inboundMessage = deserializer.decode(encodedData);
messageExpectation.accept(inboundMessage);
assertThat(new String(inboundMessage.getBodyAsBinary())).isEqualTo(body);
assertThat(inboundMessage.getProperties().getUserId()).isEqualTo(userId.getBytes(CHARSET));
assertThat(inboundMessage.getProperties().getTo()).isEqualTo(to);
assertThat(inboundMessage.getProperties().getSubject()).isEqualTo(subject);
assertThat(inboundMessage.getProperties().getReplyTo()).isEqualTo(replyTo);
assertThat(inboundMessage.getProperties().getContentType()).isEqualTo(contentType);
assertThat(inboundMessage.getProperties().getContentEncoding()).isEqualTo(contentEncoding);
assertThat(inboundMessage.getProperties().getAbsoluteExpiryTime()).isEqualTo(now + 1000);
assertThat(inboundMessage.getProperties().getCreationTime()).isEqualTo(now);
assertThat(inboundMessage.getProperties().getGroupId()).isEqualTo(groupId);
assertThat(inboundMessage.getProperties().getReplyToGroupId()).isEqualTo(replyToGroupId);
// application properties
assertThat(inboundMessage.getApplicationProperties().get("boolean")).isNotNull().isInstanceOf(Boolean.class).isEqualTo(Boolean.FALSE);
assertThat(inboundMessage.getApplicationProperties().get("byte")).isNotNull().isInstanceOf(Byte.class).isEqualTo(Byte.valueOf((byte) 1));
assertThat(inboundMessage.getApplicationProperties().get("short")).isNotNull().isInstanceOf(Short.class).isEqualTo(Short.valueOf((short) 2));
assertThat(inboundMessage.getApplicationProperties().get("int")).isNotNull().isInstanceOf(Integer.class).isEqualTo(Integer.valueOf(3));
assertThat(inboundMessage.getApplicationProperties().get("long")).isNotNull().isInstanceOf(Long.class).isEqualTo(Long.valueOf(4));
assertThat(inboundMessage.getApplicationProperties().get("ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).isEqualTo(UnsignedByte.valueOf((byte) 1));
assertThat(inboundMessage.getApplicationProperties().get("ushort")).isNotNull().isInstanceOf(UnsignedShort.class).isEqualTo(UnsignedShort.valueOf((short) 2));
assertThat(inboundMessage.getApplicationProperties().get("uint")).isNotNull().isInstanceOf(UnsignedInteger.class).isEqualTo(UnsignedInteger.valueOf(3));
assertThat(inboundMessage.getApplicationProperties().get("ulong")).isNotNull().isInstanceOf(UnsignedLong.class).isEqualTo(UnsignedLong.valueOf(4));
assertThat(inboundMessage.getApplicationProperties().get("large.ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedByte.class)).extracting(v -> v.intValue()).isEqualTo(Byte.MAX_VALUE + 10);
assertThat(inboundMessage.getApplicationProperties().get("large.ushort")).isNotNull().isInstanceOf(UnsignedShort.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedShort.class)).extracting(v -> v.intValue()).isEqualTo(Short.MAX_VALUE + 10);
assertThat(inboundMessage.getApplicationProperties().get("large.uint")).isNotNull().isInstanceOf(UnsignedInteger.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedInteger.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf((long) Integer.MAX_VALUE + 10L).toString());
assertThat(inboundMessage.getApplicationProperties().get("large.ulong")).isNotNull().isInstanceOf(UnsignedLong.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedLong.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN).toString());
assertThat(inboundMessage.getApplicationProperties().get("float")).isNotNull().isInstanceOf(Float.class).isEqualTo(Float.valueOf(3.14f));
assertThat(inboundMessage.getApplicationProperties().get("double")).isNotNull().isInstanceOf(Double.class).isEqualTo(Double.valueOf(6.28));
assertThat(inboundMessage.getApplicationProperties().get("char")).isNotNull().isInstanceOf(Character.class).isEqualTo('c');
assertThat(inboundMessage.getApplicationProperties().get("timestamp")).isNotNull().isInstanceOf(Long.class).isEqualTo(now);
assertThat(inboundMessage.getApplicationProperties().get("uuid")).isNotNull().isInstanceOf(UUID.class).isEqualTo(uuid);
assertThat(inboundMessage.getApplicationProperties().get("binary")).isNotNull().isInstanceOf(byte[].class).isEqualTo(binary);
assertThat(inboundMessage.getApplicationProperties().get("string")).isNotNull().isInstanceOf(String.class).isEqualTo(string);
assertThat(inboundMessage.getApplicationProperties().get("symbol")).isNotNull().isInstanceOf(String.class).isEqualTo(symbol);
// message annotations
assertThat(inboundMessage.getMessageAnnotations().get("annotations.boolean")).isNotNull().isInstanceOf(Boolean.class).isEqualTo(Boolean.FALSE);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.byte")).isNotNull().isInstanceOf(Byte.class).isEqualTo(Byte.valueOf((byte) 1));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.short")).isNotNull().isInstanceOf(Short.class).isEqualTo(Short.valueOf((short) 2));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.int")).isNotNull().isInstanceOf(Integer.class).isEqualTo(Integer.valueOf(3));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.long")).isNotNull().isInstanceOf(Long.class).isEqualTo(Long.valueOf(4));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).isEqualTo(UnsignedByte.valueOf((byte) 1));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.ushort")).isNotNull().isInstanceOf(UnsignedShort.class).isEqualTo(UnsignedShort.valueOf((short) 2));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.uint")).isNotNull().isInstanceOf(UnsignedInteger.class).isEqualTo(UnsignedInteger.valueOf(3));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.ulong")).isNotNull().isInstanceOf(UnsignedLong.class).isEqualTo(UnsignedLong.valueOf(4));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.ubyte")).isNotNull().isInstanceOf(UnsignedByte.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedByte.class)).extracting(v -> v.intValue()).isEqualTo(Byte.MAX_VALUE + 10);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.ushort")).isNotNull().isInstanceOf(UnsignedShort.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedShort.class)).extracting(v -> v.intValue()).isEqualTo(Short.MAX_VALUE + 10);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.uint")).isNotNull().isInstanceOf(UnsignedInteger.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedInteger.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf((long) Integer.MAX_VALUE + 10L).toString());
assertThat(inboundMessage.getMessageAnnotations().get("annotations.large.ulong")).isNotNull().isInstanceOf(UnsignedLong.class).asInstanceOf(InstanceOfAssertFactories.type(UnsignedLong.class)).extracting(v -> v.toString()).isEqualTo(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN).toString());
assertThat(inboundMessage.getMessageAnnotations().get("annotations.float")).isNotNull().isInstanceOf(Float.class).isEqualTo(Float.valueOf(3.14f));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.double")).isNotNull().isInstanceOf(Double.class).isEqualTo(Double.valueOf(6.28));
assertThat(inboundMessage.getMessageAnnotations().get("annotations.char")).isNotNull().isInstanceOf(Character.class).isEqualTo('c');
assertThat(inboundMessage.getMessageAnnotations().get("annotations.timestamp")).isNotNull().isInstanceOf(Long.class).isEqualTo(now);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.uuid")).isNotNull().isInstanceOf(UUID.class).isEqualTo(uuid);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.binary")).isNotNull().isInstanceOf(byte[].class).isEqualTo(binary);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.string")).isNotNull().isInstanceOf(String.class).isEqualTo(string);
assertThat(inboundMessage.getMessageAnnotations().get("annotations.symbol")).isNotNull().isInstanceOf(String.class).isEqualTo(symbol);
});
}
use of com.rabbitmq.stream.MessageBuilder in project rabbitmq-stream-java-client by rabbitmq.
the class AmqpInteroperabilityTest method publishToStreamConsumeFromStreamQueue.
@ParameterizedTest
@MethodSource("codecs")
void publishToStreamConsumeFromStreamQueue(Codec codec, TestInfo info) {
int messageCount = 1_000;
ConnectionFactory connectionFactory = new ConnectionFactory();
Date timestamp = new Date();
UUID messageIdUuid = UUID.randomUUID();
UUID correlationIdUuid = UUID.randomUUID();
Supplier<Stream<MessageOperation>> testMessageOperations = () -> Stream.of(mo(mb -> {
mb.properties().messageId("the message ID");
mb.properties().correlationId("the correlation ID");
}, d -> {
assertThat(d.getProperties().getMessageId()).isEqualTo("the message ID");
assertThat(d.getProperties().getCorrelationId()).isEqualTo("the correlation ID");
}), mo(mb -> {
mb.properties().messageId(// larger than 091 shortstr
StringUtils.repeat("*", 300));
mb.properties().correlationId(// larger than 091 shortstr
StringUtils.repeat("*", 300));
}, d -> {
assertThat(d.getProperties().getMessageId()).isNull();
assertThat(d.getProperties().getCorrelationId()).isNull();
assertThat(d.getProperties().getHeaders()).containsEntry("x-message-id", LongStringHelper.asLongString(StringUtils.repeat("*", 300))).containsEntry("x-correlation-id", LongStringHelper.asLongString(StringUtils.repeat("*", 300)));
}), mo(mb -> {
mb.properties().messageId(messageIdUuid);
mb.properties().correlationId(correlationIdUuid);
}, d -> {
assertThat(d.getProperties().getMessageId()).isEqualTo(messageIdUuid.toString());
assertThat(d.getProperties().getCorrelationId()).isEqualTo(correlationIdUuid.toString());
assertThat(d.getProperties().getHeaders()).containsEntry("x-message-id-type", LongStringHelper.asLongString("uuid")).containsEntry("x-correlation-id-type", LongStringHelper.asLongString("uuid"));
}), mo(mb -> {
mb.properties().messageId(10);
mb.properties().correlationId(20);
}, d -> {
assertThat(d.getProperties().getMessageId()).isEqualTo("10");
assertThat(d.getProperties().getCorrelationId()).isEqualTo("20");
assertThat(d.getProperties().getHeaders()).containsEntry("x-message-id-type", LongStringHelper.asLongString("ulong")).containsEntry("x-correlation-id-type", LongStringHelper.asLongString("ulong"));
}), mo(mb -> {
mb.properties().messageId("the message ID".getBytes(UTF8));
mb.properties().correlationId("the correlation ID".getBytes(UTF8));
}, d -> {
assertThat(Base64.getDecoder().decode(d.getProperties().getMessageId())).isEqualTo("the message ID".getBytes(UTF8));
assertThat(Base64.getDecoder().decode(d.getProperties().getCorrelationId())).isEqualTo("the correlation ID".getBytes(UTF8));
assertThat(d.getProperties().getHeaders()).containsEntry("x-message-id-type", LongStringHelper.asLongString("binary")).containsEntry("x-correlation-id-type", LongStringHelper.asLongString("binary"));
}), mo(mb -> {
mb.properties().messageId(StringUtils.repeat("a", 300).getBytes(// larger than 091 shortstr
UTF8));
mb.properties().correlationId(StringUtils.repeat("b", 300).getBytes(// larger than 091 shortstr
UTF8));
}, d -> {
assertThat(d.getProperties().getMessageId()).isNull();
assertThat(d.getProperties().getCorrelationId()).isNull();
assertThat(d.getProperties().getHeaders()).containsEntry("x-message-id", LongStringHelper.asLongString(StringUtils.repeat("a", 300).getBytes(UTF8))).containsEntry("x-correlation-id", LongStringHelper.asLongString(StringUtils.repeat("b", 300).getBytes(UTF8)));
}));
testMessageOperations.get().forEach(testMessageOperation -> {
CountDownLatch confirmLatch = new CountDownLatch(messageCount);
Client client = cf.get(new Client.ClientParameters().codec(codec).publishConfirmListener((publisherId, publishingId) -> confirmLatch.countDown()));
String s = streamName(info);
Client.Response response = client.create(s);
assertThat(response.isOk()).isTrue();
Supplier<Stream<MessageOperation>> messageOperations = () -> Stream.of(mo(mb -> mb.properties().userId("the user ID".getBytes(UTF8)), d -> assertThat(d.getProperties().getUserId()).isEqualTo("the user ID")), mo(mb -> mb.properties().to("the to address"), d -> {
}), mo(mb -> mb.properties().subject("the subject"), d -> {
}), mo(mb -> mb.properties().replyTo("the reply to address"), d -> assertThat(d.getProperties().getReplyTo()).isEqualTo("the reply to address")), mo(mb -> mb.properties().contentType("the content type"), d -> assertThat(d.getProperties().getContentType()).isEqualTo("the content type")), mo(mb -> mb.properties().contentEncoding("the content encoding"), d -> assertThat(d.getProperties().getContentEncoding()).isEqualTo("the content encoding")), mo(mb -> mb.properties().absoluteExpiryTime(timestamp.getTime() + 1000), d -> {
}), mo(mb -> mb.properties().creationTime(timestamp.getTime()), d -> assertThat(d.getProperties().getTimestamp().getTime()).isEqualTo((timestamp.getTime() / 1000) * // in seconds in 091, in ms in 1.0, so losing
1000)), mo(mb -> mb.properties().groupId("the group ID"), d -> {
}), mo(mb -> mb.properties().groupSequence(10), d -> {
}), mo(mb -> mb.properties().replyToGroupId("the reply to group ID"), d -> {
}), mo(mb -> mb.applicationProperties().entry("byte", Byte.MAX_VALUE), d -> assertThat(d.getProperties().getHeaders()).containsEntry("byte", Byte.MAX_VALUE)), mo(mb -> mb.applicationProperties().entry("short", Short.MAX_VALUE), d -> assertThat(d.getProperties().getHeaders()).containsEntry("short", Short.MAX_VALUE)), mo(mb -> mb.applicationProperties().entry("integer", Integer.MAX_VALUE), d -> assertThat(d.getProperties().getHeaders()).containsEntry("integer", Integer.MAX_VALUE)), mo(mb -> mb.applicationProperties().entry("long", Long.MAX_VALUE), d -> assertThat(d.getProperties().getHeaders()).containsEntry("long", Long.MAX_VALUE)), mo(mb -> mb.applicationProperties().entry("string", "a string"), d -> assertThat(d.getProperties().getHeaders()).containsEntry("string", LongStringHelper.asLongString("a string"))), mo(mb -> mb.applicationProperties().entryTimestamp("timestamp", timestamp.getTime()), d -> assertThat(d.getProperties().getHeaders()).containsEntry("timestamp", new Date((timestamp.getTime() / 1000) * 1000))), mo(mb -> mb.applicationProperties().entry("boolean", Boolean.TRUE), d -> assertThat(d.getProperties().getHeaders()).containsEntry("boolean", Boolean.TRUE)), mo(mb -> mb.applicationProperties().entry("float", 3.14f), d -> assertThat(d.getProperties().getHeaders()).containsEntry("float", 3.14f)), mo(mb -> mb.applicationProperties().entry("binary", "hello".getBytes(UTF8)), d -> assertThat(d.getProperties().getHeaders()).containsEntry("binary", "hello".getBytes(UTF8))));
client.declarePublisher(b(1), null, s);
IntStream.range(0, messageCount).forEach(i -> {
MessageBuilder messageBuilder = client.messageBuilder();
messageBuilder.addData(("stream " + i).getBytes(UTF8));
testMessageOperation.messageBuilderConsumer.accept(messageBuilder);
messageOperations.get().forEach(messageOperation -> messageOperation.messageBuilderConsumer.accept(messageBuilder));
client.publish(b(1), Collections.singletonList(messageBuilder.build()));
});
try (Connection c = connectionFactory.newConnection()) {
assertThat(confirmLatch.await(10, SECONDS)).isTrue();
Channel ch = c.createChannel();
ch.basicQos(200);
CountDownLatch consumedLatch = new CountDownLatch(messageCount);
Set<String> messageBodies = ConcurrentHashMap.newKeySet(messageCount);
Set<Delivery> messages = ConcurrentHashMap.newKeySet(messageCount);
ch.basicConsume(s, false, Collections.singletonMap("x-stream-offset", 0), (consumerTag, message) -> {
messages.add(message);
messageBodies.add(new String(message.getBody(), UTF8));
consumedLatch.countDown();
ch.basicAck(message.getEnvelope().getDeliveryTag(), false);
}, consumerTag -> {
});
assertThat(consumedLatch.await(10, SECONDS)).isTrue();
assertThat(messageBodies).hasSize(messageCount);
IntStream.range(0, messageCount).forEach(i -> assertThat(messageBodies.contains("stream " + i)).isTrue());
Delivery message = messages.iterator().next();
assertThat(message.getEnvelope().getExchange()).isEmpty();
assertThat(message.getEnvelope().getRoutingKey()).isEqualTo(s);
assertThat(message.getProperties().getHeaders()).containsKey("x-stream-offset");
testMessageOperation.deliveryConsumer.accept(message);
messageOperations.get().forEach(messageOperation -> messageOperation.deliveryConsumer.accept(message));
} catch (Exception e) {
throw new RuntimeException(e);
}
response = client.delete(s);
assertThat(response.isOk()).isTrue();
});
}
use of com.rabbitmq.stream.MessageBuilder in project rabbitmq-stream-java-client by rabbitmq.
the class TestUtils method publishAndWaitForConfirms.
static void publishAndWaitForConfirms(TestUtils.ClientFactory cf, Function<MessageBuilder, Message> messageFactory, int publishCount, String stream) {
CountDownLatch latchConfirm = new CountDownLatch(publishCount);
Client.PublishConfirmListener publishConfirmListener = (publisherId, correlationId) -> latchConfirm.countDown();
Client client = cf.get(new Client.ClientParameters().publishConfirmListener(publishConfirmListener));
client.declarePublisher(b(1), null, stream);
for (int i = 1; i <= publishCount; i++) {
Message message = messageFactory.apply(client.messageBuilder());
client.publish(b(1), Collections.singletonList(message));
}
try {
assertThat(latchConfirm.await(60, SECONDS)).isTrue();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
use of com.rabbitmq.stream.MessageBuilder in project spring-amqp by spring-projects.
the class DefaultStreamMessageConverter method fromMessage.
@Override
public com.rabbitmq.stream.Message fromMessage(Message message) throws MessageConversionException {
MessageBuilder builder = this.builderSupplier.get();
PropertiesBuilder propsBuilder = builder.properties();
MessageProperties props = message.getMessageProperties();
Assert.isInstanceOf(StreamMessageProperties.class, props);
StreamMessageProperties mProps = (StreamMessageProperties) props;
JavaUtils.INSTANCE.acceptIfNotNull(mProps.getMessageId(), // TODO different types
propsBuilder::messageId).acceptIfNotNull(mProps.getUserId(), usr -> propsBuilder.userId(usr.getBytes(this.charset))).acceptIfNotNull(mProps.getTo(), propsBuilder::to).acceptIfNotNull(mProps.getSubject(), propsBuilder::subject).acceptIfNotNull(mProps.getReplyTo(), propsBuilder::replyTo).acceptIfNotNull(mProps.getCorrelationId(), // TODO different types
propsBuilder::correlationId).acceptIfNotNull(mProps.getContentType(), propsBuilder::contentType).acceptIfNotNull(mProps.getContentEncoding(), propsBuilder::contentEncoding).acceptIfNotNull(mProps.getExpiration(), exp -> propsBuilder.absoluteExpiryTime(Long.parseLong(exp))).acceptIfNotNull(mProps.getCreationTime(), propsBuilder::creationTime).acceptIfNotNull(mProps.getGroupId(), propsBuilder::groupId).acceptIfNotNull(mProps.getGroupSequence(), propsBuilder::groupSequence).acceptIfNotNull(mProps.getReplyToGroupId(), propsBuilder::replyToGroupId);
if (mProps.getHeaders().size() > 0) {
ApplicationPropertiesBuilder appPropsBuilder = builder.applicationProperties();
mProps.getHeaders().forEach((key, val) -> {
mapProp(key, val, appPropsBuilder);
});
}
builder.addData(message.getBody());
return builder.build();
}
Aggregations