Search in sources :

Example 21 with Message

use of com.rabbitmq.stream.Message 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)

Example 22 with Message

use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.

the class Client method publishBatches.

public List<Long> publishBatches(byte publisherId, List<MessageBatch> messageBatches, OutboundEntityMappingCallback mappingCallback, 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);
        OriginalAndEncodedOutboundEntity wrapper = new OriginalAndEncodedOutboundEntity(batch, encodedMessageBatch);
        encodedMessageBatches.add(wrapper);
    }
    return publishInternal(this.channel, publisherId, encodedMessageBatches, new OriginalEncodedEntityOutboundEntityWriteCallback(mappingCallback, 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 23 with Message

use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.

the class StompInteroperabilityTest method publishToStompDestinationConsumeFromStream.

@Test
void publishToStompDestinationConsumeFromStream() throws Exception {
    String messageBody = UUID.randomUUID().toString();
    stompConnect();
    String receipt = UUID.randomUUID().toString();
    byte[] frame = frameBuilder().command("SEND").header("destination", "/amq/queue/" + stream).header("content-type", "text/plain").header("content-length", String.valueOf(messageBody.length())).header("some-header", "some header value").header("receipt", receipt).body(messageBody).build();
    out.write(frame);
    AtomicBoolean gotReceipt = new AtomicBoolean(false);
    read(line -> {
        gotReceipt.compareAndSet(false, line.contains(receipt));
        return line.equals(NULL) && gotReceipt.get();
    });
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Message> messageReference = new AtomicReference<>();
    env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message1) -> {
        messageReference.set(message1);
        latch.countDown();
    }).build();
    assertThat(latchAssert(latch)).completes();
    Message message = messageReference.get();
    assertThat(message.getBodyAsBinary()).isEqualTo(messageBody.getBytes(StandardCharsets.UTF_8));
    assertThat(message.getProperties().getContentType()).isEqualTo("text/plain");
    assertThat(message.getApplicationProperties().get("content-length")).isEqualTo(String.valueOf(messageBody.length()));
    assertThat(message.getApplicationProperties().get("receipt")).isNotNull().isInstanceOf(String.class);
    assertThat(message.getApplicationProperties().get("some-header")).isEqualTo("some header value");
    assertThat(message.getMessageAnnotations().get("x-routing-key")).isEqualTo(stream);
    assertThat(message.getMessageAnnotations().get("x-exchange")).isEqualTo("");
}
Also used : DisabledIfStompNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfStompNotEnabled) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) AfterAll(org.junit.jupiter.api.AfterAll) Future(java.util.concurrent.Future) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) OutputStream(java.io.OutputStream) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) UUID(java.util.UUID) Producer(com.rabbitmq.stream.Producer) InputStreamReader(java.io.InputStreamReader) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) BufferedReader(java.io.BufferedReader) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(com.rabbitmq.stream.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 24 with Message

use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentTest method createPublishConsumeDelete.

@ParameterizedTest
@ValueSource(booleans = { false, true })
void createPublishConsumeDelete(boolean lazyInit, TestInfo info) {
    try (Environment env = environmentBuilder.lazyInitialization(lazyInit).build()) {
        String s = streamName(info);
        env.streamCreator().stream(s).create();
        int messageCount = 50_000;
        CountDownLatch confirmLatch = new CountDownLatch(messageCount);
        CountDownLatch consumeLatch = new CountDownLatch(messageCount);
        Producer producer = env.producerBuilder().stream(s).build();
        ConfirmationHandler confirmationHandler = confirmationStatus -> confirmLatch.countDown();
        IntStream.range(0, messageCount).forEach(i -> {
            Message message = producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build();
            producer.send(message, confirmationHandler);
        });
        latchAssert(confirmLatch).completes();
        Consumer consumer = env.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((context, message) -> consumeLatch.countDown()).build();
        latchAssert(consumeLatch).completes();
        producer.close();
        consumer.close();
        env.deleteStream(s);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Message(com.rabbitmq.stream.Message) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with Message

use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentTest method growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed.

@Test
void growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed(TestInfo info) throws Exception {
    int messageCount = 100;
    int streamCount = 20;
    int producersCount = ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT * 3 + 10;
    int consumersCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT * 2 + 10;
    try (Environment environment = environmentBuilder.build()) {
        List<String> streams = IntStream.range(0, streamCount).mapToObj(i -> streamName(info)).map(s -> {
            environment.streamCreator().stream(s).create();
            return s;
        }).collect(Collectors.toCollection(() -> new CopyOnWriteArrayList<>()));
        CountDownLatch confirmLatch = new CountDownLatch(messageCount * producersCount);
        CountDownLatch consumeLatch = new CountDownLatch(messageCount * producersCount);
        List<Producer> producers = IntStream.range(0, producersCount).mapToObj(i -> {
            String s = streams.get(i % streams.size());
            return environment.producerBuilder().stream(s).build();
        }).collect(Collectors.toList());
        List<Consumer> consumers = IntStream.range(0, consumersCount).mapToObj(i -> {
            String s = streams.get(new Random().nextInt(streams.size()));
            return environment.consumerBuilder().stream(s).messageHandler((offset, message) -> consumeLatch.countDown()).build();
        }).collect(Collectors.toList());
        producers.stream().parallel().forEach(producer -> {
            IntStream.range(0, messageCount).forEach(messageIndex -> {
                producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
                    if (confirmationStatus.isConfirmed()) {
                        confirmLatch.countDown();
                    }
                });
            });
        });
        assertThat(confirmLatch.await(10, SECONDS)).isTrue();
        assertThat(consumeLatch.await(10, SECONDS)).isTrue();
        EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
        assertThat(environmentInfo.getProducers()).hasSize(1);
        int producerManagerCount = environmentInfo.getProducers().get(0).getClients().size();
        assertThat(producerManagerCount).isPositive();
        assertThat(environmentInfo.getConsumers()).hasSize(1);
        int consumerManagerCount = environmentInfo.getConsumers().get(0).getClients().size();
        assertThat(consumerManagerCount).isPositive();
        java.util.function.Consumer<AutoCloseable> closing = agent -> {
            try {
                agent.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
        Collections.reverse(producers);
        List<Producer> subProducers = producers.subList(0, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT);
        subProducers.forEach(closing);
        Collections.reverse(consumers);
        List<Consumer> subConsumers = consumers.subList(0, ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT);
        subConsumers.forEach(closing);
        producers.removeAll(subProducers);
        consumers.removeAll(subConsumers);
        environmentInfo = MonitoringTestUtils.extract(environment);
        assertThat(environmentInfo.getProducers()).hasSize(1);
        assertThat(environmentInfo.getProducers().get(0).getClients()).hasSizeLessThan(producerManagerCount);
        assertThat(environmentInfo.getConsumers()).hasSize(1);
        assertThat(environmentInfo.getConsumers().get(0).getClients()).hasSizeLessThan(consumerManagerCount);
        producers.forEach(closing);
        consumers.forEach(closing);
        streams.stream().forEach(stream -> environment.deleteStream(stream));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) CountDownLatch(java.util.concurrent.CountDownLatch) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) StreamException(com.rabbitmq.stream.StreamException) ConnectException(java.net.ConnectException) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) Random(java.util.Random) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) Environment(com.rabbitmq.stream.Environment) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Message (com.rabbitmq.stream.Message)29 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Environment (com.rabbitmq.stream.Environment)13 List (java.util.List)13 Producer (com.rabbitmq.stream.Producer)11 StandardCharsets (java.nio.charset.StandardCharsets)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Test (org.junit.jupiter.api.Test)10 EncodedMessage (com.rabbitmq.stream.Codec.EncodedMessage)9 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 SECONDS (java.util.concurrent.TimeUnit.SECONDS)9 IntStream (java.util.stream.IntStream)9 Codec (com.rabbitmq.stream.Codec)8 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)8 Collections (java.util.Collections)8 Consumer (com.rabbitmq.stream.Consumer)7