K - the key type.V - the value type.public class DefaultKafkaProducerFactory<K,V> extends java.lang.Object implements ProducerFactory<K,V>, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextStoppedEvent>, org.springframework.beans.factory.DisposableBean
ProducerFactory implementation for a singleton shared
Producer instance.
This implementation will return the same Producer instance (if transactions are
not enabled) for the provided Map configs and optional Serializer
keySerializer, valueSerializer implementations on each
createProducer() invocation.
The Producer is wrapped and the underlying KafkaProducer instance is
not actually closed when Producer.close() is invoked. The KafkaProducer
is physically closed when DisposableBean.destroy() is invoked or when the
application context publishes a ContextStoppedEvent. You can also invoke
reset().
Setting setTransactionIdPrefix(String) enables transactions; in which case, a
cache of producers is maintained; closing a producer returns it to the cache. The
producers are closed and the cache is cleared when the factory is destroyed, the
application context stopped, or the reset() method is called.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
DefaultKafkaProducerFactory.CloseSafeProducer<K,V>
A wrapper class for the delegate.
|
| Modifier and Type | Field and Description |
|---|---|
static java.time.Duration |
DEFAULT_PHYSICAL_CLOSE_TIMEOUT
The default close timeout duration as 30 seconds.
|
| Constructor and Description |
|---|
DefaultKafkaProducerFactory(java.util.Map<java.lang.String,java.lang.Object> configs)
Construct a factory with the provided configuration.
|
DefaultKafkaProducerFactory(java.util.Map<java.lang.String,java.lang.Object> configs,
org.apache.kafka.common.serialization.Serializer<K> keySerializer,
org.apache.kafka.common.serialization.Serializer<V> valueSerializer)
Construct a factory with the provided configuration and
Serializers. |
| Modifier and Type | Method and Description |
|---|---|
void |
closeProducerFor(java.lang.String suffix)
Remove the specified producer from the cache and close it.
|
protected org.apache.kafka.clients.producer.Producer<K,V> |
createKafkaProducer()
Subclasses must return a raw producer which will be wrapped in a
DefaultKafkaProducerFactory.CloseSafeProducer. |
org.apache.kafka.clients.producer.Producer<K,V> |
createProducer()
Create a producer.
|
protected org.apache.kafka.clients.producer.Producer<K,V> |
createTransactionalProducer()
Subclasses must return a producer from the
getCache() or a
new raw producer wrapped in a DefaultKafkaProducerFactory.CloseSafeProducer. |
void |
destroy() |
protected java.util.concurrent.BlockingQueue<DefaultKafkaProducerFactory.CloseSafeProducer<K,V>> |
getCache() |
java.util.Map<java.lang.String,java.lang.Object> |
getConfigurationProperties()
Return an unmodifiable reference to the configuration map for this factory.
|
protected java.lang.String |
getTransactionIdPrefix() |
boolean |
isProducerPerConsumerPartition()
Return the producerPerConsumerPartition.
|
void |
onApplicationEvent(org.springframework.context.event.ContextStoppedEvent event) |
void |
reset()
Close the
Producer(s) and clear the cache of transactional
Producer(s). |
void |
setApplicationContext(org.springframework.context.ApplicationContext applicationContext) |
void |
setKeySerializer(org.apache.kafka.common.serialization.Serializer<K> keySerializer) |
void |
setPhysicalCloseTimeout(int physicalCloseTimeout)
|
void |
setProducerPerConsumerPartition(boolean producerPerConsumerPartition)
Set to false to revert to the previous behavior of a simple incrementing
transactional.id suffix for each producer instead of maintaining a producer
for each group/topic/partition.
|
void |
setTransactionIdPrefix(java.lang.String transactionIdPrefix)
Set a prefix for the
ProducerConfig.TRANSACTIONAL_ID_CONFIG config. |
void |
setValueSerializer(org.apache.kafka.common.serialization.Serializer<V> valueSerializer) |
boolean |
transactionCapable()
Return true if the factory supports transactions.
|
public static final java.time.Duration DEFAULT_PHYSICAL_CLOSE_TIMEOUT
public DefaultKafkaProducerFactory(java.util.Map<java.lang.String,java.lang.Object> configs)
configs - the configuration.public DefaultKafkaProducerFactory(java.util.Map<java.lang.String,java.lang.Object> configs,
@Nullable
org.apache.kafka.common.serialization.Serializer<K> keySerializer,
@Nullable
org.apache.kafka.common.serialization.Serializer<V> valueSerializer)
Serializers.
Also configures a transactionIdPrefix as a value from the
ProducerConfig.TRANSACTIONAL_ID_CONFIG if provided.
This config is going to be overridden with a suffix for target Producer instance.configs - the configuration.keySerializer - the key Serializer.valueSerializer - the value Serializer.public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
throws org.springframework.beans.BeansException
setApplicationContext in interface org.springframework.context.ApplicationContextAwareorg.springframework.beans.BeansExceptionpublic void setKeySerializer(@Nullable
org.apache.kafka.common.serialization.Serializer<K> keySerializer)
public void setValueSerializer(@Nullable
org.apache.kafka.common.serialization.Serializer<V> valueSerializer)
public void setPhysicalCloseTimeout(int physicalCloseTimeout)
reset() or destroy() is invoked).
Specified in seconds; default DEFAULT_PHYSICAL_CLOSE_TIMEOUT.physicalCloseTimeout - the timeout in seconds.public final void setTransactionIdPrefix(java.lang.String transactionIdPrefix)
ProducerConfig.TRANSACTIONAL_ID_CONFIG config.
By default a ProducerConfig.TRANSACTIONAL_ID_CONFIG value from configs is used as a prefix
in the target producer configs.transactionIdPrefix - the prefix.protected java.lang.String getTransactionIdPrefix()
public void setProducerPerConsumerPartition(boolean producerPerConsumerPartition)
producerPerConsumerPartition - false to revert.public boolean isProducerPerConsumerPartition()
isProducerPerConsumerPartition in interface ProducerFactory<K,V>public java.util.Map<java.lang.String,java.lang.Object> getConfigurationProperties()
public boolean transactionCapable()
ProducerFactorytransactionCapable in interface ProducerFactory<K,V>public void destroy()
destroy in interface org.springframework.beans.factory.DisposableBeanpublic void onApplicationEvent(org.springframework.context.event.ContextStoppedEvent event)
onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextStoppedEvent>public void reset()
Producer(s) and clear the cache of transactional
Producer(s).public org.apache.kafka.clients.producer.Producer<K,V> createProducer()
ProducerFactorycreateProducer in interface ProducerFactory<K,V>protected org.apache.kafka.clients.producer.Producer<K,V> createKafkaProducer()
DefaultKafkaProducerFactory.CloseSafeProducer.protected org.apache.kafka.clients.producer.Producer<K,V> createTransactionalProducer()
getCache() or a
new raw producer wrapped in a DefaultKafkaProducerFactory.CloseSafeProducer.protected java.util.concurrent.BlockingQueue<DefaultKafkaProducerFactory.CloseSafeProducer<K,V>> getCache()
public void closeProducerFor(java.lang.String suffix)
ProducerFactorycloseProducerFor in interface ProducerFactory<K,V>suffix - the producer's transaction id suffix.