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, Nathan Xu
-
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
ConstructorDescriptionRoutingKafkaTemplate
(Map<Pattern, ProducerFactory<Object, Object>> factories) Construct an instance with the provided properties. -
Method Summary
Modifier and TypeMethodDescription<T> T
execute
(KafkaOperations.ProducerCallback<Object, Object, T> callback) Execute some arbitrary operation(s) on the producer and return the result.<T> T
Execute some arbitrary operation(s) on the operations and return the result.void
flush()
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()
.void
sendOffsetsToTransaction
(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, setObservationRegistry, setProducerInterceptor, 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.context.ApplicationListener
supportsAsyncExecution
Methods 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 asLinkedHashMap
should be used with more specific patterns declared first.- Parameters:
factories
- the factories.
-
-
Method Details
-
getProducerFactory
Description copied from class:KafkaTemplate
Return the producer factory used by this template.- Specified by:
getProducerFactory
in interfaceKafkaOperations<Object,
Object> - Overrides:
getProducerFactory
in classKafkaTemplate<Object,
Object> - Returns:
- the factory.
-
getProducerFactory
Description copied from class:KafkaTemplate
Return the producer factory used by this template based on the topic. The default implementation returns the only producer factory.- Overrides:
getProducerFactory
in classKafkaTemplate<Object,
Object> - Parameters:
topic
- the topic.- Returns:
- the factory.
-
execute
Description copied from interface:KafkaOperations
Execute some arbitrary operation(s) on the producer and return the result.- Specified by:
execute
in interfaceKafkaOperations<Object,
Object> - Overrides:
execute
in classKafkaTemplate<Object,
Object> - Type Parameters:
T
- the result type.- Parameters:
callback
- the callback.- Returns:
- the result.
-
executeInTransaction
Description copied from interface:KafkaOperations
Execute 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:
executeInTransaction
in interfaceKafkaOperations<Object,
Object> - Overrides:
executeInTransaction
in 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:KafkaOperations
When 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:
sendOffsetsToTransaction
in interfaceKafkaOperations<Object,
Object> - Overrides:
sendOffsetsToTransaction
in classKafkaTemplate<Object,
Object> - Parameters:
offsets
- The offsets.groupMetadata
- the consumer group metadata.- See Also:
-
Producer.sendOffsetsToTransaction(Map, ConsumerGroupMetadata)
-
metrics
Description copied from interface:KafkaOperations
SeeProducer.metrics()
.- Specified by:
metrics
in interfaceKafkaOperations<Object,
Object> - Overrides:
metrics
in classKafkaTemplate<Object,
Object> - Returns:
- the metrics.
-
flush
public void flush()Description copied from class:KafkaTemplate
Flush the producer.Note It only makes sense to invoke this method if the
ProducerFactory
serves up a singleton producer (such as theDefaultKafkaProducerFactory
).- Specified by:
flush
in interfaceKafkaOperations<Object,
Object> - Overrides:
flush
in classKafkaTemplate<Object,
Object>
-