Search in sources :

Example 1 with KafkaCompanion

use of io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceTest method testRetry.

@SuppressWarnings({ "rawtypes" })
@Test
@Tag(TestTags.SLOW)
public void testRetry() {
    // This test need an individual Kafka container
    try (StrimziKafkaContainer kafka = KafkaBrokerExtension.createKafkaContainer()) {
        kafka.start();
        await().until(kafka::isRunning);
        MapBasedConfig config = newCommonConfigForSource().with("bootstrap.servers", kafka.getBootstrapServers()).with("value.deserializer", IntegerDeserializer.class.getName()).with("retry", true).with("retry-attempts", 100).with("retry-max-wait", 30);
        KafkaCompanion kafkaCompanion = new KafkaCompanion(kafka.getBootstrapServers());
        KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
        source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
        List<KafkaRecord> messages1 = new ArrayList<>();
        source.getStream().subscribe().with(messages1::add);
        AtomicInteger counter = new AtomicInteger();
        kafkaCompanion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
        await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
        try (@SuppressWarnings("unused") StrimziKafkaContainer container = KafkaBrokerExtension.restart(kafka, 2)) {
            kafkaCompanion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
            await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 20);
            assertThat(messages1.size()).isGreaterThanOrEqualTo(20);
        }
    }
}
Also used : StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 2 with KafkaCompanion

use of io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithLegacyMetadataTest method testRetry.

@SuppressWarnings({ "rawtypes" })
@Test
@Tag(TestTags.SLOW)
public void testRetry() {
    // This test need an individual Kafka container
    try (StrimziKafkaContainer kafka = KafkaBrokerExtension.createKafkaContainer()) {
        kafka.start();
        await().until(kafka::isRunning);
        MapBasedConfig config = newCommonConfigForSource().with("bootstrap.servers", kafka.getBootstrapServers()).with("value.deserializer", IntegerDeserializer.class.getName()).with("retry", true).with("retry-attempts", 100).with("retry-max-wait", 30);
        KafkaCompanion kafkaCompanion = new KafkaCompanion(kafka.getBootstrapServers());
        KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
        source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
        List<KafkaRecord> messages1 = new ArrayList<>();
        source.getStream().subscribe().with(messages1::add);
        kafkaCompanion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
        await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
        try (@SuppressWarnings("unused") StrimziKafkaContainer container = restart(kafka, 2)) {
            kafkaCompanion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, 10), 10);
            await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 20);
            assertThat(messages1.size()).isGreaterThanOrEqualTo(20);
        }
    }
}
Also used : StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 3 with KafkaCompanion

use of io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion in project smallrye-reactive-messaging by smallrye.

the class KafkaClientReactiveStreamsPublisherTest method init.

@BeforeAll
public static void init(@KafkaBootstrapServers String bootstrapServers) {
    companion = new KafkaCompanion(bootstrapServers);
    String newTopic = "tck-" + UUID.randomUUID();
    companion.topics().createAndWait(newTopic, partitions);
    topic = newTopic;
    vertx = Vertx.vertx();
    companion.produceStrings().usingGenerator(i -> new ProducerRecord<>(topic, Integer.toString(i % partitions), Integer.toString(i)), MESSAGE_COUNT).awaitCompletion(Duration.ofSeconds(30));
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) HashMap(java.util.HashMap) Multi(io.smallrye.mutiny.Multi) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) AfterAll(org.junit.jupiter.api.AfterAll) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PublisherVerification(org.reactivestreams.tck.junit5.PublisherVerification) BeforeAll(org.junit.jupiter.api.BeforeAll) KafkaBootstrapServers(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension.KafkaBootstrapServers) Duration(java.time.Duration) Map(java.util.Map) KafkaBrokerExtension(io.smallrye.reactive.messaging.kafka.companion.test.KafkaBrokerExtension) CountKafkaCdiEvents(io.smallrye.reactive.messaging.kafka.CountKafkaCdiEvents) Tuple2(io.smallrye.mutiny.tuples.Tuple2) Publisher(org.reactivestreams.Publisher) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) UUID(java.util.UUID) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) AfterEach(org.junit.jupiter.api.AfterEach) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) TestEnvironment(org.reactivestreams.tck.TestEnvironment) Vertx(io.vertx.mutiny.core.Vertx) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with KafkaCompanion

use of io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion in project smallrye-reactive-messaging by smallrye.

the class KafkaTest method serdes.

public void serdes() {
    // <serdes>
    KafkaCompanion companion = new KafkaCompanion("localhost:9092");
    // Register serde to the companion
    companion.registerSerde(Orders.class, new OrdersSerializer(), new OrdersDeserializer());
    // Companion will configure consumer accordingly
    ConsumerTask<Integer, Orders> orders = companion.consume(Integer.class, Orders.class).fromTopics("orders", 1000).awaitCompletion();
    for (ConsumerRecord<Integer, Orders> order : orders) {
    // ... check consumed records
    }
// </serdes>
}
Also used : KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion)

Example 5 with KafkaCompanion

use of io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion in project smallrye-reactive-messaging by smallrye.

the class KafkaTest method newCompanion.

public void newCompanion() {
    // <companion>
    // Create companion with bootstrap servers and API timeout (default is 10 seconds)
    KafkaCompanion companion = new KafkaCompanion("localhost:9092", Duration.ofSeconds(5));
    // Create producer and start producer task
    ProducerBuilder<String, Integer> producer = companion.produceIntegers().withClientId("my-producer").withProp("max.block.ms", "5000");
    producer.usingGenerator(i -> new ProducerRecord<>("topic", i), 100);
    // Create consumer and start consumer task
    ConsumerBuilder<String, Integer> consumer = companion.consumeIntegers().withGroupId("my-group").withCommitAsyncWhen(record -> true);
    ConsumerTask<String, Integer> records = consumer.fromTopics("topic", Duration.ofSeconds(10));
    // Await completion and assert consumed record count
    Assertions.assertEquals(records.awaitCompletion().count(), 100);
// </companion>
}
Also used : KafkaCompanion(io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion)

Aggregations

KafkaCompanion (io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion)11 Test (org.junit.jupiter.api.Test)5 BeforeAll (org.junit.jupiter.api.BeforeAll)4 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)3 HashMap (java.util.HashMap)3 Tag (org.junit.jupiter.api.Tag)3 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)2 StrimziKafkaContainer (io.strimzi.test.container.StrimziKafkaContainer)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 Endpoint (org.apache.kafka.common.Endpoint)2 Multi (io.smallrye.mutiny.Multi)1 Tuple2 (io.smallrye.mutiny.tuples.Tuple2)1 CountKafkaCdiEvents (io.smallrye.reactive.messaging.kafka.CountKafkaCdiEvents)1 IncomingKafkaRecord (io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord)1 KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)1 UnsatisfiedInstance (io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance)1 TestTags (io.smallrye.reactive.messaging.kafka.companion.TestTags)1