Class KafkaProducerMessageHandler<K,V>

java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.handler.MessageHandlerSupport
org.springframework.integration.handler.AbstractMessageHandler
org.springframework.integration.handler.AbstractMessageProducingHandler
org.springframework.integration.handler.AbstractReplyProducingMessageHandler
org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler<K,V>
Type Parameters:
K - the key type.
V - the value type.
All Implemented Interfaces:
org.reactivestreams.Subscriber<org.springframework.messaging.Message<?>>, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.context.Lifecycle, org.springframework.core.Ordered, org.springframework.integration.context.ExpressionCapable, org.springframework.integration.context.Orderable, org.springframework.integration.core.MessageProducer, org.springframework.integration.handler.HeaderPropagationAware, org.springframework.integration.IntegrationPattern, org.springframework.integration.support.context.NamedComponent, org.springframework.integration.support.management.IntegrationManagement, org.springframework.integration.support.management.ManageableLifecycle, org.springframework.integration.support.management.TrackableComponent, org.springframework.messaging.MessageHandler, reactor.core.CoreSubscriber<org.springframework.messaging.Message<?>>

public class KafkaProducerMessageHandler<K,V> extends org.springframework.integration.handler.AbstractReplyProducingMessageHandler implements org.springframework.integration.support.management.ManageableLifecycle
A Message Handler for Apache Kafka; when supplied with a ReplyingKafkaTemplate it is used as the handler in an outbound gateway. When supplied with a simple KafkaTemplate it used as the handler in an outbound channel adapter.

The handler also supports receiving a pre-built ProducerRecord payload. In that case, most configuration properties (setTopicExpression(Expression) etc.) are ignored. If the handler is used as gateway, the ProducerRecord will have its headers enhanced to add the KafkaHeaders.REPLY_TOPIC unless it already contains such a header. The handler will not map any additional headers; providing such a payload assumes the headers have already been mapped.

