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

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

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

    paulwong

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

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

    為方便實例:業(yè)務表pp,狀態(tài)表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;


    觸發(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>

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

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

    Feedback

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

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


    主站蜘蛛池模板: 亚洲专区中文字幕| 亚洲AV无码乱码在线观看| 久久久久久夜精品精品免费啦| 亚洲第一视频在线观看免费| 韩国亚洲伊人久久综合影院| 青草久久精品亚洲综合专区| 亚洲av无码无线在线观看| 亚洲av第一网站久章草| 亚洲av无码成人精品区一本二本 | 成人免费无码大片A毛片抽搐色欲| 免费v片在线观看视频网站| 四虎免费影院ww4164h| 五月婷婷综合免费| 插B内射18免费视频| 永久免费看mv网站入口| 四虎1515hm免费国产| 亚洲一区视频在线播放| 中文字幕在线亚洲精品| 亚洲va中文字幕无码久久| 亚洲电影免费观看| 一本色道久久88—综合亚洲精品 | 亚洲男人都懂得羞羞网站| 亚洲精品电影在线| 亚洲人成77777在线观看网| 亚洲av午夜电影在线观看| 青青青视频免费观看| 中文字幕视频在线免费观看| 午夜影院免费观看| 毛片免费视频观看| 免费永久看黄在线观看app| 在线精品亚洲一区二区三区 | 日韩免费观看视频| 亚洲人成无码www久久久| 久热综合在线亚洲精品| 亚洲成人免费网站| 在线观看亚洲视频| a成人毛片免费观看| 日韩国产免费一区二区三区| 国产伦一区二区三区免费| 亚洲日本va中文字幕久久| 亚洲激情电影在线|