Search in sources :

Example 11 with Producer

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

the class StreamConsumerTest method useSubscriptionListenerToRestartExactlyWhereDesired.

@Test
@DisabledIfRabbitMqCtlNotSet
void useSubscriptionListenerToRestartExactlyWhereDesired() throws Exception {
    AtomicInteger subscriptionListenerCallCount = new AtomicInteger(0);
    AtomicInteger receivedMessages = new AtomicInteger(0);
    AtomicLong offsetTracking = new AtomicLong(0);
    AtomicBoolean started = new AtomicBoolean(false);
    int storeEvery = 10_000;
    String reference = "ref-1";
    CountDownLatch poisonLatch = new CountDownLatch(1);
    environment.consumerBuilder().name(reference).stream(stream).offset(OffsetSpecification.first()).subscriptionListener(subscriptionContext -> {
        subscriptionListenerCallCount.getAndIncrement();
        OffsetSpecification offsetSpecification = started.get() ? OffsetSpecification.offset(offsetTracking.get() + 1) : subscriptionContext.offsetSpecification();
        subscriptionContext.offsetSpecification(offsetSpecification);
    }).messageHandler((context, message) -> {
        receivedMessages.incrementAndGet();
        offsetTracking.set(context.offset());
        started.set(true);
        if ("poison".equals(new String(message.getBodyAsBinary()))) {
            poisonLatch.countDown();
        }
    }).autoTrackingStrategy().flushInterval(// long flush interval
    Duration.ofMinutes(60)).messageCountBeforeStorage(storeEvery).builder().build();
    AtomicInteger publishedMessages = new AtomicInteger(0);
    Producer producer = environment.producerBuilder().stream(stream).build();
    IntConsumer publish = messagesToPublish -> {
        publishedMessages.addAndGet(messagesToPublish);
        IntStream.range(0, messagesToPublish).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
        }));
    };
    publish.accept(storeEvery * 2 - 100);
    waitAtMost(5, () -> receivedMessages.get() == publishedMessages.get());
    Host.killConnection("rabbitmq-stream-consumer-0");
    publish.accept(storeEvery * 2);
    producer.send(producer.messageBuilder().addData("poison".getBytes()).build(), confirmationStatus -> {
    });
    latchAssert(poisonLatch).completes();
    // no duplicates because the custom offset tracking overrides the stored offset in the
    // subscription listener
    assertThat(receivedMessages).hasValue(publishedMessages.get() + 1);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnaryOperator(java.util.function.UnaryOperator) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) MethodSource(org.junit.jupiter.params.provider.MethodSource) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) String.format(java.lang.String.format) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ConsumerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Collections(java.util.Collections) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) IntConsumer(java.util.function.IntConsumer) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Producer

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

the class StreamConsumerTest method autoTrackingShouldStorePeriodicallyAndAfterInactivity.

@Test
void autoTrackingShouldStorePeriodicallyAndAfterInactivity() throws Exception {
    AtomicInteger messageCount = new AtomicInteger(0);
    int storeEvery = 10_000;
    String reference = "ref-1";
    AtomicLong lastReceivedOffset = new AtomicLong(0);
    environment.consumerBuilder().name(reference).stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
        lastReceivedOffset.set(context.offset());
        messageCount.incrementAndGet();
    }).autoTrackingStrategy().flushInterval(Duration.ofSeconds(1).plusMillis(100)).messageCountBeforeStorage(storeEvery).builder().build();
    Producer producer = environment.producerBuilder().stream(stream).build();
    IntStream.range(0, storeEvery * 2).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
    }));
    waitAtMost(5, () -> messageCount.get() == storeEvery * 2);
    Client client = cf.get();
    waitAtMost(5, () -> client.queryOffset(reference, stream).getOffset() == lastReceivedOffset.get());
    int extraMessages = storeEvery / 10;
    IntStream.range(0, extraMessages).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
    }));
    waitAtMost(5, () -> messageCount.get() == storeEvery * 2 + extraMessages);
    waitAtMost(5, () -> client.queryOffset(reference, stream).getOffset() == lastReceivedOffset.get());
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnaryOperator(java.util.function.UnaryOperator) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) MethodSource(org.junit.jupiter.params.provider.MethodSource) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) String.format(java.lang.String.format) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ConsumerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Collections(java.util.Collections) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicLong(java.util.concurrent.atomic.AtomicLong) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Producer

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

the class StreamConsumerTest method externalOffsetTrackingWithSubscriptionListener.

