Search in sources :

Example 26 with Producer

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

the class SuperStreamUsage method producerSimple.

void producerSimple() {
    Environment environment = Environment.builder().build();
    // tag::producer-simple[]
    Producer producer = environment.producerBuilder().stream(// <1>
    "invoices").routing(// <2>
    message -> message.getProperties().getMessageIdAsString()).producerBuilder().build();
    // ...
    // <4>
    producer.close();
// end::producer-simple[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment)

Example 27 with Producer

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

the class SuperStreamUsage method producerKeyRoutingStrategy.

void producerKeyRoutingStrategy() {
    Environment environment = Environment.builder().build();
    // tag::producer-key-routing-strategy[]
    Producer producer = environment.producerBuilder().stream("invoices").routing(// <1>
    msg -> msg.getApplicationProperties().get("region").toString()).key().producerBuilder().build();
// end::producer-key-routing-strategy[]
}
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) Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment)

Example 28 with Producer

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

the class SuperStreamUsage method producerCustomRoutingStrategy.

void producerCustomRoutingStrategy() {
    Environment environment = Environment.builder().build();
    // tag::producer-custom-routing-strategy[]
    AtomicLong messageCount = new AtomicLong(0);
    RoutingStrategy routingStrategy = (message, metadata) -> {
        List<String> partitions = metadata.partitions();
        String stream = partitions.get((int) messageCount.getAndIncrement() % partitions.size());
        return Collections.singletonList(stream);
    };
    Producer producer = environment.producerBuilder().stream("invoices").routing(// <1>
    null).strategy(// <2>
    routingStrategy).producerBuilder().build();
// end::producer-custom-routing-strategy[]
}
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) AtomicLong(java.util.concurrent.atomic.AtomicLong) Producer(com.rabbitmq.stream.Producer) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy) Environment(com.rabbitmq.stream.Environment) List(java.util.List)

Example 29 with Producer

use of com.rabbitmq.stream.Producer 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 30 with Producer

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

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