Search in sources :

Example 1 with EncodedMessage

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);
}
Also used : Codec(com.rabbitmq.stream.Codec) CompressionCodec(com.rabbitmq.stream.compression.CompressionCodec) Message(com.rabbitmq.stream.Message) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage)

Example 2 with EncodedMessage

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);
}
Also used : Codec(com.rabbitmq.stream.Codec) CompressionCodec(com.rabbitmq.stream.compression.CompressionCodec) Message(com.rabbitmq.stream.Message) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage)

Example 3 with EncodedMessage

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();
}
Also used : CompressedEncodedMessageBatch(com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EncodedMessageBatch(com.rabbitmq.stream.impl.Client.EncodedMessageBatch) CompressedEncodedMessageBatch(com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with EncodedMessage

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);
}
Also used : Arrays(java.util.Arrays) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) UnsignedInteger(com.rabbitmq.stream.amqp.UnsignedInteger) Message(com.rabbitmq.stream.Message) Date(java.util.Date) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Codec(com.rabbitmq.stream.Codec) UnsignedByte(com.rabbitmq.stream.amqp.UnsignedByte) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) UnsignedLong(com.rabbitmq.stream.amqp.UnsignedLong) Charset(java.nio.charset.Charset) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) BigInteger(java.math.BigInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ThrowableAssert(org.assertj.core.api.ThrowableAssert) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) MessageBuilder(com.rabbitmq.stream.MessageBuilder) QpidProtonAmqpMessageWrapper(com.rabbitmq.stream.codec.QpidProtonCodec.QpidProtonAmqpMessageWrapper) UnsignedShort(com.rabbitmq.stream.amqp.UnsignedShort) Mockito.mock(org.mockito.Mockito.mock) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) Message(com.rabbitmq.stream.Message) QpidProtonAmqpMessageWrapper(com.rabbitmq.stream.codec.QpidProtonCodec.QpidProtonAmqpMessageWrapper) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with EncodedMessage

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);
}
Also used : Codec(com.rabbitmq.stream.Codec) CompressionCodec(com.rabbitmq.stream.compression.CompressionCodec) Message(com.rabbitmq.stream.Message) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage)

Aggregations

EncodedMessage (com.rabbitmq.stream.Codec.EncodedMessage)6 ArrayList (java.util.ArrayList)6 Codec (com.rabbitmq.stream.Codec)5 Message (com.rabbitmq.stream.Message)5 CompressionCodec (com.rabbitmq.stream.compression.CompressionCodec)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 MessageBuilder (com.rabbitmq.stream.MessageBuilder)1 UnsignedByte (com.rabbitmq.stream.amqp.UnsignedByte)1 UnsignedInteger (com.rabbitmq.stream.amqp.UnsignedInteger)1 UnsignedLong (com.rabbitmq.stream.amqp.UnsignedLong)1 UnsignedShort (com.rabbitmq.stream.amqp.UnsignedShort)1 QpidProtonAmqpMessageWrapper (com.rabbitmq.stream.codec.QpidProtonCodec.QpidProtonAmqpMessageWrapper)1 CompressedEncodedMessageBatch (com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch)1 EncodedMessageBatch (com.rabbitmq.stream.impl.Client.EncodedMessageBatch)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 InputStream (java.io.InputStream)1