Search in sources :

Example 16 with Consumer

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

the class SuperStreamUsage method consumerSimple.

void consumerSimple() {
    Environment environment = Environment.builder().build();
    // tag::consumer-simple[]
    Consumer consumer = environment.consumerBuilder().superStream(// <1>
    "invoices").messageHandler((context, message) -> {
    // message processing
    }).build();
    // ...
    // <2>
    consumer.close();
// end::consumer-simple[]
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageHandler(com.rabbitmq.stream.MessageHandler) List(java.util.List) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) Collections(java.util.Collections) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment)

Example 17 with Consumer

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

the class AlarmsTest method publishingShouldBeBlockedWhenDiskAlarmIsOn.

@Test
void publishingShouldBeBlockedWhenDiskAlarmIsOn() throws Exception {
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Producer producer = env.producerBuilder().stream(stream).build();
    range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
    assertThat(latchAssert(latch.get())).completes();
    try (AutoCloseable alarm = diskAlarm()) {
        latch.set(new CountDownLatch(messageCount));
        new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
        assertThat(latchAssert(latch.get())).doesNotComplete(Duration.ofSeconds(5));
    }
    assertThat(latchAssert(latch.get())).completes();
    producer.close();
    latch.set(new CountDownLatch(messageCount * 2));
    Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
    assertThat(latchAssert(latch.get())).completes();
    consumer.close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) 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) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 18 with Consumer

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

the class AlarmsTest method diskAlarmShouldNotPreventConsumption.

@Test
void diskAlarmShouldNotPreventConsumption() throws Exception {
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Producer producer = env.producerBuilder().stream(stream).build();
    range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
    assertThat(latchAssert(latch)).completes();
    producer.close();
    try (AutoCloseable alarm = diskAlarm()) {
        latch.set(new CountDownLatch(messageCount));
        Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
        assertThat(latchAssert(latch)).completes();
        consumer.close();
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) 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) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 19 with Consumer

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

the class AlarmsTest method publishingWhenMemoryAlarmIsOnShouldWork.

@Test
void publishingWhenMemoryAlarmIsOnShouldWork() throws Exception {
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Producer producer = env.producerBuilder().stream(stream).build();
    range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
    assertThat(latchAssert(latch.get())).completes();
    try (AutoCloseable alarm = memoryAlarm()) {
        latch.set(new CountDownLatch(messageCount));
        new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
        assertThat(latchAssert(latch.get())).completes();
    }
    assertThat(latchAssert(latch.get())).completes();
    producer.close();
    latch.set(new CountDownLatch(messageCount * 2));
    Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
    assertThat(latchAssert(latch.get())).completes();
    consumer.close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) 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) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 20 with Consumer

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

the class StreamConsumerTest method autoTrackingShouldStoreAfterClosing.

@Test
void autoTrackingShouldStoreAfterClosing() throws Exception {
    int storeEvery = 10_000;
    int messageCount = storeEvery * 5 - 100;
    CountDownLatch consumeLatch = new CountDownLatch(messageCount);
    String reference = "ref-1";
    AtomicLong lastReceivedOffset = new AtomicLong(0);
    Consumer consumer = environment.consumerBuilder().name(reference).stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
        lastReceivedOffset.set(context.offset());
        consumeLatch.countDown();
    }).autoTrackingStrategy().flushInterval(// long flush interval
    Duration.ofHours(1)).messageCountBeforeStorage(storeEvery).builder().build();
    Producer producer = environment.producerBuilder().stream(stream).build();
    IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
    }));
    latchAssert(consumeLatch).completes();
    consumer.close();
    Client client = cf.get();
    waitAtMost(5, () -> client.queryOffset(reference, stream).getOffset() == lastReceivedOffset.get(), () -> format("Expecting stored offset %d to be equal to last received offset %d", 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) IntConsumer(java.util.function.IntConsumer) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Consumer (com.rabbitmq.stream.Consumer)25 Environment (com.rabbitmq.stream.Environment)24 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 Test (org.junit.jupiter.api.Test)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)21 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)20 Producer (com.rabbitmq.stream.Producer)19 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)19 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)19 EventLoopGroup (io.netty.channel.EventLoopGroup)19 BeforeEach (org.junit.jupiter.api.BeforeEach)19 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)19 Collections (java.util.Collections)18 List (java.util.List)18 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)17 Duration (java.time.Duration)17 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)16 TestInfo (org.junit.jupiter.api.TestInfo)16 AfterEach (org.junit.jupiter.api.AfterEach)15