Class 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>
    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
    • 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 as LinkedHashMap 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​(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 class KafkaTemplate<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 interface KafkaOperations<java.lang.Object,​java.lang.Object>
        Overrides:
        execute in class KafkaTemplate<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 interface KafkaOperations<java.lang.Object,​java.lang.Object>
        Overrides:
        executeInTransaction in class KafkaTemplate<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: 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 a KafkaAwareTransactionManager) since the container will take care of sending the offsets to the transaction.
        Specified by:
        sendOffsetsToTransaction in interface KafkaOperations<java.lang.Object,​java.lang.Object>
        Overrides:
        sendOffsetsToTransaction in class KafkaTemplate<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: KafkaOperations
        See Producer.metrics().
        Specified by:
        metrics in interface KafkaOperations<java.lang.Object,​java.lang.Object>
        Overrides:
        metrics in class KafkaTemplate<java.lang.Object,​java.lang.Object>
        Returns:
        the metrics.