@Test
@DisabledIfRabbitMqCtlNotSet
void externalOffsetTrackingWithSubscriptionListener() throws Exception {
    AtomicInteger subscriptionListenerCallCount = new AtomicInteger(0);
    AtomicInteger receivedMessages = new AtomicInteger(0);
    AtomicLong offsetTracking = new AtomicLong(0);
    AtomicBoolean started = new AtomicBoolean(false);
    environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).subscriptionListener(subscriptionContext -> {
        subscriptionListenerCallCount.incrementAndGet();
        OffsetSpecification offsetSpecification = started.get() ? OffsetSpecification.offset(offsetTracking.get() + 1) : subscriptionContext.offsetSpecification();
        subscriptionContext.offsetSpecification(offsetSpecification);
    }).messageHandler((context, message) -> {
        receivedMessages.incrementAndGet();
        offsetTracking.set(context.offset());
        started.set(true);
    }).build();
    int messageCount = 10_000;
    Producer producer = environment.producerBuilder().stream(stream).build();
    Runnable publish = () -> IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
    }));
    publish.run();
    waitAtMost(5, () -> receivedMessages.get() == messageCount);
    assertThat(offsetTracking.get()).isGreaterThanOrEqualTo(messageCount - 1);
    Host.killConnection("rabbitmq-stream-consumer-0");
    waitAtMost(recoveryInitialDelay.multipliedBy(2), () -> subscriptionListenerCallCount.get() == 2);
    publish.run();
    waitAtMost(5, () -> receivedMessages.get() == messageCount * 2);
    assertThat(offsetTracking.get()).isGreaterThanOrEqualTo(messageCount * 2 - 1);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StreamDoesNotExistException(com.rabbitmq.stream.StreamDoesNotExistException) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnaryOperator(java.util.function.UnaryOperator) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) MethodSource(org.junit.jupiter.params.provider.MethodSource) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) String.format(java.lang.String.format) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ConsumerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ConsumerInfo) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Collections(java.util.Collections) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DisabledIfRabbitMqCtlNotSet(com.rabbitmq.stream.impl.TestUtils.DisabledIfRabbitMqCtlNotSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with Producer

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

the class SuperStreamProducerTest method messageIsNackedIfNoRouteFound.

@Test
void messageIsNackedIfNoRouteFound() throws Exception {
    routingKeys = new String[] { "amer", "emea", "apac" };
    declareSuperStreamTopology(connection, superStream, routingKeys);
    Producer producer = environment.producerBuilder().stream(superStream).routing(message -> message.getApplicationProperties().get("region").toString()).key().producerBuilder().build();
    AtomicBoolean confirmed = new AtomicBoolean(true);
    AtomicInteger code = new AtomicInteger();
    CountDownLatch publishLatch = new CountDownLatch(1);
    producer.send(producer.messageBuilder().applicationProperties().entry("region", "atlantis").messageBuilder().build(), confirmationStatus -> {
        confirmed.set(confirmationStatus.isConfirmed());
        code.set(confirmationStatus.getCode());
        publishLatch.countDown();
    });
    assertThat(latchAssert(publishLatch)).completes(5);
    assertThat(confirmed).isFalse();
    assertThat(code).hasValue(Constants.CODE_NO_ROUTE_FOUND);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Connection(com.rabbitmq.client.Connection) TestUtils.deleteSuperStreamTopology(com.rabbitmq.stream.impl.TestUtils.deleteSuperStreamTopology) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TestUtils.declareSuperStreamTopology(com.rabbitmq.stream.impl.TestUtils.declareSuperStreamTopology) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) EventLoopGroup(io.netty.channel.EventLoopGroup) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) AfterEach(org.junit.jupiter.api.AfterEach) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 15 with Producer

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

the class SuperStreamTest method allMessagesSentWithRoutingKeyRoutingShouldBeThenConsumed.

@Test
void allMessagesSentWithRoutingKeyRoutingShouldBeThenConsumed() throws Exception {
    int messageCount = 10_000 * partitions;
    routingKeys = new String[] { "amer", "emea", "apac" };
    declareSuperStreamTopology(connection, superStream, routingKeys);
    Producer producer = environment.producerBuilder().stream(superStream).routing(message -> message.getApplicationProperties().get("region").toString()).key().producerBuilder().build();
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().applicationProperties().entry("region", routingKeys[i % routingKeys.length]).messageBuilder().build(), confirmationStatus -> publishLatch.countDown()));
    assertThat(latchAssert(publishLatch)).completes(5);
    AtomicInteger totalCount = new AtomicInteger(0);
    CountDownLatch consumeLatch = new CountDownLatch(messageCount);
    environment.consumerBuilder().superStream(superStream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
        totalCount.incrementAndGet();
        consumeLatch.countDown();
    }).build();
    latchAssert(consumeLatch).completes();
    assertThat(totalCount.get()).isEqualTo(messageCount);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) EventLoopGroup(io.netty.channel.EventLoopGroup) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Connection(com.rabbitmq.client.Connection) UUID(java.util.UUID) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.deleteSuperStreamTopology(com.rabbitmq.stream.impl.TestUtils.deleteSuperStreamTopology) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TestUtils.declareSuperStreamTopology(com.rabbitmq.stream.impl.TestUtils.declareSuperStreamTopology) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Producer (com.rabbitmq.stream.Producer)51 Environment (com.rabbitmq.stream.Environment)44 CountDownLatch (java.util.concurrent.CountDownLatch)36 Test (org.junit.jupiter.api.Test)36 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)32 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)30 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)30 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)30 BeforeEach (org.junit.jupiter.api.BeforeEach)30 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)30 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)29 EventLoopGroup (io.netty.channel.EventLoopGroup)29 IntStream (java.util.stream.IntStream)28 UUID (java.util.UUID)26 AfterEach (org.junit.jupiter.api.AfterEach)26 TestInfo (org.junit.jupiter.api.TestInfo)26 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)25 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)24 Duration (java.time.Duration)24