Class JdbcMessageHandler

java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.handler.MessageHandlerSupport
org.springframework.integration.handler.AbstractMessageHandler
org.springframework.integration.jdbc.JdbcMessageHandler
All Implemented Interfaces:
org.reactivestreams.Subscriber<org.springframework.messaging.Message<?>>, org.springframework.beans.factory.Aware, 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.core.Ordered, org.springframework.integration.context.ExpressionCapable, org.springframework.integration.context.Orderable, org.springframework.integration.IntegrationPattern, org.springframework.integration.support.context.NamedComponent, org.springframework.integration.support.management.IntegrationManagement, org.springframework.integration.support.management.TrackableComponent, org.springframework.messaging.MessageHandler, reactor.core.CoreSubscriber<org.springframework.messaging.Message<?>>

public class JdbcMessageHandler
extends org.springframework.integration.handler.AbstractMessageHandler
A message handler that executes an SQL update. Dynamic query parameters are supported through the SqlParameterSourceFactory abstraction, the default implementation of which wraps the message so that its bean properties can be referred to by name in the query string, e.g.
 INSERT INTO FOOS (MESSAGE_ID, PAYLOAD) VALUES (:headers[id], :payload)
 

When a message payload is an instance of Iterable, a NamedParameterJdbcOperations.batchUpdate(String, SqlParameterSource[]) is performed, where each SqlParameterSource instance is based on items wrapped into an internal Message implementation with headers from the request message.

When a preparedStatementSetter is configured, it is applied for each item in the appropriate JdbcOperations.batchUpdate(String, BatchPreparedStatementSetter) function.

NOTE: The batch update is not supported when keysGenerated is in use. N.B. do not use quotes to escape the header keys. The default SQL parameter source (from Spring JDBC) can also handle headers with dotted names (e.g. business.id)

Since:
2.0
  • Nested Class Summary

    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.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
    JdbcMessageHandler​(javax.sql.DataSource dataSource, java.lang.String updateSql)
    Constructor taking DataSource from which the DB Connection can be obtained and the select query to execute to retrieve new rows.
    JdbcMessageHandler​(org.springframework.jdbc.core.JdbcOperations jdbcOperations, java.lang.String updateSql)
    Constructor taking JdbcOperations instance to use for query execution and the select query to execute to retrieve new rows.
  • Method Summary

    Modifier and Type Method Description
    protected java.util.List<? extends java.util.Map<java.lang.String,​java.lang.Object>> executeUpdateQuery​(org.springframework.messaging.Message<?> message, boolean keysGenerated)  
    java.lang.String getComponentType()  
    protected void handleMessageInternal​(org.springframework.messaging.Message<?> message)
    Executes the update, passing the message into the SqlParameterSourceFactory.
    protected void onInit()  
    void setKeysGenerated​(boolean keysGenerated)
    Flag to indicate that the update query is an insert with auto-generated keys, which will be logged at debug level.
    void setPreparedStatementSetter​(MessagePreparedStatementSetter preparedStatementSetter)
    Specify a MessagePreparedStatementSetter to populate parameters on the PreparedStatement with the Message context.
    void setSqlParameterSourceFactory​(SqlParameterSourceFactory sqlParameterSourceFactory)  

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

    handleMessage, onComplete, onError, onNext, onSubscribe

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

    buildSendTimer, destroy, getIntegrationPatternType, getManagedName, getManagedType, getMetricsCaptor, getOrder, getOverrides, isLoggingEnabled, registerMetricsCaptor, 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, getIntegrationProperty, 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

    • JdbcMessageHandler

      public JdbcMessageHandler​(javax.sql.DataSource dataSource, java.lang.String updateSql)
      Constructor taking DataSource from which the DB Connection can be obtained and the select query to execute to retrieve new rows.
      Parameters:
      dataSource - Must not be null
      updateSql - query to execute
    • JdbcMessageHandler

      public JdbcMessageHandler​(org.springframework.jdbc.core.JdbcOperations jdbcOperations, java.lang.String updateSql)
      Constructor taking JdbcOperations instance to use for query execution and the select query to execute to retrieve new rows.
      Parameters:
      jdbcOperations - instance to use for query execution
      updateSql - query to execute
  • Method Details

    • setKeysGenerated

      public void setKeysGenerated​(boolean keysGenerated)
      Flag to indicate that the update query is an insert with auto-generated keys, which will be logged at debug level.
      Parameters:
      keysGenerated - the flag value to set
    • setSqlParameterSourceFactory

      public void setSqlParameterSourceFactory​(SqlParameterSourceFactory sqlParameterSourceFactory)
    • setPreparedStatementSetter

      public void setPreparedStatementSetter​(@Nullable MessagePreparedStatementSetter preparedStatementSetter)
      Specify a MessagePreparedStatementSetter to populate parameters on the PreparedStatement with the Message context.

      This is a low-level alternative to the SqlParameterSourceFactory.

      Parameters:
      preparedStatementSetter - the MessagePreparedStatementSetter to set.
      Since:
      4.2
    • getComponentType

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

      protected void onInit()
      Overrides:
      onInit in class org.springframework.integration.context.IntegrationObjectSupport
    • handleMessageInternal

      protected void handleMessageInternal​(org.springframework.messaging.Message<?> message)
      Executes the update, passing the message into the SqlParameterSourceFactory.
      Specified by:
      handleMessageInternal in class org.springframework.integration.handler.AbstractMessageHandler
    • executeUpdateQuery

      protected java.util.List<? extends java.util.Map<java.lang.String,​java.lang.Object>> executeUpdateQuery​(org.springframework.messaging.Message<?> message, boolean keysGenerated)