Package org.springframework.kafka.core
Class RoutingKafkaTemplate
- java.lang.Object
-
- org.springframework.kafka.core.KafkaTemplate<java.lang.Object,java.lang.Object>
-
- org.springframework.kafka.core.RoutingKafkaTemplate
-
- All Implemented Interfaces:
java.util.EventListener,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.DisposableBean,org.springframework.context.ApplicationContextAware,org.springframework.context.ApplicationListener<org.springframework.context.event.ContextStoppedEvent>,KafkaOperations<java.lang.Object,java.lang.Object>
public class RoutingKafkaTemplate extends KafkaTemplate<java.lang.Object,java.lang.Object>
AKafkaTemplatethat routes messages based on the topic name. Does not support transactions,flush(),metrics(), andexecute(org.springframework.kafka.core.KafkaOperations.ProducerCallback), only simple send operations.- Since:
- 2.5
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.springframework.kafka.core.KafkaOperations
KafkaOperations.OperationsCallback<K,V,T>, KafkaOperations.ProducerCallback<K,V,T>
-
-
Field Summary
-
Fields inherited from class org.springframework.kafka.core.KafkaTemplate
logger
-
Fields inherited from interface org.springframework.kafka.core.KafkaOperations
DEFAULT_POLL_TIMEOUT
-
-
Constructor Summary
Constructors Constructor Description RoutingKafkaTemplate(java.util.Map<java.util.regex.Pattern,ProducerFactory<java.lang.Object,java.lang.Object>> factories)Construct an instance with the provided properties.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T> Texecute(KafkaOperations.ProducerCallback<java.lang.Object,java.lang.Object,T> callback)Execute some arbitrary operation(s) on the producer and return the result.<T> TexecuteInTransaction(KafkaOperations.OperationsCallback<java.lang.Object,java.lang.Object,T> callback)Execute some arbitrary operation(s) on the operations and return the result.voidflush()Flush the producer.ProducerFactory<java.lang.Object,java.lang.Object>getProducerFactory()Return the producer factory used by this template.ProducerFactory<java.lang.Object,java.lang.Object>getProducerFactory(java.lang.String topic)Return the producer factory used by this template based on the topic.java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric>metrics()SeeProducer.metrics().voidsendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets)Deprecated.voidsendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, java.lang.String consumerGroupId)Deprecated.voidsendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata)When running in a transaction, send the consumer offset(s) to the transaction.-
Methods inherited from class org.springframework.kafka.core.KafkaTemplate
closeProducer, destroy, doSend, getDefaultTopic, getMessageConverter, getTheProducer, getTransactionIdPrefix, inTransaction, isAllowNonTransactional, isTransactional, onApplicationEvent, partitionsFor, receive, receive, send, send, send, send, send, send, sendDefault, sendDefault, sendDefault, sendDefault, setAllowNonTransactional, setApplicationContext, setBeanName, setCloseTimeout, setConsumerFactory, setDefaultTopic, setMessageConverter, setMessagingConverter, setMicrometerEnabled, setMicrometerTags, setProducerListener, setTransactionIdPrefix
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.springframework.kafka.core.KafkaOperations
receive, receive
-
-
-
-
Constructor Detail
-
RoutingKafkaTemplate
public RoutingKafkaTemplate(java.util.Map<java.util.regex.Pattern,ProducerFactory<java.lang.Object,java.lang.Object>> factories)
Construct an instance with the provided properties. The topic patterns will be traversed in order so an ordered map, such asLinkedHashMapshould be used with more specific patterns declared first.- Parameters:
factories- the factories.
-
-
Method Detail
-
getProducerFactory
public ProducerFactory<java.lang.Object,java.lang.Object> getProducerFactory()
Description copied from class:KafkaTemplateReturn the producer factory used by this template.- Specified by:
getProducerFactoryin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
getProducerFactoryin classKafkaTemplate<java.lang.Object,java.lang.Object>- Returns:
- the factory.
-
getProducerFactory
public ProducerFactory<java.lang.Object,java.lang.Object> getProducerFactory(java.lang.String topic)
Description copied from class:KafkaTemplateReturn the producer factory used by this template based on the topic. The default implementation returns the only producer factory.- Overrides:
getProducerFactoryin classKafkaTemplate<java.lang.Object,java.lang.Object>- Parameters:
topic- the topic.- Returns:
- the factory.
-
execute
public <T> T execute(KafkaOperations.ProducerCallback<java.lang.Object,java.lang.Object,T> callback)
Description copied from interface:KafkaOperationsExecute some arbitrary operation(s) on the producer and return the result.- Specified by:
executein interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
executein classKafkaTemplate<java.lang.Object,java.lang.Object>- Type Parameters:
T- the result type.- Parameters:
callback- the callback.- Returns:
- the result.
-
executeInTransaction
public <T> T executeInTransaction(KafkaOperations.OperationsCallback<java.lang.Object,java.lang.Object,T> callback)
Description copied from interface:KafkaOperationsExecute some arbitrary operation(s) on the operations and return the result. The operations are invoked within a local transaction and do not participate in a global transaction (if present).- Specified by:
executeInTransactionin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
executeInTransactionin classKafkaTemplate<java.lang.Object,java.lang.Object>- Type Parameters:
T- the result type.- Parameters:
callback- the callback.- Returns:
- the result.
-
sendOffsetsToTransaction
@Deprecated public void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, java.lang.String consumerGroupId)Deprecated.Description copied from interface:KafkaOperationsWhen running in a transaction, send the consumer offset(s) to the transaction. It is not necessary to call this method if the operations are invoked on a listener container thread (and the listener container is configured with aKafkaAwareTransactionManager) since the container will take care of sending the offsets to the transaction.- Specified by:
sendOffsetsToTransactionin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
sendOffsetsToTransactionin classKafkaTemplate<java.lang.Object,java.lang.Object>- Parameters:
offsets- The offsets.consumerGroupId- the consumer's group.id.
-
sendOffsetsToTransaction
@Deprecated public void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets)
Deprecated.Description copied from interface:KafkaOperationsWhen running in a transaction, send the consumer offset(s) to the transaction. The group id is obtained fromKafkaUtils.getConsumerGroupId(). It is not necessary to call this method if the operations are invoked on a listener container thread (and the listener container is configured with aKafkaAwareTransactionManager) since the container will take care of sending the offsets to the transaction.- Specified by:
sendOffsetsToTransactionin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
sendOffsetsToTransactionin classKafkaTemplate<java.lang.Object,java.lang.Object>- Parameters:
offsets- The offsets.
-
sendOffsetsToTransaction
public void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata)Description copied from interface:KafkaOperationsWhen running in a transaction, send the consumer offset(s) to the transaction. It is not necessary to call this method if the operations are invoked on a listener container thread (and the listener container is configured with aKafkaAwareTransactionManager) since the container will take care of sending the offsets to the transaction. Use with 2.5 brokers or later.- Specified by:
sendOffsetsToTransactionin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
sendOffsetsToTransactionin classKafkaTemplate<java.lang.Object,java.lang.Object>- Parameters:
offsets- The offsets.groupMetadata- the consumer group metadata.- See Also:
Producer.sendOffsetsToTransaction(Map, ConsumerGroupMetadata)
-
metrics
public java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric> metrics()
Description copied from interface:KafkaOperationsSeeProducer.metrics().- Specified by:
metricsin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
metricsin classKafkaTemplate<java.lang.Object,java.lang.Object>- Returns:
- the metrics.
-
flush
public void flush()
Description copied from class:KafkaTemplateFlush the producer.Note It only makes sense to invoke this method if the
ProducerFactoryserves up a singleton producer (such as theDefaultKafkaProducerFactory).- Specified by:
flushin interfaceKafkaOperations<java.lang.Object,java.lang.Object>- Overrides:
flushin classKafkaTemplate<java.lang.Object,java.lang.Object>
-
-