io.micronaut.configuration.kafka.KafkaConsumerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-kafka Show documentation
Show all versions of micronaut-kafka Show documentation
Integration between Micronaut and Kafka Messaging
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.configuration.kafka;
import io.micronaut.configuration.kafka.config.AbstractKafkaConsumerConfiguration;
import io.micronaut.configuration.kafka.serde.JsonObjectSerde;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.TypeHint;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.CooperativeStickyAssignor;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.RangeAssignor;
import org.apache.kafka.clients.consumer.RoundRobinAssignor;
import org.apache.kafka.clients.consumer.StickyAssignor;
import org.apache.kafka.clients.producer.RoundRobinPartitioner;
import org.apache.kafka.common.security.authenticator.AbstractLogin;
import org.apache.kafka.common.security.authenticator.DefaultLogin;
import org.apache.kafka.common.security.authenticator.SaslClientCallbackHandler;
import org.apache.kafka.common.security.authenticator.SaslServerCallbackHandler;
import org.apache.kafka.common.security.plain.PlainLoginModule;
import org.apache.kafka.common.serialization.*;
import java.util.Optional;
import java.util.Properties;
/**
* A factory class for creating Kafka {@link org.apache.kafka.clients.consumer.Consumer} instances.
*
* @author Graeme Rocher
* @since 1.0
*/
@Factory
@TypeHint({
CooperativeStickyAssignor.class,
// serializers
ShortSerializer.class,
DoubleSerializer.class,
LongSerializer.class,
BytesSerializer.class,
ByteArraySerializer.class,
IntegerSerializer.class,
ByteBufferSerializer.class,
StringSerializer.class,
FloatSerializer.class,
// serdes
JsonObjectSerde.class,
// deserializers
ShortDeserializer.class,
DoubleDeserializer.class,
LongDeserializer.class,
BytesDeserializer.class,
ByteArrayDeserializer.class,
IntegerDeserializer.class,
ByteBufferDeserializer.class,
StringDeserializer.class,
FloatDeserializer.class,
// partitioners
RoundRobinPartitioner.class,
// assigners
RangeAssignor.class,
RoundRobinAssignor.class,
StickyAssignor.class,
// authentication
DefaultLogin.class,
SaslServerCallbackHandler.class,
PlainLoginModule.class,
SaslClientCallbackHandler.class,
AbstractLogin.DefaultLoginCallbackHandler.class
})
public class KafkaConsumerFactory {
/**
* Creates a new {@link KafkaConsumer} for the given configuration.
*
* @param consumerConfiguration The consumer configuration
* @param The key type
* @param The value type
* @return The consumer
*/
@Prototype
public Consumer createConsumer(
@Parameter AbstractKafkaConsumerConfiguration consumerConfiguration) {
Optional> keyDeserializer = consumerConfiguration.getKeyDeserializer();
Optional> valueDeserializer = consumerConfiguration.getValueDeserializer();
Properties config = consumerConfiguration.getConfig();
if (keyDeserializer.isPresent() && valueDeserializer.isPresent()) {
return new KafkaConsumer<>(
config,
keyDeserializer.get(),
valueDeserializer.get()
);
} else if (keyDeserializer.isPresent() || valueDeserializer.isPresent()) {
throw new ConfigurationException("Both the [keyDeserializer] and [valueDeserializer] must be set when setting either");
} else {
return new KafkaConsumer<>(
config
);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy