Search in sources :

Example 1 with ArgumentInjectionPoint

use of io.micronaut.inject.ArgumentInjectionPoint in project micronaut-kafka by micronaut-projects.

the class KafkaProducerFactory method getProducer.

/**
 * Creates a new {@link KafkaProducer} for the given configuration.
 *
 * @param injectionPoint The injection point used to create the bean
 * @param producerConfiguration An optional producer configuration
 * @param <K> The key type
 * @param <V> The value type
 * @return The consumer
 */
@Bean
@Any
public <K, V> Producer<K, V> getProducer(@Nullable InjectionPoint<KafkaProducer<K, V>> injectionPoint, @Nullable @Parameter AbstractKafkaProducerConfiguration<K, V> producerConfiguration) {
    if (injectionPoint == null) {
        if (producerConfiguration != null) {
            Optional<Serializer<K>> keySerializer = producerConfiguration.getKeySerializer();
            Optional<Serializer<V>> valueSerializer = producerConfiguration.getValueSerializer();
            Properties config = producerConfiguration.getConfig();
            if (keySerializer.isPresent() && valueSerializer.isPresent()) {
                Serializer<K> ks = keySerializer.get();
                Serializer<V> vs = valueSerializer.get();
                return new KafkaProducer<>(config, ks, vs);
            } else if (keySerializer.isPresent() || valueSerializer.isPresent()) {
                throw new ConfigurationException("Both the [keySerializer] and [valueSerializer] must be set when setting either");
            } else {
                return new KafkaProducer<>(config);
            }
        } else {
            throw new ConfigurationException("No Kafka configuration specified when using direct instantiation");
        }
    }
    Argument<?> argument;
    if (injectionPoint instanceof FieldInjectionPoint) {
        argument = ((FieldInjectionPoint<?, ?>) injectionPoint).asArgument();
    } else if (injectionPoint instanceof ArgumentInjectionPoint) {
        argument = ((ArgumentInjectionPoint<?, ?>) injectionPoint).getArgument();
    } else {
        throw new ConfigurationException("Cannot directly retrieve KafkaProducer instances. Use @Inject or constructor injection");
    }
    Argument<?> k = argument.getTypeVariable("K").orElse(null);
    Argument<?> v = argument.getTypeVariable("V").orElse(null);
    if (k == null || v == null) {
        throw new ConfigurationException("@KafkaClient used on type missing generic argument values for Key and Value: " + injectionPoint);
    }
    final String id = injectionPoint.getAnnotationMetadata().stringValue(KafkaClient.class).orElse(null);
    return getKafkaProducer(id, null, k, v, false);
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Properties(java.util.Properties) ArgumentInjectionPoint(io.micronaut.inject.ArgumentInjectionPoint) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) KafkaClient(io.micronaut.configuration.kafka.annotation.KafkaClient) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) Serializer(org.apache.kafka.common.serialization.Serializer) Any(io.micronaut.context.annotation.Any) Bean(io.micronaut.context.annotation.Bean)

Aggregations

KafkaClient (io.micronaut.configuration.kafka.annotation.KafkaClient)1 Any (io.micronaut.context.annotation.Any)1 Bean (io.micronaut.context.annotation.Bean)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 ArgumentInjectionPoint (io.micronaut.inject.ArgumentInjectionPoint)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 Properties (java.util.Properties)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 Serializer (org.apache.kafka.common.serialization.Serializer)1