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);
}
}
}
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);
}
}
}
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));
}
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>
}
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>
}
Aggregations