网上见到很少的spring integration 的实际使用教程,碰巧最近项目中遇到一个需求,非常适合使用spring integration 进行开发,于是参考spring integration 的Refrence,自己摸索了下算初步完成,现在已经在运行中。在此发布出来,一是为了记录自己的工作成果;二是为了抛块砖头引下玉,希望大家可以多指出改进的地方。一起学习和完善spring integration 的使用。
一、首先说下项目的大概需求:我们自己的系统A(以下用SYSA代替),外不系统(SYSB),SYSA用的是Java、Linux和Oracle环境,SYSB用的是.NET、Windows和SqlServer环境,现在的需求是SYSA中有一部数据要同步给SYSB,为方便叙述以下把要同步的数据看成是一个表,用TBL_SYSCHN,表示。当在SYSA中对TBL_SYNSC进增、删、改的时候要能(准实时)同步到SYSB中。两方商定的主要规则如下:
1、SYSA要能将变化的数据抽取出来并以文件的形式放入共用FTP服务器;
2、SYSA在将同步的文件放入FTP后,通过TCP/IP协议通知SYSB,告诉SYSB可以取新的同步数据了。
3、同步文件格式是JSON格式的;介绍表结构时候同时介绍协议的JSON格式。
4、TCP/IP消息双方共同制定的可识别格式;
(说实话,不知道谁谈定的这种开发规则,简直是蛋疼!!没办法,定了就要开发哇。)
大概需求就是如此,其实,实际项目中要同步的数据不止一张表。要建立一个多张表共用的同步数据表TBL_SYSCHN_DATA来进行同步;所有要同步的数据通过关联TBL_SYSCHN_DATA来抽取。为叙述方便现在只针对一张表同步来叙述。表结构大致如下:
其中TBL_SYSCHN是要同步的数据;TBL_SYSCHN_DATA是记录TBL_SYSCHN中某条数据同步信息的;比如,向TBL_SYSCHN中插入了一条数据,插入的数据的SYSCHN_ID为1;则TBL_SYSCHN_DATA会同时记录一条相应的数据,内容如下:
SYSCHN_DATA_ID : 自增一个
SYSCHN_TBL_ID :1 --插入的TBL_SYSCHN的数据的ID
SYSCHN_TBL_NAME:‘TBL_SYSCHN’
SYSCHN_FLAG:‘0’ --表示未同步。
SYSCHN_DATE:null
SYSCHN_OP:1 ---表示插入一条数据。
同步的时候关联着两张表就可将要同步的数据抽取出来。JSON的格式规定为
{"syschnDataId ":'',"syschnTblId":'',"syschnOp":'',"syschnTblName":'',"entryData":{"SYSCHN_TBL_ID ",'',"COL_A":'',"COL_B":'',"COL_C":''}}
而且这些数据写在一行上,中间不能分行,每行以换行符结尾。一个同步文件为N行这样的数据。
三、TCP/IP,消息的格式为
客户端发送格式: FLAG_GB|FTP文件名|文件大小|发送标志|FLAG_ED;
服务端返回格式: FLAG_GB|接受标志|处理标识|FLAG_ED;
以上就是大体需求,下面按照以上需求用Java spring integration进行实现。
四、开发环境搭建
不少刚接触spring integration的人都刚到对 spring integration无从下手,我在网上也搜索了些资料没有一个详细的关于spring integration的搭建教程,一开始我很不解,直到完成这个项目后才发现是自己有些太无知了,呵呵。其实spring integration的开发环境和Spring的开发环境是一样的,只是要在项目中多加入一些关于spring integration的Jar包而已。下面就说下我的环境
1、JDK1.6或以上;
2、Eclipse或者Spring Tool Suite ,我用的后者;
3、Spring3.2.6;
4、Spring Integration 3.0.0;
5、数据库Oracle;
6、ftp服务器Serv-U;
首先新建工程,我用的Maven;所以新建Maven工程;
选择Maven Project->Nnext;
如上图勾选->Next;
根据你的情况填写以上->Finish;
建好后的项目结构,下面添加pom.xml配置,让Maven自动添加项目所需Jar包;
4.0.0 com.joinandjoin.javaeye.sip siptest 0.0.1-SNAPSHOT siptest siptest UTF-8 3.0.0.RELEASE 3.2.6.RELEASE 2.2.3 1.9.13 4.11 1.2.17 org.apache.maven.plugins maven-compiler-plugin 3.1 1.6 1.6 -Xlint:all true true org.mortbay.jetty jetty-maven-plugin 8.1.11.v20130520 org.apache.tomcat.maven tomcat7-maven-plugin 2.1 / junit junit ${junit.version} test log4j log4j ${log4j.version} org.springframework spring-context-support ${spring.version} org.springframework spring-orm ${spring.version} org.springframework.integration spring-integration-core ${spring.integration.version} org.springframework.integration spring-integration-jdbc ${spring.integration.version} org.springframework.integration spring-integration-ftp ${spring.integration.version} org.springframework.integration spring-integration-ip ${spring.integration.version} com.fasterxml.jackson.module jackson-module-jaxb-annotations ${jackson.version} org.codehaus.jackson jackson-mapper-asl ${jackson.asl.version} org.slf4j slf4j-log4j12 1.6.6 org.slf4j jcl-over-slf4j 1.6.6 org.slf4j jul-to-slf4j 1.6.6 org.lazyluke log4jdbc-remix 0.2.7 com.jolbox bonecp 0.8.0.RELEASE com.oracle ojdbc14 10.2.0.4.0 dom4j dom4j 1.6.1 org.apache.ftpserver ftpserver-core 1.0.6 javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.1 provided javax.servlet jstl 1.2
保存pom.xml后,可以看到Maven已经为我们添加了所需的Jar包;
但是项目提示错误,
Description Resource Path Location Type
Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix. siptest line 1 Maven Configuration Problem
提示我们用Maven更新项目,照它说的做
按图勾选其中项目选择你所要更新的项目,OK后,项目错误消除
往/src/main/resources路径下添加如下目录结构
同时添加两个文件,app.properties和log4j.xml
最后目录结构如下
其中,spring-integration-context.xml文件初始内容如下,随着项目的进行会逐步完善。
spring-integration-context.xml文件要确保添加spring-integration、spring-integration-jdbc、spring-integration-ftp
和spring-integration-ip这几命名空间的配置,后面会用到。
app.properties是配置文件以后会添加配置内容如数据库连接和连接池配置参数等;
log4j.xml是日志的配置文件,根据你自己的情况自行配置即可;
到此,环境基本搭建完成。
至于Serv-U的配置,大家自己搜索教程操作,这里不再筹述。
数据库建表语句:
-- Create table
create table TBL_SYSCHN_DATA
(
SYSCHN_DATA_ID NUMBER(14) not null,
SYSCHN_TBL_ID NUMBER(14) not null,
SYSCHN_TBL_NAME VARCHAR2(255) not null,
SYSCHN_FLAG NUMBER(2) default 0 not null,
SYSCHN_DATE DATE,
SYSCHN_OP NUMBER(2) default 1 not null
);
-- Create/Recreate primary, unique and foreign key constraints
alter table TBL_SYSCHN_DATA
add constraint PK_SYSCHN_DATA primary key (SYSCHN_DATA_ID);
-- Create table
create table TBL_SYSCHN
(
SYSCHN_ID NUMBER(14) not null,
COL_A VARCHAR2(20) not null,
COL_B VARCHAR2(20) not null,
COL_C VARCHAR2(20)
);
-- Create/Recreate primary, unique and foreign key constraints
alter table TBL_SYSCHN
add constraint PK_SYSCHN_TBL primary key (SYSCHN_ID);
-- Create sequence
create sequence TBL_SYSCHN_DATA_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
-- Create sequence
create sequence TBL_SYSCHN_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
以上开发环境基本搭建完成。
未完待续。。。。。。。。。。。。。。。。。