Search in sources :

Example 46 with Producer

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

the class StreamProducerTest method sendToNonExistingStreamShouldReturnUnconfirmedStatus.

@Test
void sendToNonExistingStreamShouldReturnUnconfirmedStatus() throws Exception {
    Client client = cf.get();
    String s = UUID.randomUUID().toString();
    Client.Response response = client.create(s);
    assertThat(response.isOk()).isTrue();
    Producer producer = environment.producerBuilder().stream(s).build();
    response = client.delete(s);
    assertThat(response.isOk()).isTrue();
    // it must close
    waitAtMost(10, () -> !((StreamProducer) producer).isOpen());
    CountDownLatch confirmationLatch = new CountDownLatch(1);
    AtomicReference<ConfirmationStatus> confirmationStatusReference = new AtomicReference<>();
    producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
        confirmationStatusReference.set(confirmationStatus);
        confirmationLatch.countDown();
    });
    assertThat(confirmationLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(confirmationStatusReference.get()).isNotNull();
    assertThat(confirmationStatusReference.get().isConfirmed()).isFalse();
    assertThat(confirmationStatusReference.get().getCode()).isEqualTo(Constants.CODE_PRODUCER_CLOSED);
}
Also used : ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Producer(com.rabbitmq.stream.Producer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with Producer

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

the class StreamProducerTest method messagesShouldBeDeDuplicatedWhenUsingNameAndPublishingId.

@ParameterizedTest
@ValueSource(ints = { 1, 7 })
void messagesShouldBeDeDuplicatedWhenUsingNameAndPublishingId(int subEntrySize) throws Exception {
    int lineCount = 50_000;
    int firstWaveLineCount = lineCount / 5;
    int backwardCount = firstWaveLineCount / 10;
    SortedSet<Integer> document = new TreeSet<>();
    IntStream.range(0, lineCount).forEach(i -> document.add(i));
    Producer producer = environment.producerBuilder().name("producer-1").stream(stream).subEntrySize(subEntrySize).build();
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(firstWaveLineCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Consumer<Integer> publishMessage = i -> producer.send(producer.messageBuilder().publishingId(i).addData(String.valueOf(i).getBytes()).build(), confirmationHandler);
    document.headSet(firstWaveLineCount).forEach(publishMessage);
    assertThat(latch.get().await(10, TimeUnit.SECONDS)).isTrue();
    latch.set(new CountDownLatch(lineCount - firstWaveLineCount + backwardCount));
    document.tailSet(firstWaveLineCount - backwardCount).forEach(publishMessage);
    assertThat(latch.get().await(5, TimeUnit.SECONDS)).isTrue();
    CountDownLatch consumeLatch = new CountDownLatch(lineCount);
    AtomicInteger consumed = new AtomicInteger();
    environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
        consumed.incrementAndGet();
        consumeLatch.countDown();
    }).build();
    assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
    Thread.sleep(1000);
    // if we are using sub-entries, we cannot avoid duplicates.
    // here, a sub-entry in the second wave, right at the end of the re-submitted
    // values will contain those duplicates, because its publishing ID will be
    // the one of its last message, so the server will accept the whole sub-entry,
    // including the duplicates.
    assertThat(consumed.get()).isEqualTo(lineCount + backwardCount % subEntrySize);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SortedSet(java.util.SortedSet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) TreeSet(java.util.TreeSet) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProducerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) Host(com.rabbitmq.stream.Host) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Status(com.rabbitmq.stream.impl.StreamProducer.Status) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Compression(com.rabbitmq.stream.compression.Compression) Producer(com.rabbitmq.stream.Producer) Collectors(java.util.stream.Collectors) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Level(ch.qos.logback.classic.Level) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TreeSet(java.util.TreeSet) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 48 with Producer

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

the class SuperStreamTest method allMessagesSentWithHashRoutingShouldBeThenConsumed.

@Test
void allMessagesSentWithHashRoutingShouldBeThenConsumed() throws Exception {
    int messageCount = 10_000 * partitions;
    declareSuperStreamTopology(connection, superStream, partitions);
    Producer producer = environment.producerBuilder().stream(superStream).routing(message -> message.getProperties().getMessageIdAsString()).producerBuilder().build();
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().properties().messageId(UUID.randomUUID().toString()).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)

Example 49 with Producer

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

the class TlsTest method environmentPublisherConsumer.

@Test
void environmentPublisherConsumer() throws Exception {
    try (Environment env = Environment.builder().uri("rabbitmq-stream+tls://localhost").addressResolver(addr -> new Address("localhost", Client.DEFAULT_TLS_PORT)).tls().sslContext(SslContextBuilder.forClient().trustManager(caCertificate()).build()).environmentBuilder().build()) {
        int messageCount = 10_000;
        CountDownLatch latchConfirm = new CountDownLatch(messageCount);
        Producer producer = env.producerBuilder().stream(this.stream).build();
        ConfirmationHandler confirmationHandler = confirmationStatus -> latchConfirm.countDown();
        IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler));
        assertThat(latchAssert(latchConfirm)).completes();
        CountDownLatch latchConsume = new CountDownLatch(messageCount);
        env.consumerBuilder().stream(this.stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latchConsume.countDown()).build();
        assertThat(latchAssert(latchConsume)).completes();
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) CertificateFactory(java.security.cert.CertificateFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SSLParameters(javax.net.ssl.SSLParameters) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) InetAddress(java.net.InetAddress) TRUST_EVERYTHING_TRUST_MANAGER(com.rabbitmq.stream.impl.Utils.TRUST_EVERYTHING_TRUST_MANAGER) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) Charset(java.nio.charset.Charset) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) Host(com.rabbitmq.stream.Host) SslContext(io.netty.handler.ssl.SslContext) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Files(java.nio.file.Files) Environment(com.rabbitmq.stream.Environment) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Producer(com.rabbitmq.stream.Producer) UnknownHostException(java.net.UnknownHostException) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) KeyFactory(java.security.KeyFactory) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) SSLException(javax.net.ssl.SSLException) AfterEach(org.junit.jupiter.api.AfterEach) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) SslHandler(io.netty.handler.ssl.SslHandler) PrivateKey(java.security.PrivateKey) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Address(com.rabbitmq.stream.Address) InetAddress(java.net.InetAddress) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 50 with Producer

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

the class SuperStreamProducerTest method allMessagesSentToSuperStreamWithRoutingKeyRoutingShouldBeThenConsumed.

@Test
void allMessagesSentToSuperStreamWithRoutingKeyRoutingShouldBeThenConsumed() throws Exception {
    int messageCount = 10_000;
    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);
    Map<String, AtomicLong> counts = new ConcurrentHashMap<>();
    AtomicLong totalCount = new AtomicLong(0);
    for (String routingKey : routingKeys) {
        String stream = superStream + "-" + routingKey;
        AtomicLong streamCount = new AtomicLong(0);
        counts.put(stream, streamCount);
        environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
            streamCount.incrementAndGet();
            totalCount.incrementAndGet();
        }).build();
    }
    waitAtMost(10, () -> totalCount.get() == messageCount);
    assertThat(counts.values().stream().map(AtomicLong::get)).hasSameSizeAs(routingKeys).doesNotContain(0L);
    assertThat(counts.values().stream().map(AtomicLong::get).reduce(0L, Long::sum)).isEqualTo(messageCount);
}
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) AtomicLong(java.util.concurrent.atomic.AtomicLong) Producer(com.rabbitmq.stream.Producer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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