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