Search in sources :

Example 1 with KafkaConsumerRebalanceListener

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

the class ReactiveKafkaConsumerTest method testOffsetResetLatest.

@Test
public void testOffsetResetLatest() throws Exception {
    int count = 10;
    sendMessages(0, count);
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).with("topic", topic).with(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
    SingletonInstance<KafkaConsumerRebalanceListener> listeners = new SingletonInstance<>(groupId, getKafkaConsumerRebalanceListenerAwaitingAssignation());
    source = new KafkaSource<>(vertx, groupId, new KafkaConnectorIncomingConfiguration(config), listeners, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), 0);
    AssertSubscriber<IncomingKafkaRecord<Integer, String>> subscriber = source.getStream().invoke(this::onReceive).subscribe().withSubscriber(AssertSubscriber.create(10));
    await().until(() -> {
        Map<TopicPartition, Long> map = source.getConsumer().getPositions().await().indefinitely();
        return map.values().stream().mapToLong(l -> l).sum() == 10;
    });
    subscriber.assertSubscribed().assertHasNotReceivedAnyItem();
    sendMessages(count, count);
    await().untilAsserted(() -> assertThat(subscriber.getItems()).hasSize(count));
    await().until(() -> {
        Map<TopicPartition, Long> map = source.getConsumer().getPositions().await().indefinitely();
        return map.values().stream().mapToLong(l -> l).sum() == 20;
    });
    subscriber.cancel();
    checkConsumedMessages(count, count);
}
Also used : KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) TopicPartition(org.apache.kafka.common.TopicPartition) SingletonInstance(io.smallrye.reactive.messaging.kafka.base.SingletonInstance) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 2 with KafkaConsumerRebalanceListener

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

the class ClientTestBase method createSourceSeekToOffset.

public KafkaSource<Integer, String> createSourceSeekToOffset() {
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
    SingletonInstance<KafkaConsumerRebalanceListener> listeners = new SingletonInstance<>(groupId, getKafkaConsumerRebalanceListenerAwaitingAssignationAndSeekToOffset());
    source = new KafkaSource<>(vertx, groupId, new KafkaConnectorIncomingConfiguration(config), listeners, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), 0);
    return source;
}
Also used : KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) SingletonInstance(io.smallrye.reactive.messaging.kafka.base.SingletonInstance) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 3 with KafkaConsumerRebalanceListener

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

the class ClientTestBase method createSourceSeekToEnd.

public KafkaSource<Integer, String> createSourceSeekToEnd() {
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
    SingletonInstance<KafkaConsumerRebalanceListener> listeners = new SingletonInstance<>(groupId, getKafkaConsumerRebalanceListenerAwaitingAssignationAndSeekToEnd());
    source = new KafkaSource<>(vertx, groupId, new KafkaConnectorIncomingConfiguration(config), listeners, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), 0);
    return source;
}
Also used : KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) SingletonInstance(io.smallrye.reactive.messaging.kafka.base.SingletonInstance) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 4 with KafkaConsumerRebalanceListener

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

the class ClientTestBase method createSourceSeekToBeginning.

public KafkaSource<Integer, String> createSourceSeekToBeginning() {
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
    SingletonInstance<KafkaConsumerRebalanceListener> listeners = new SingletonInstance<>(groupId, getKafkaConsumerRebalanceListenerAwaitingAssignationAndSeekToBeginning());
    source = new KafkaSource<>(vertx, groupId, new KafkaConnectorIncomingConfiguration(config), listeners, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), 0);
    return source;
}
Also used : KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) SingletonInstance(io.smallrye.reactive.messaging.kafka.base.SingletonInstance) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 5 with KafkaConsumerRebalanceListener

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

the class RebalanceListeners method createRebalanceListener.

static ConsumerRebalanceListener createRebalanceListener(ReactiveKafkaConsumer<?, ?> reactiveKafkaConsumer, KafkaConnectorIncomingConfiguration config, String consumerGroup, Instance<KafkaConsumerRebalanceListener> instances, KafkaCommitHandler commitHandler) {
    Optional<KafkaConsumerRebalanceListener> rebalanceListener = findMatchingListener(config, consumerGroup, instances);
    if (rebalanceListener.isPresent()) {
        KafkaConsumerRebalanceListener listener = rebalanceListener.get();
        return new ConsumerRebalanceListener() {

            @Override
            public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
                log.executingConsumerRevokedRebalanceListener(consumerGroup);
                try {
                    reactiveKafkaConsumer.removeFromQueueRecordsFromTopicPartitions(partitions);
                    commitHandler.partitionsRevoked(partitions);
                    listener.onPartitionsRevoked(reactiveKafkaConsumer.unwrap(), partitions);
                    log.executedConsumerRevokedRebalanceListener(consumerGroup);
                } catch (RuntimeException e) {
                    log.unableToExecuteConsumerRevokedRebalanceListener(consumerGroup, e);
                    throw e;
                }
            }

            @Override
            public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                try {
                    if (reactiveKafkaConsumer.isPaused()) {
                        reactiveKafkaConsumer.unwrap().pause(partitions);
                    }
                    commitHandler.partitionsAssigned(partitions);
                    listener.onPartitionsAssigned(reactiveKafkaConsumer.unwrap(), partitions);
                    log.executedConsumerAssignedRebalanceListener(consumerGroup);
                } catch (RuntimeException e) {
                    log.reEnablingConsumerForGroup(consumerGroup);
                    throw e;
                }
            }
        };
    } else {
        return new ConsumerRebalanceListener() {

            @Override
            public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
                reactiveKafkaConsumer.removeFromQueueRecordsFromTopicPartitions(partitions);
                commitHandler.partitionsRevoked(partitions);
            }

            @Override
            public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                if (reactiveKafkaConsumer.isPaused()) {
                    reactiveKafkaConsumer.unwrap().pause(partitions);
                }
                commitHandler.partitionsAssigned(partitions);
            }
        };
    }
}
Also used : KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener)

Aggregations

KafkaConsumerRebalanceListener (io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener)5 KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)4 SingletonInstance (io.smallrye.reactive.messaging.kafka.base.SingletonInstance)4 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)4 IncomingKafkaRecord (io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord)1 ConsumerRebalanceListener (org.apache.kafka.clients.consumer.ConsumerRebalanceListener)1 TopicPartition (org.apache.kafka.common.TopicPartition)1