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>
AKafkaTemplate
that 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
-
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> T
execute(KafkaOperations.ProducerCallback<java.lang.Object,java.lang.Object,T> callback)
Execute some arbitrary operation(s) on the producer and return the result.<T> T
executeInTransaction(KafkaOperations.OperationsCallback<java.lang.Object,java.lang.Object,T> callback)
Execute some arbitrary operation(s) on the operations and return the result.void
flush()
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()
.void
sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets)
Deprecated.void
sendOffsetsToTransaction(java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, java.lang.String consumerGroupId)
Deprecated.void
sendOffsetsToTransaction(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, 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, setMessageConverter, setMessagingConverter, setMicrometerEnabled, setMicrometerTags, setMicrometerTagsProvider, 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, usingCompletableFuture
-
-
-
-
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 asLinkedHashMap
should 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:KafkaTemplate
Return the producer factory used by this template.- Specified by:
getProducerFactory
in interfaceKafkaOperations<java.lang.Object,java.lang.Object>
- Overrides:
getProducerFactory
in 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: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<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:KafkaOperations
Execute some arbitrary operation(s) on the producer and return the result.- Specified by:
execute
in interfaceKafkaOperations<java.lang.Object,java.lang.Object>
- Overrides:
execute
in 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: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<java.lang.Object,java.lang.Object>
- Overrides:
executeInTransaction
in 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: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.- Specified by:
sendOffsetsToTransaction
in interfaceKafkaOperations<java.lang.Object,java.lang.Object>
- Overrides:
sendOffsetsToTransaction
in 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:KafkaOperations
When 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:
sendOffsetsToTransaction
in interfaceKafkaOperations<java.lang.Object,java.lang.Object>
- Overrides:
sendOffsetsToTransaction
in 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: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<java.lang.Object,java.lang.Object>
- Overrides:
sendOffsetsToTransaction
in 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:KafkaOperations
SeeProducer.metrics()
.- Specified by:
metrics
in interfaceKafkaOperations<java.lang.Object,java.lang.Object>
- Overrides:
metrics
in classKafkaTemplate<java.lang.Object,java.lang.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<java.lang.Object,java.lang.Object>
- Overrides:
flush
in classKafkaTemplate<java.lang.Object,java.lang.Object>
-
-