use of com.rabbitmq.stream.Codec.EncodedMessage in project rabbitmq-stream-java-client by rabbitmq.
the class Client method publish.
public List<Long> publish(byte publisherId, List<Message> messages, OutboundEntityMappingCallback mappingCallback, ToLongFunction<Object> publishSequenceFunction) {
List<Object> encodedMessages = new ArrayList<>(messages.size());
for (Message message : messages) {
Codec.EncodedMessage encodedMessage = codec.encode(message);
checkMessageFitsInFrame(encodedMessage);
OriginalAndEncodedOutboundEntity wrapper = new OriginalAndEncodedOutboundEntity(message, encodedMessage);
encodedMessages.add(wrapper);
}
return publishInternal(this.channel, publisherId, encodedMessages, new OriginalEncodedEntityOutboundEntityWriteCallback(mappingCallback, OUTBOUND_MESSAGE_WRITE_CALLBACK), publishSequenceFunction);
}
use of com.rabbitmq.stream.Codec.EncodedMessage in project rabbitmq-stream-java-client by rabbitmq.
the class Client method publishBatches.
public List<Long> publishBatches(byte publisherId, List<MessageBatch> messageBatches, ToLongFunction<Object> publishSequenceFunction) {
List<Object> encodedMessageBatches = new ArrayList<>(messageBatches.size());
for (MessageBatch batch : messageBatches) {
EncodedMessageBatch encodedMessageBatch = createEncodedMessageBatch(batch.compression, batch.messages.size());
for (Message message : batch.messages) {
Codec.EncodedMessage encodedMessage = codec.encode(message);
checkMessageFitsInFrame(encodedMessage);
encodedMessageBatch.add(encodedMessage);
}
encodedMessageBatch.close();
checkMessageBatchFitsInFrame(encodedMessageBatch);
encodedMessageBatches.add(encodedMessageBatch);
}
return publishInternal(this.channel, publisherId, encodedMessageBatches, OUTBOUND_MESSAGE_BATCH_WRITE_CALLBACK, publishSequenceFunction);
}
use of com.rabbitmq.stream.Codec.EncodedMessage in project rabbitmq-stream-java-client by rabbitmq.
the class CompressionCodecsTest method compressedEncodedMessageBatch.
@ParameterizedTest
@MethodSource
void compressedEncodedMessageBatch(CompressionCodec compressionCodec, CompressionCodec decompressionCodec) throws IOException {
assertThat(compressionCodec.code()).isEqualTo(decompressionCodec.code());
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
int messageCount = 100;
EncodedMessageBatch encodedMessageBatch = new CompressedEncodedMessageBatch(allocator, compressionCodec, messageCount);
List<EncodedMessage> encodedMessages = new ArrayList<>(messageCount);
IntStream.range(0, messageCount).forEach(i -> {
byte[] body = ("message " + i).getBytes(StandardCharsets.UTF_8);
EncodedMessage encodedMessage = new EncodedMessage(body.length, body);
encodedMessageBatch.add(encodedMessage);
encodedMessages.add(encodedMessage);
});
encodedMessageBatch.close();
int plainSize = encodedMessages.stream().mapToInt(EncodedMessage::getSize).sum() + encodedMessages.size() * 4;
int compressedSize = encodedMessageBatch.sizeInBytes();
ByteBuf destinationBb = allocator.buffer(compressedSize);
encodedMessageBatch.write(destinationBb);
assertThat(compressedSize).isLessThan(plainSize);
assertThat(destinationBb.writerIndex()).isEqualTo(compressedSize);
ByteBuf outBb = allocator.buffer(plainSize);
destinationBb.readerIndex(0);
InputStream inputStream = decompressionCodec.decompress(new ByteBufInputStream(destinationBb));
int n;
while (-1 != (n = inputStream.read())) {
outBb.writeByte(n);
}
List<EncodedMessage> decompressedMessages = new ArrayList<>(messageCount);
while (outBb.isReadable()) {
int size = outBb.readInt();
byte[] msg = new byte[size];
outBb.readBytes(msg);
decompressedMessages.add(new EncodedMessage(size, msg));
}
assertThat(decompressedMessages).hasSameSizeAs(encodedMessages);
IntStream.range(0, messageCount).forEach(i -> {
EncodedMessage originalMessage = encodedMessages.get(i);
EncodedMessage decompressedMessage = decompressedMessages.get(i);
assertThat(decompressedMessage.getSize()).isEqualTo(originalMessage.getSize());
assertThat(decompressedMessage.getData()).isEqualTo(originalMessage.getData());
});
destinationBb.release();
outBb.release();
}
use of com.rabbitmq.stream.Codec.EncodedMessage in project rabbitmq-stream-java-client by rabbitmq.
the class CodecsTest method supportAmqpValueBody.
@ParameterizedTest
@MethodSource("codecs")
void supportAmqpValueBody(Codec codec) {
Function<Object, Message> encodeDecode = content -> {
org.apache.qpid.proton.message.Message nativeMessage = org.apache.qpid.proton.message.Message.Factory.create();
nativeMessage.setBody(new AmqpValue(content));
QpidProtonAmqpMessageWrapper wrapper = new QpidProtonAmqpMessageWrapper(true, 1L, nativeMessage);
EncodedMessage encoded = new QpidProtonCodec().encode(wrapper);
byte[] encodedData = new byte[encoded.getSize()];
System.arraycopy(encoded.getData(), 0, encodedData, 0, encoded.getSize());
Message decodedMessage = codec.decode(encodedData);
return decodedMessage;
};
Message m1 = encodeDecode.apply("hello".getBytes(StandardCharsets.UTF_8));
assertThat(m1.getBodyAsBinary()).asString(StandardCharsets.UTF_8).isEqualTo("hello");
Message m2 = encodeDecode.apply("a string is not an array of byte");
assertThatThrownBy(() -> m2.getBodyAsBinary()).isInstanceOf(IllegalStateException.class);
}
use of com.rabbitmq.stream.Codec.EncodedMessage in project rabbitmq-stream-java-client by rabbitmq.
the class Client method publish.
public List<Long> publish(byte publisherId, List<Message> messages, ToLongFunction<Object> publishSequenceFunction) {
List<Object> encodedMessages = new ArrayList<>(messages.size());
for (Message message : messages) {
Codec.EncodedMessage encodedMessage = codec.encode(message);
checkMessageFitsInFrame(encodedMessage);
encodedMessages.add(encodedMessage);
}
return publishInternal(this.channel, publisherId, encodedMessages, OUTBOUND_MESSAGE_WRITE_CALLBACK, publishSequenceFunction);
}
Aggregations