Search in sources :

Example 1 with KafkaListener

use of io.micronaut.configuration.kafka.annotation.KafkaListener in project micronaut-kafka by micronaut-projects.

the class KafkaConsumerProcessor method process.

@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
    List<AnnotationValue<Topic>> topicAnnotations = method.getDeclaredAnnotationValuesByType(Topic.class);
    final AnnotationValue<KafkaListener> consumerAnnotation = method.getAnnotation(KafkaListener.class);
    if (CollectionUtils.isEmpty(topicAnnotations)) {
        topicAnnotations = beanDefinition.getDeclaredAnnotationValuesByType(Topic.class);
    }
    if (consumerAnnotation == null || CollectionUtils.isEmpty(topicAnnotations)) {
        // No topics to consume
        return;
    }
    final Class<?> beanType = beanDefinition.getBeanType();
    String groupId = consumerAnnotation.stringValue("groupId").filter(StringUtils::isNotEmpty).orElseGet(() -> applicationConfiguration.getName().orElse(beanType.getName()));
    if (consumerAnnotation.isTrue("uniqueGroupId")) {
        groupId = groupId + "_" + UUID.randomUUID();
    }
    final String clientId = consumerAnnotation.stringValue("clientId").filter(StringUtils::isNotEmpty).orElseGet(() -> applicationConfiguration.getName().map(s -> s + '-' + NameUtils.hyphenate(beanType.getSimpleName())).orElse(null));
    final OffsetStrategy offsetStrategy = consumerAnnotation.enumValue("offsetStrategy", OffsetStrategy.class).orElse(OffsetStrategy.AUTO);
    final AbstractKafkaConsumerConfiguration<?, ?> consumerConfigurationDefaults = beanContext.findBean(AbstractKafkaConsumerConfiguration.class, Qualifiers.byName(groupId)).orElse(defaultConsumerConfiguration);
    final DefaultKafkaConsumerConfiguration<?, ?> consumerConfiguration = new DefaultKafkaConsumerConfiguration<>(consumerConfigurationDefaults);
    final Properties properties = createConsumerProperties(consumerAnnotation, consumerConfiguration, clientId, groupId, offsetStrategy);
    configureDeserializers(method, consumerConfiguration);
    submitConsumerThreads(method, clientId, groupId, offsetStrategy, topicAnnotations, consumerAnnotation, consumerConfiguration, properties, beanType);
}
Also used : AbstractKafkaConsumerConfiguration(io.micronaut.configuration.kafka.config.AbstractKafkaConsumerConfiguration) OffsetStrategy(io.micronaut.configuration.kafka.annotation.OffsetStrategy) Properties(java.util.Properties) KafkaListener(io.micronaut.configuration.kafka.annotation.KafkaListener) DefaultKafkaConsumerConfiguration(io.micronaut.configuration.kafka.config.DefaultKafkaConsumerConfiguration) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Topic(io.micronaut.configuration.kafka.annotation.Topic)

Aggregations

KafkaListener (io.micronaut.configuration.kafka.annotation.KafkaListener)1 OffsetStrategy (io.micronaut.configuration.kafka.annotation.OffsetStrategy)1 Topic (io.micronaut.configuration.kafka.annotation.Topic)1 AbstractKafkaConsumerConfiguration (io.micronaut.configuration.kafka.config.AbstractKafkaConsumerConfiguration)1 DefaultKafkaConsumerConfiguration (io.micronaut.configuration.kafka.config.DefaultKafkaConsumerConfiguration)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Properties (java.util.Properties)1