Since:
5.4
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Creates a ProducerRecord from a Message and/or properties derived from configuration and/or the message.

    Nested classes/interfaces inherited from class org.springframework.integration.handler.AbstractReplyProducingMessageHandler

    org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler

    Nested classes/interfaces inherited from interface org.springframework.integration.support.management.IntegrationManagement

    org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides
  • Field Summary

    Fields inherited from class org.springframework.integration.handler.AbstractMessageProducingHandler

    messagingTemplate

    Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport

    EXPRESSION_PARSER, logger

    Fields inherited from interface org.springframework.integration.support.management.IntegrationManagement

    METER_PREFIX, RECEIVE_COUNTER_NAME, SEND_TIMER_NAME

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    KafkaProducerMessageHandler(org.springframework.kafka.core.KafkaTemplate<K,V> kafkaTemplate)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
     
     
    protected org.springframework.messaging.MessageChannel
     
    org.springframework.kafka.support.KafkaHeaderMapper
     
    org.springframework.kafka.core.KafkaTemplate<?,?>
     
    protected org.springframework.messaging.MessageChannel
     
    protected org.springframework.messaging.MessageChannel
     
    protected Object
    handleRequestMessage(org.springframework.messaging.Message<?> message)
     
    boolean
     
    void
    processSendResult(org.springframework.messaging.Message<?> message, org.apache.kafka.clients.producer.ProducerRecord<K,V> producerRecord, CompletableFuture<org.springframework.kafka.support.SendResult<K,V>> future, org.springframework.messaging.MessageChannel metadataChannel)
     
    void
    setAssignmentDuration(Duration assignmentDuration)
    Set the time to wait for partition assignment, when used as a gateway, to determine the default reply-to topic/partition.
    void
    setErrorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy errorMessageStrategy)
    Set the error message strategy implementation to use when sending error messages after send failures.
    void
    setFlushExpression(org.springframework.expression.Expression flushExpression)
    Specify a SpEL expression that evaluates to a Boolean to determine whether the producer should be flushed after the send.
    void
    setFuturesChannel(org.springframework.messaging.MessageChannel futuresChannel)
    Set the futures channel.
    void
    setFuturesChannelName(String futuresChannelName)
    Set the futures channel name.
    void
    setHeaderMapper(org.springframework.kafka.support.KafkaHeaderMapper headerMapper)
    Set the header mapper to use.
    void
    setMessageKeyExpression(org.springframework.expression.Expression messageKeyExpression)
     
    void
    setPartitionIdExpression(org.springframework.expression.Expression partitionIdExpression)
     
    void
    Set a KafkaProducerMessageHandler.ProducerRecordCreator to create the ProducerRecord.
    void
    setReplyMessageConverter(org.springframework.kafka.support.converter.RecordMessageConverter messageConverter)
    Set a message converter for gateway replies.
    void
    When using a type-aware message converter (such as StringJsonMessageConverter, set the payload type the converter should create.
    void
    setSendFailureChannel(org.springframework.messaging.MessageChannel sendFailureChannel)
    Set the failure channel.
    void
    setSendFailureChannelName(String sendFailureChannelName)
    Set the failure channel name.
    void
    setSendSuccessChannel(org.springframework.messaging.MessageChannel sendSuccessChannel)
    Set the success channel.
    void
    setSendSuccessChannelName(String sendSuccessChannelName)
    Set the Success channel name.
    final void
    setSendTimeout(long sendTimeout)
    Specify a timeout in milliseconds for how long this KafkaProducerMessageHandler should wait for send operation results.
    void
    setSendTimeoutExpression(org.springframework.expression.Expression sendTimeoutExpression)
    Specify a SpEL expression to evaluate a timeout in milliseconds for how long this KafkaProducerMessageHandler should wait for send operation results.
    void
    setSync(boolean sync)
    A boolean indicating if the KafkaProducerMessageHandler should wait for the send operation results or not.
    void
    setTimeoutBuffer(int timeoutBuffer)
    Set a buffer, in milliseconds, added to the configured delivery.timeout.ms to determine the minimum time to wait for the send future completion when sync is true.
    void
    setTimestampExpression(org.springframework.expression.Expression timestampExpression)
    Specify a SpEL expression to evaluate a timestamp that will be added in the Kafka record.
    void
    setTopicExpression(org.springframework.expression.Expression topicExpression)
     
    void
    setUseTemplateConverter(boolean useTemplateConverter)
    Set to true to use the template's message converter to create the ProducerRecord instead of the producerRecordCreator.
    void
     
    void
     

    Methods inherited from class org.springframework.integration.handler.AbstractReplyProducingMessageHandler

    doInvokeAdvisedRequestHandler, getBeanClassLoader, getIntegrationPatternType, getRequiresReply, handleMessageInternal, hasAdviceChain, onInit, setAdviceChain, setBeanClassLoader, setRequiresReply

    Methods inherited from class org.springframework.integration.handler.AbstractMessageProducingHandler

    addNotPropagatedHeaders, createOutputMessage, getNotPropagatedHeaders, getOutputChannel, isAsync, messageBuilderForReply, produceOutput, resolveErrorChannel, sendErrorMessage, sendOutput, sendOutputs, setAsync, setNotPropagatedHeaders, setOutputChannel, setOutputChannelName, setupMessageProcessor, shouldCopyRequestHeaders, shouldSplitOutput, updateNotPropagatedHeaders

    Methods inherited from class org.springframework.integration.handler.AbstractMessageHandler

    handleMessage, onComplete, onError, onNext, onSubscribe, setObservationConvention

    Methods inherited from class org.springframework.integration.handler.MessageHandlerSupport

    buildSendTimer, destroy, getManagedName, getManagedType, getMetricsCaptor, getObservationRegistry, getOrder, getOverrides, isLoggingEnabled, isObserved, registerMetricsCaptor, registerObservationRegistry, sendTimer, setLoggingEnabled, setManagedName, setManagedType, setOrder, setShouldTrack, shouldTrack

    Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport

    afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentName, getConversionService, getExpression, getIntegrationProperties, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentName, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface reactor.core.CoreSubscriber

    currentContext

    Methods inherited from interface org.springframework.integration.support.management.IntegrationManagement

    getThisAs

    Methods inherited from interface org.springframework.integration.support.context.NamedComponent

    getBeanName, getComponentName
  • Constructor Details

    • KafkaProducerMessageHandler

      public KafkaProducerMessageHandler(org.springframework.kafka.core.KafkaTemplate<K,V> kafkaTemplate)
  • Method Details

    • setTopicExpression

      public void setTopicExpression(org.springframework.expression.Expression topicExpression)
    • setMessageKeyExpression

      public void setMessageKeyExpression(org.springframework.expression.Expression messageKeyExpression)
    • setPartitionIdExpression

      public void setPartitionIdExpression(org.springframework.expression.Expression partitionIdExpression)
    • setTimestampExpression

      public void setTimestampExpression(org.springframework.expression.Expression timestampExpression)
      Specify a SpEL expression to evaluate a timestamp that will be added in the Kafka record. The resulting value should be a Long type representing epoch time in milliseconds.
      Parameters:
      timestampExpression - the Expression for timestamp to wait for result fo send operation.
    • setFlushExpression

      public void setFlushExpression(org.springframework.expression.Expression flushExpression)
      Specify a SpEL expression that evaluates to a Boolean to determine whether the producer should be flushed after the send. Defaults to looking for a Boolean value in a KafkaIntegrationHeaders.FLUSH header; false if absent.
      Parameters:
      flushExpression - the Expression.
    • setHeaderMapper

      public void setHeaderMapper(org.springframework.kafka.support.KafkaHeaderMapper headerMapper)
      Set the header mapper to use.
      Parameters:
      headerMapper - the mapper; can be null to disable header mapping.
    • getHeaderMapper

      public org.springframework.kafka.support.KafkaHeaderMapper getHeaderMapper()
    • getKafkaTemplate

      public org.springframework.kafka.core.KafkaTemplate<?,?> getKafkaTemplate()
    • setSync

      public void setSync(boolean sync)
      A boolean indicating if the KafkaProducerMessageHandler should wait for the send operation results or not. Defaults to false. In sync mode a downstream send operation exception will be re-thrown.
      Parameters:
      sync - the send mode; async by default.
    • setSendTimeout

      public final void setSendTimeout(long sendTimeout)
      Specify a timeout in milliseconds for how long this KafkaProducerMessageHandler should wait for send operation results. Defaults to the kafka delivery.timeout.ms property + 5 seconds. The timeout is applied Also applies when sending to the success or failure channels.
      Overrides:
      setSendTimeout in class org.springframework.integration.handler.AbstractMessageProducingHandler
      Parameters:
      sendTimeout - the timeout to wait for result for a send operation.
    • setSendTimeoutExpression

      public void setSendTimeoutExpression(org.springframework.expression.Expression sendTimeoutExpression)
      Specify a SpEL expression to evaluate a timeout in milliseconds for how long this KafkaProducerMessageHandler should wait for send operation results. Defaults to the kafka delivery.timeout.ms property + 5 seconds. The timeout is applied only in sync mode. If this expression yields a result that is less than that value, the higher value is used.
      Parameters:
      sendTimeoutExpression - the Expression for timeout to wait for result for a send operation.
      See Also:
    • setSendFailureChannel

      public void setSendFailureChannel(org.springframework.messaging.MessageChannel sendFailureChannel)
      Set the failure channel. After a send failure, an ErrorMessage will be sent to this channel with a payload of a KafkaSendFailureException with the failed message and cause.
      Parameters:
      sendFailureChannel - the failure channel.
    • setSendFailureChannelName

      public void setSendFailureChannelName(String sendFailureChannelName)
      Set the failure channel name. After a send failure, an ErrorMessage will be sent to this channel name with a payload of a KafkaSendFailureException with the failed message and cause.
      Parameters:
      sendFailureChannelName - the failure channel name.
    • setSendSuccessChannel

      public void setSendSuccessChannel(org.springframework.messaging.MessageChannel sendSuccessChannel)
      Set the success channel.
      Parameters:
      sendSuccessChannel - the Success channel.
    • setSendSuccessChannelName

      public void setSendSuccessChannelName(String sendSuccessChannelName)
      Set the Success channel name.
      Parameters:
      sendSuccessChannelName - the Success channel name.
    • setFuturesChannel

      public void setFuturesChannel(org.springframework.messaging.MessageChannel futuresChannel)
      Set the futures channel.
      Parameters:
      futuresChannel - the futures channel.
    • setFuturesChannelName

      public void setFuturesChannelName(String futuresChannelName)
      Set the futures channel name.
      Parameters:
      futuresChannelName - the futures channel name.
    • setErrorMessageStrategy

      public void setErrorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy errorMessageStrategy)
      Set the error message strategy implementation to use when sending error messages after send failures. Cannot be null.
      Parameters:
      errorMessageStrategy - the implementation.
    • setReplyMessageConverter

      public void setReplyMessageConverter(org.springframework.kafka.support.converter.RecordMessageConverter messageConverter)
      Set a message converter for gateway replies.
      Parameters:
      messageConverter - the converter.
      See Also:
    • setReplyPayloadType

      public void setReplyPayloadType(Type payloadType)
      When using a type-aware message converter (such as StringJsonMessageConverter, set the payload type the converter should create. Defaults to Object.
      Parameters:
      payloadType - the type.
      See Also:
    • setProducerRecordCreator

      public void setProducerRecordCreator(KafkaProducerMessageHandler.ProducerRecordCreator<K,V> producerRecordCreator)
      Set a KafkaProducerMessageHandler.ProducerRecordCreator to create the ProducerRecord. Ignored if useTemplateConverter is true.
      Parameters:
      producerRecordCreator - the creator.
      See Also:
    • setTimeoutBuffer

      public void setTimeoutBuffer(int timeoutBuffer)
      Set a buffer, in milliseconds, added to the configured delivery.timeout.ms to determine the minimum time to wait for the send future completion when sync is true.
      Parameters:
      timeoutBuffer - the buffer.
      See Also:
    • setUseTemplateConverter

      public void setUseTemplateConverter(boolean useTemplateConverter)
      Set to true to use the template's message converter to create the ProducerRecord instead of the producerRecordCreator.
      Parameters:
      useTemplateConverter - true to use the converter.
      Since:
      5.5.5
      See Also:
    • setAssignmentDuration

      public void setAssignmentDuration(Duration assignmentDuration)
      Set the time to wait for partition assignment, when used as a gateway, to determine the default reply-to topic/partition.
      Parameters:
      assignmentDuration - the assignmentDuration to set.
      Since:
      6.0
    • getComponentType

      public String getComponentType()
      Specified by:
      getComponentType in interface org.springframework.integration.support.context.NamedComponent
      Overrides:
      getComponentType in class org.springframework.integration.handler.MessageHandlerSupport
    • getSendFailureChannel

      @Nullable protected org.springframework.messaging.MessageChannel getSendFailureChannel()
    • getSendSuccessChannel

      protected org.springframework.messaging.MessageChannel getSendSuccessChannel()
    • getFuturesChannel

      protected org.springframework.messaging.MessageChannel getFuturesChannel()
    • doInit

      protected void doInit()
      Overrides:
      doInit in class org.springframework.integration.handler.AbstractReplyProducingMessageHandler
    • start

      public void start()
      Specified by:
      start in interface org.springframework.context.Lifecycle
      Specified by:
      start in interface org.springframework.integration.support.management.ManageableLifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface org.springframework.context.Lifecycle
      Specified by:
      stop in interface org.springframework.integration.support.management.ManageableLifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle
      Specified by:
      isRunning in interface org.springframework.integration.support.management.ManageableLifecycle
    • handleRequestMessage

      protected Object handleRequestMessage(org.springframework.messaging.Message<?> message)
      Specified by:
      handleRequestMessage in class org.springframework.integration.handler.AbstractReplyProducingMessageHandler
    • processSendResult

      public void processSendResult(org.springframework.messaging.Message<?> message, org.apache.kafka.clients.producer.ProducerRecord<K,V> producerRecord, CompletableFuture<org.springframework.kafka.support.SendResult<K,V>> future, org.springframework.messaging.MessageChannel metadataChannel) throws InterruptedException, ExecutionException
      Throws:
      InterruptedException
      ExecutionException