网上见到很少的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包;
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.joinandjoin.javaeye.sip</groupId>
<artifactId>siptest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>siptest</name>
<description>siptest</description>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<spring.integration.version>
3.0.0.RELEASE
</spring.integration.version>
<spring.version>3.2.6.RELEASE</spring.version>
<jackson.version>2.2.3</jackson.version>
<jackson.asl.version>1.9.13</jackson.asl.version>
<junit.version>4.11</junit.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>
-Xlint:all
</compilerArgument>
<showWarnings>
true
</showWarnings>
<showDeprecation>
true
</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>
org.mortbay.jetty
</groupId>
<artifactId>
jetty-maven-plugin
</artifactId>
<version>8.1.11.v20130520</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>
tomcat7-maven-plugin
</artifactId>
<version>2.1</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ftp</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ip</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>
jackson-module-jaxb-annotations
</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.asl.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.lazyluke</groupId>
<artifactId>log4jdbc-remix</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
保存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文件初始内容如下,随着项目的进行会逐步完善。
<?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-ftp="http://www.springframework.org/schema/integration/ftp"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jdbc
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-3.0.xsd
http://www.springframework.org/schema/integration/ftp
http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-3.0.xsd
http://www.springframework.org/schema/integration/ip
http://www.springframework.org/schema/integration/ip/spring-integration-ip-3.0.xsd">
<context:property-placeholder location="classpath:app.properties"/>
</beans>
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;
以上开发环境基本搭建完成。
未完待续。。。。。。。。。。。。。。。。。