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
- Author:
- Gary Russell
-
-
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
-
-
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 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, java.lang.String consumerGroupId)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, send, send, send, send, send, send, sendDefault, sendDefault, sendDefault, sendDefault, sendOffsetsToTransaction, sendOffsetsToTransaction, setAllowNonTransactional, setApplicationContext, setBeanName, setCloseTimeout, setDefaultTopic, setMessageConverter, setMicrometerEnabled, setMicrometerTags, setProducerListener, setTransactionIdPrefix
-
-
-
-
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
public void sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, java.lang.String consumerGroupId)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.
-
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>
-
-