Class KafkaProducerFactory

java.lang.Object
io.micronaut.configuration.kafka.KafkaProducerFactory
All Implemented Interfaces:
ProducerRegistry, TransactionalProducerRegistry

@Factory public class KafkaProducerFactory extends Object implements ProducerRegistry, TransactionalProducerRegistry
A registry class for Kafka Producer instances.
Since:
1.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    KafkaProducerFactory(io.micronaut.context.BeanContext beanContext, SerdeRegistry serdeRegistry, ProducerFactory producerFactory)
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    close(org.apache.kafka.clients.producer.Producer<?,?> producer)
    Closed the producer.
    <K, V> org.apache.kafka.clients.producer.Producer<K,V>
    getProducer(@Nullable io.micronaut.inject.InjectionPoint<org.apache.kafka.clients.producer.KafkaProducer<K,V>> injectionPoint, @Nullable AbstractKafkaProducerConfiguration<K,V> producerConfiguration)
    Creates a new KafkaProducer for the given configuration.
    <K, V> org.apache.kafka.clients.producer.Producer<K,V>
    getProducer(String id, io.micronaut.core.type.Argument<K> keyType, io.micronaut.core.type.Argument<V> valueType)
    Returns a managed Producer.
    <K, V> org.apache.kafka.clients.producer.Producer<K,V>
    getTransactionalProducer(String id, String transactionalId, io.micronaut.core.type.Argument<K> keyType, io.micronaut.core.type.Argument<V> valueType)
    Returns a transactional managed Producer.
    protected void
    Shuts down any existing clients.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • KafkaProducerFactory

      public KafkaProducerFactory(io.micronaut.context.BeanContext beanContext, SerdeRegistry serdeRegistry, ProducerFactory producerFactory)
      Default constructor.
      Parameters:
      beanContext - The bean context
      serdeRegistry - The serde registry
      producerFactory - The producer factory
  • Method Details

    • getProducer

      @Bean @Any public <K, V> org.apache.kafka.clients.producer.Producer<K,V> getProducer(@Nullable @Nullable io.micronaut.inject.InjectionPoint<org.apache.kafka.clients.producer.KafkaProducer<K,V>> injectionPoint, @Nullable @Parameter @Nullable AbstractKafkaProducerConfiguration<K,V> producerConfiguration)
      Creates a new KafkaProducer for the given configuration.
      Type Parameters:
      K - The key type
      V - The value type
      Parameters:
      injectionPoint - The injection point used to create the bean
      producerConfiguration - An optional producer configuration
      Returns:
      The consumer
    • stop

      @PreDestroy protected void stop()
      Shuts down any existing clients.
    • getProducer

      public <K, V> org.apache.kafka.clients.producer.Producer<K,V> getProducer(String id, io.micronaut.core.type.Argument<K> keyType, io.micronaut.core.type.Argument<V> valueType)
      Description copied from interface: ProducerRegistry
      Returns a managed Producer.
      Specified by:
      getProducer in interface ProducerRegistry
      Type Parameters:
      K - The key generic type
      V - The value generic type
      Parameters:
      id - The id of the producer.
      keyType - The key type
      valueType - The value type
      Returns:
      The producer
    • getTransactionalProducer

      public <K, V> org.apache.kafka.clients.producer.Producer<K,V> getTransactionalProducer(String id, String transactionalId, io.micronaut.core.type.Argument<K> keyType, io.micronaut.core.type.Argument<V> valueType)
      Description copied from interface: TransactionalProducerRegistry
      Returns a transactional managed Producer.
      Specified by:
      getTransactionalProducer in interface TransactionalProducerRegistry
      Type Parameters:
      K - The key generic type
      V - The value generic type
      Parameters:
      id - The client id of the producer.
      transactionalId - The transactional id of the producer.
      keyType - The key type
      valueType - The value type
      Returns:
      The producer
    • close

      public void close(org.apache.kafka.clients.producer.Producer<?,?> producer)
      Description copied from interface: TransactionalProducerRegistry
      Closed the producer. Should be used for cases when ProducerFencedException is thrown.
      Specified by:
      close in interface TransactionalProducerRegistry
      Parameters:
      producer - The producer