<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    spring integration同步數據庫數據

    需求為:當客戶已有系統的數據被同步到我方數據庫后,若再有新數據,只同步新數據到我方數據庫。解決:因為客戶的業務表是不能變動的,我方在客戶數據庫中新建一狀態表,記錄哪些數據被更新過。
    當客戶業務表有新數據插入時,用觸發器將新數據id插入到狀態表。

    為方便實例:業務表pp,狀態表status
    結構為:
    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;


    觸發器:
    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>

    轉:http://wiselyman.iteye.com/blog/1150495

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

    Feedback

    # re: spring integration同步數據庫數據 2014-07-24 15:18 石因

    沒有將數據插入到數據庫的例子??!  回復  更多評論   


    主站蜘蛛池模板: 国产成人精品亚洲| 国产婷婷高清在线观看免费| 麻豆安全免费网址入口| 久久精品国产亚洲AV无码娇色| 国产一区在线观看免费| 国产大片线上免费观看| 日韩免费在线观看视频| 一个人免费播放在线视频看片| 亚洲欧美综合精品成人导航| 亚洲视频在线观看网址| 亚洲第一AV网站| 国产亚洲精品拍拍拍拍拍| 国产jizzjizz视频免费看| 女人与禽交视频免费看| 精品久久久久久久久免费影院| 久久国产精品免费视频| 中文字幕无码免费久久| AAAAA级少妇高潮大片免费看| 色www免费视频| 国产精品亚洲а∨天堂2021| 亚洲精华国产精华精华液网站| 亚洲av日韩av综合| 亚洲人成在线播放| 亚洲一级毛片在线观| 亚洲欧洲日产国码www| 亚洲欧洲国产日韩精品| 亚洲男人第一av网站| 亚洲四虎永久在线播放| 亚洲三级电影网站| 亚洲国产一区二区a毛片| 亚洲第一精品福利| 久久亚洲精品无码VA大香大香| 午夜亚洲国产理论秋霞| 亚洲天堂在线播放| 亚洲美女在线观看播放| 亚洲精品综合久久中文字幕| 亚洲国产成人无码av在线播放| 亚洲免费在线观看视频| 国产成人精品亚洲2020| 亚洲av无码有乱码在线观看| 337p日本欧洲亚洲大胆人人|