paulwong

          spring integration同步數(shù)據(jù)庫數(shù)據(jù)

          需求為:當(dāng)客戶已有系統(tǒng)的數(shù)據(jù)被同步到我方數(shù)據(jù)庫后,若再有新數(shù)據(jù),只同步新數(shù)據(jù)到我方數(shù)據(jù)庫。解決:因為客戶的業(yè)務(wù)表是不能變動的,我方在客戶數(shù)據(jù)庫中新建一狀態(tài)表,記錄哪些數(shù)據(jù)被更新過。
          當(dāng)客戶業(yè)務(wù)表有新數(shù)據(jù)插入時,用觸發(fā)器將新數(shù)據(jù)id插入到狀態(tài)表。

          為方便實例:業(yè)務(wù)表pp,狀態(tài)表status
          結(jié)構(gòu)為:
          pp:
          CREATE TABLE `pp` (
            `name` 
          varchar(255default NULL,
            `address` 
          varchar(255default NULL,
            `id` 
          int(11NOT NULL auto_increment,
            
          PRIMARY KEY  (`id`)
          ) ENGINE
          =InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

          status:

          CREATE TABLE `status` (
            `id` 
          int(11NOT NULL auto_increment,
            `status` 
          varchar(255default 'new',
            `ppid` 
          int(11NOT NULL,
            
          PRIMARY KEY  (`id`)
          ) ENGINE
          =InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;


          觸發(fā)器:
          DROP TRIGGER if EXISTS mytrigger
          CREATE TRIGGER mytrigger after INSERT on pp
          for EACH ROW
          BEGIN
           
          INSERT into `status`(ppid) values(new.id);
          END;


          核心配置:jdbc-inbound-context.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans" 
                 xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:context
          ="http://www.springframework.org/schema/context" 
                 xmlns:int
          ="http://www.springframework.org/schema/integration" 
                 xmlns:int-jdbc
          ="http://www.springframework.org/schema/integration/jdbc"    
                 xmlns:int-jms
          ="http://www.springframework.org/schema/integration/jms" 
                 xmlns:jdbc
          ="http://www.springframework.org/schema/jdbc" 
                 xsi:schemaLocation
          ="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-3.0.xsd 
              http://www.springframework.org/schema/integration 
              http://www.springframework.org/schema/integration/spring-integration-2.0.xsd 
              http://www.springframework.org/schema/integration/jdbc 
              http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd 
              http://www.springframework.org/schema/jdbc 
              http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
               http://www.springframework.org/schema/integration/jms 
              http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd"
          >
              
          <context:component-scan base-package="com.wisely.inbound"/>
               
              
          <int:channel id="target"/>
              
              
          <int-jdbc:inbound-channel-adapter channel="target" 
                              data-source
          ="dataSource"
                              query
          ="select p.id as ppid,p.name as ppname from pp p,status s where p.id=s.ppid and s.status='new'"
                              update
          ="update status as st set st.status='old' where ppid in (:ppid)"
                                                 
          >
                  
          <!-- 每隔多少毫秒去抓取 -->
                  
          <int:poller fixed-rate="5000" >
                      
          <int:transactional/>
                  
          </int:poller>
                  
          <!--  指定時刻抓取
                  <int:poller max-messages-per-poll="1">
                      <int:transactional/>
                      <int:cron-trigger expression="0 0 3 * * ?"/>
                  </int:poller>
                  
          -->
              
          </int-jdbc:inbound-channel-adapter>
              
          <int:service-activator input-channel="target" ref="jdbcMessageHandler"/>     
               
          <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
               
          <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
                  
          <property name="driverClassName" value="${database.driverClassName}"/>
                  
          <property name="url" value="${database.url}"/>
                  
          <property name="username" value="${database.username}"/>
                  
          <property name="password" value="${database.password}"/>
              
          </bean>   
              
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                  
          <property name="dataSource" ref="dataSource"/>
              
          </bean>    
             
          </beans>


          JdbcMessageHandler:
          package com.wisely.inbound.jdbc;

          import java.util.List;
          import java.util.Map;

          import org.springframework.integration.annotation.ServiceActivator;
          import org.springframework.stereotype.Component;

          @Component
          public class JdbcMessageHandler {
              @ServiceActivator
              
          public void handleJdbcMessage(List<Map<String ,Object>> message){
                  
          for(Map<String,Object> resultMap:message){
                      System.out.println(
          "組:");
                      
          for(String column:resultMap.keySet()){
                          System.out.println(
          "字段:"+column+" 值:"+resultMap.get(column));
                      }

                  }

              }

          }


          測試類:
          package com.wisely.inbound.jdbc;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          public class JdbcInbound {

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                    ApplicationContext context 
          = 
                              
          new ClassPathXmlApplicationContext("/META-INF/spring/jdbc-inbound-context.xml");
              }


          }


          若將channel改為jms的通道。配置文件做以下修改:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans" 
                 xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:context
          ="http://www.springframework.org/schema/context" 
                 xmlns:int
          ="http://www.springframework.org/schema/integration" 
                 xmlns:int-jdbc
          ="http://www.springframework.org/schema/integration/jdbc"    
                 xmlns:int-jms
          ="http://www.springframework.org/schema/integration/jms" 
                 xmlns:jdbc
          ="http://www.springframework.org/schema/jdbc" 
                 xsi:schemaLocation
          ="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-3.0.xsd 
              http://www.springframework.org/schema/integration 
              http://www.springframework.org/schema/integration/spring-integration-2.0.xsd 
              http://www.springframework.org/schema/integration/jdbc 
              http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd 
              http://www.springframework.org/schema/jdbc 
              http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
               http://www.springframework.org/schema/integration/jms 
              http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd"
          >
              
          <context:component-scan base-package="com.wisely.inbound"/>
               
              
          <int-jms:channel id="target"  queue-name="jdbc.queue" connection-factory="connectionFactory"/>
              
              
          <int-jdbc:inbound-channel-adapter channel="target" 
                                                data-source
          ="dataSource"
                                                query
          ="select p.id as ppid,p.name as ppname from pp p,status s where p.id=s.ppid and s.status='new'"
                                                update
          ="update status as st set st.status='old' where ppid in (:ppid)"
                                                 
          >
                  
          <!-- 每隔多少毫秒去抓取 -->
                  
          <int:poller fixed-rate="5000" >
                      
          <int:transactional/>
                  
          </int:poller>
                  
          <!--  指定時刻抓取
                  <int:poller max-messages-per-poll="1">
                      <int:transactional/>
                      <int:cron-trigger expression="0 0 3 * * ?"/>
                  </int:poller>
                  
          -->
              
          </int-jdbc:inbound-channel-adapter>
              
          <!--  
              <int-jms:message-driven-channel-adapter id="queInbound" destination-name="jmsQueue" channel="target"/>
              
          -->
              
          <int:service-activator input-channel="target" ref="jdbcMessageHandler"/>
               
               
          <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
               
          <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
                  
          <property name="driverClassName" value="${database.driverClassName}"/>
                  
          <property name="url" value="${database.url}"/>
                  
          <property name="username" value="${database.username}"/>
                  
          <property name="password" value="${database.password}"/>
              
          </bean>
              
              
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                  
          <property name="dataSource" ref="dataSource"/>
              
          </bean>
              
              
              
          <bean id="activeMqConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
                  
          <property name="brokerURL" value="vm://localhost" />
              
          </bean>
              
              
          <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
                  
          <property name="sessionCacheSize" value="10" />
                  
          <property name="cacheProducers" value="false"/>
                  
          <property name="targetConnectionFactory" ref="activeMqConnectionFactory"/>
              
          </bean>
              
          <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                  
          <property name="connectionFactory" ref="connectionFactory"/>
                  
          <property name="defaultDestinationName" value="jmsQueue" />
              
          </bean>
          </beans>

          轉(zhuǎn):http://wiselyman.iteye.com/blog/1150495

          posted on 2012-10-17 11:50 paulwong 閱讀(2327) 評論(1)  編輯  收藏 所屬分類: SPRING INTERGRATION

          Feedback

          # re: spring integration同步數(shù)據(jù)庫數(shù)據(jù) 2014-07-24 15:18 石因

          沒有將數(shù)據(jù)插入到數(shù)據(jù)庫的例子啊!  回復(fù)  更多評論   


          主站蜘蛛池模板: 吉水县| 界首市| 惠来县| 嘉义县| 博乐市| 黄浦区| 旅游| 铜鼓县| 内乡县| 连州市| 宣威市| 延安市| 稻城县| 桂林市| 东安县| 辽源市| 讷河市| 乳源| 射洪县| 临朐县| 乌兰察布市| 巴林左旗| 固镇县| 湾仔区| 盐城市| 潞西市| 崇礼县| 云梦县| 子长县| 连江县| 深圳市| 丰台区| 黑河市| 宕昌县| 寻甸| 博乐市| 井陉县| 新安县| 莱芜市| 大邑县| 红河县|