use of io.micronaut.configuration.kafka.config.DefaultKafkaConsumerConfiguration 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);
}
use of io.micronaut.configuration.kafka.config.DefaultKafkaConsumerConfiguration in project micronaut-kafka by micronaut-projects.
the class KafkaConsumerProcessor method configureDeserializers.
private void configureDeserializers(final ExecutableMethod<?, ?> method, final DefaultKafkaConsumerConfiguration consumerConfiguration) {
final Properties properties = consumerConfiguration.getConfig();
// figure out the Key deserializer
final Argument<?> bodyArgument = findBodyArgument(method);
if (!properties.containsKey(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG) && !consumerConfiguration.getKeyDeserializer().isPresent()) {
final Optional<Argument<?>> keyArgument = Arrays.stream(method.getArguments()).filter(arg -> arg.isAnnotationPresent(KafkaKey.class)).findFirst();
if (keyArgument.isPresent()) {
consumerConfiguration.setKeyDeserializer(serdeRegistry.pickDeserializer(keyArgument.get()));
} else {
// noinspection SingleStatementInBlock
if (bodyArgument != null && ConsumerRecord.class.isAssignableFrom(bodyArgument.getType())) {
final Optional<Argument<?>> keyType = bodyArgument.getTypeVariable("K");
if (keyType.isPresent()) {
consumerConfiguration.setKeyDeserializer(serdeRegistry.pickDeserializer(keyType.get()));
} else {
consumerConfiguration.setKeyDeserializer(new ByteArrayDeserializer());
}
} else {
consumerConfiguration.setKeyDeserializer(new ByteArrayDeserializer());
}
}
}
// figure out the Value deserializer
if (!properties.containsKey(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG) && !consumerConfiguration.getValueDeserializer().isPresent()) {
if (bodyArgument == null) {
// noinspection SingleStatementInBlock
consumerConfiguration.setValueDeserializer(new StringDeserializer());
} else {
if (ConsumerRecord.class.isAssignableFrom(bodyArgument.getType())) {
final Optional<Argument<?>> valueType = bodyArgument.getTypeVariable("V");
if (valueType.isPresent()) {
consumerConfiguration.setValueDeserializer(serdeRegistry.pickDeserializer(valueType.get()));
} else {
consumerConfiguration.setValueDeserializer(new StringDeserializer());
}
} else {
final boolean batch = method.isTrue(KafkaListener.class, "batch");
consumerConfiguration.setValueDeserializer(serdeRegistry.pickDeserializer(batch ? getComponentType(bodyArgument) : bodyArgument));
}
}
}
debugDeserializationConfiguration(method, consumerConfiguration, properties);
}
Aggregations