Package org.springframework.kafka.core
Class RoutingKafkaTemplate
java.lang.Object
org.springframework.kafka.core.KafkaTemplate<Object,Object>
org.springframework.kafka.core.RoutingKafkaTemplate
- All Implemented Interfaces:
EventListener,Aware,BeanNameAware,DisposableBean,SmartInitializingSingleton,ApplicationContextAware,ApplicationListener<ContextStoppedEvent>,KafkaOperations<Object,Object>
A
KafkaTemplate that routes messages based on the topic name. Does not support
transactions, flush(), metrics(), and
execute(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
loggerFields inherited from interface org.springframework.kafka.core.KafkaOperations
DEFAULT_POLL_TIMEOUT -
Constructor Summary
ConstructorsConstructorDescriptionRoutingKafkaTemplate(Map<Pattern, ProducerFactory<Object, Object>> factories) Construct an instance with the provided properties. -
Method Summary
Modifier and TypeMethodDescription<T> Texecute(KafkaOperations.ProducerCallback<Object, Object, T> callback) Execute some arbitrary operation(s) on the producer and return the result.<T> TExecute some arbitrary operation(s) on the operations and return the result.voidflush()Flush the producer.Return the producer factory used by this template.getProducerFactory(String topic) Return the producer factory used by this template based on the topic.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric> metrics()SeeProducer.metrics().voidsendOffsetsToTransaction(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
afterSingletonsInstantiated, closeProducer, destroy, doSend, getDefaultTopic, getKafkaAdmin, getMessageConverter, getMicrometerTagsProvider, 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, setKafkaAdmin, setMessageConverter, setMessagingConverter, setMicrometerEnabled, setMicrometerTags, setMicrometerTagsProvider, setObservationConvention, setObservationEnabled, setProducerInterceptor, setProducerListener, setTransactionIdPrefixMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.kafka.core.KafkaOperations
receive, receive
-
Constructor Details
-
RoutingKafkaTemplate
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 Details
-
getProducerFactory
Description copied from class:KafkaTemplateReturn the producer factory used by this template.- Specified by:
getProducerFactoryin interfaceKafkaOperations<Object,Object> - Overrides:
getProducerFactoryin classKafkaTemplate<Object,Object> - Returns:
- the factory.
-
getProducerFactory
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<Object,Object> - Parameters:
topic- the topic.- Returns:
- the factory.
-
execute
Description copied from interface:KafkaOperationsExecute some arbitrary operation(s) on the producer and return the result.- Specified by:
executein interfaceKafkaOperations<Object,Object> - Overrides:
executein classKafkaTemplate<Object,Object> - Type Parameters:
T- the result type.- Parameters:
callback- the callback.- Returns:
- the result.
-
executeInTransaction
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<Object,Object> - Overrides:
executeInTransactionin classKafkaTemplate<Object,Object> - Type Parameters:
T- the result type.- Parameters:
callback- the callback.- Returns:
- the result.
-
sendOffsetsToTransaction
public void sendOffsetsToTransaction(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<Object,Object> - Overrides:
sendOffsetsToTransactionin classKafkaTemplate<Object,Object> - Parameters:
offsets- The offsets.groupMetadata- the consumer group metadata.- See Also:
-
Producer.sendOffsetsToTransaction(Map, ConsumerGroupMetadata)
-
metrics
Description copied from interface:KafkaOperationsSeeProducer.metrics().- Specified by:
metricsin interfaceKafkaOperations<Object,Object> - Overrides:
metricsin classKafkaTemplate<Object,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<Object,Object> - Overrides:
flushin classKafkaTemplate<Object,Object>
-