从头认识SpringBatch批处理框架---JobRepository数据库存储元数据并。。。
JobRepository存储执⾏期的元数据,提供两种默认实现,⼀种是存放在内存中,默认实现类为:MapJobRepositoryFactoryBean。在xml中的配置如下:
<bean id="jobRepository"class="org.epository.support.MapJobRepositoryFactoryBean"></bean>
另⼀种是存⼊数据库中,可以随时监控批处理Job的执⾏状态,查看Job执⾏结果是成功还是失败,并且使得在Job失败的情况下重新启动Job成为可能。具体配置如下:
<?xml version="1.0" encoding="UTF-8"?><bean:beans xmlns="www.springframework/schema/batch"xmlns:bean="www.springframe work/schema/beans"xmlns:xsi="www.w3/2001/XMLSchema-instance"xmlns:p="www.springframework/schema/p"xmln s:tx="www.springframework/schema/tx"xmlns:aop="www.springframework/schema/aop"xmlns:context="www.springfra mework/schema/context"xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/bean s/spring-beans-3.0.xsd www.springframework.o
rg/schema/tx www.springframework/schema/tx/spring-tx-3.0.xsd www.sprin gframework/schema/aop www.springframework/schema/aop/spring-aop-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-2.5.xsd www.springframework/schema/batch www.springframework. org/schema/batch/spring-batch-2.2.xsd"><!-- 作业仓库 --><job-repository id="jobRepository"data-source="dataSource"transaction-manager="trans actionManager"isolation-level-for-create="SERIALIZABLE"table-prefix="BATCH_"max-varchar-length="1000" /><!-- 作业调度器 --><bean:bean id=" jobLauncher"class="org.launch.support.SimpleJobLauncher"><bean:property name="jobRepository"ref="jobRepository"/ ></bean:bean><!-- 事务管理器 --><bean:bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager "><bean:property name="dataSource"ref="dataSource" /></bean:bean><!-- 数据源 --><bean:bean id="dataSource"class="org.springframework.jd bc.datasource.DriverManagerDataSource"><bean:property name="driverClassName"><bean:value&sql.jdbc.Driver</bean:value></bea n:property><bean:property name="url"><bean:value>jdbc:mysql://localhost:3306/mydataba</bean:
value></bean:property><bean:property name="urname"value="root"></bean:property><bean:property name="password"value="wbw123456"></bean:property></bean:bean></bean:be ans>
注意:在使⽤数据库的仓库时,需要初始化数据库表,数据库表脚本位置放在org\springframework\batch\spring-batch-
core\3.0.6.RELEASE\spring-batch-core-3.0.6.RELEASE.jar⽬录下,并且针对不同的数据库提供了不同的数据库脚本
在这⾥我们选⽤schema-mysql.sql 具体的建表语句如下:
-- Autogenerated: do not edit this fileCREATE TABLE BATCH_JOB_INSTANCE( JOB_INSTANCE_ID BIGINT NOT NULL PRIMARY KEY , VERSION BI GINT , JOB_NAME VARCHAR(100) NOT NULL, JOB_KEY VARCHAR(32) NOT NULL, constraint JOB_INST_UN unique(JOB_NAME, JOB_KEY)) ENGI NE=InnoDB;CREATE TABLE BATCH_JOB_EXECUTION( JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY , VERSION BIGINT , JOB_INSTA NCE_ID BIGINT NOT NULL, CREATE_TIME DATETIME NOT NULL, START_TIME DATETIME DEFAULT NULL , END_TIME DATETIME DEFAULT NU LL , STATUS VARCHAR(10) , EXIT_CODE VARCHAR(2500) , EXIT_MESSAGE VARCHAR(2500) , LAST_UPDATED DATETIME, JOB_CONFIGURATIO N_LOCATION VARCHAR(2500) NULL, constraint JOB_INST_EXEC_FK foreign key(JOB_INSTANCE_ID) references BATCH_JOB_INSTANCE(JOB_IN STANCE_ID)) ENGINE=InnoDB;CREATE TABLE BATCH_JOB_EXECUTION_PARAMS( JOB_EXECUTION_ID BIGINT NOT NULL , TYPE_CD VARCH AR(6) NOT NULL , KEY_NAME VARCHAR(100) NOT NULL , STRING_VAL VARCHAR(250) , DATE_VAL DATETIME DEFAULT NULL , LONG_VAL BIG INT , DOUBLE_VAL DOUBLE PRECISION , IDENTIFYING CHAR(1) NOT NULL , constraint JOB_EXEC_PARAMS_FK foreign key(JOB_EXECUTION_I D) references BATCH_JOB_EXECUTIO
N(JOB_EXECUTION_ID)) ENGINE=InnoDB;CREATE TABLE BATCH_STEP_EXECUTION( STEP_EXECUTION _ID BIGINT NOT NULL PRIMARY KEY , VERSION BIGINT NOT NULL, STEP_NAME VARCHAR(100) NOT NULL, JOB_EXECUTION_ID BIGINT NOT N ULL, START_TIME DATETIME NOT NULL , END_TIME DATETIME DEFAULT NULL , STATUS VARCHAR(10) , COMMIT_COUNT BIGINT , READ_CO UNT BIGINT , FILTER_COUNT BIGINT , WRITE_COUNT BIGINT , READ_SKIP_COUNT BIGINT , WRITE_SKIP_COUNT BIGINT , PROCESS_SKIP_C OUNT BIGINT , ROLLBACK_COUNT BIGINT , EXIT_CODE VARCHAR(2500) , EXIT_MESSAGE VARCHAR(2500) , LAST_UPDATED DATETIME, const raint JOB_EXEC_STEP_FK foreign key(JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)) ENGINE=InnoDB;CREAT E TABLE BATCH_STEP_EXECUTION_CONTEXT( STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY, SHORT_CONTEXT VARCHAR(2500) N OT NULL, SERIALIZED_CONTEXT TEXT , constraint STEP_EXEC_CTX_FK foreign key(STEP_EXECUTION_ID) references BATCH_STEP_EXECUTI ON(STEP_EXECUTION_ID)) ENGINE=InnoDB;CREATE TABLE BATCH_JOB_EXECUTION_CONTEXT( JOB_EXECUTION_ID BIGINT NOT NULL PRI MARY KEY, SHORT_CONTEXT VARCHAR(2500) NOT NULL, SERIALIZED_CONTEXT TEXT , constraint JOB_EXEC_CTX_FK foreign key(JOB_EXEC UTION_ID) references BATCH_JOB_EXECUTION(J
OB_EXECUTION_ID)) ENGINE=InnoDB;CREATE TABLE BATCH_STEP_EXECUTION_SEQ( ID BI GINT NOT NULL, UNIQUE_KEY CHAR(1) NOT NULL, constraint UNIQUE_KEY_UN unique(UNIQUE_KEY)) ENGINE=InnoDB;INSERT INTO BATCH_S TEP_EXECUTION_SEQ(ID, UNIQUE_KEY) lect * from(lect 0 as ID, '0' as UNIQUE_KEY) as tmp where not exists(lect * from BATCH_STEP_EXE CUTION_SEQ);CREATE TABLE BATCH_JOB_EXECUTION_SEQ( ID BIGINT NOT NULL, UNIQUE_KEY CHAR(1) NOT NULL, constraint UNIQUE_KEY _UN unique(UNIQUE_KEY)) ENGINE=InnoDB;INSERT INTO BATCH_JOB_EXECUTION_SEQ(ID, UNIQUE_KEY) lect * from(lect 0 as ID, '0' as U NIQUE_KEY) as tmp where not exists(lect * from BATCH_JOB_EXECUTION_SEQ);CREATE TABLE BATCH_JOB_SEQ( ID BIGINT NOT NULL, UNI QUE_KEY CHAR(1) NOT NULL, constraint UNIQUE_KEY_UN unique(UNIQUE_KEY)) ENGINE=InnoDB;INSERT INTO BATCH_JOB_SEQ(ID, UNIQUE _KEY) lect * from(lect 0 as ID, '0' as UNIQUE_KEY) as tmp where not exists(lect * from BATCH_JOB_SEQ);
SpringBatch框架进⾏元数据的管理共有九张表:
下⾯分别对9张表的数据结构讲解哈:
BATCH_JOB_INSTANCE(作业实例表,存放JOB的实例信息)
字段名称字段 类型字段说明
JOB_INSTANCE_ID bigint 主键 ,作业实例编号,根据BATCH_JOB_SEQ⾃动⽣成
VERSION bigint 版本号
JOB_NAME varchar作业名称 即在配置⽂件定义的JobId字段内容
JOB_KEY varchar作业标识,根据作业参数序列化⽣成的标识,需要注意通过JOB_NAME+JOB_KEY能够唯⼀区分⼀个作业实例
注意:JOB_KEY------如果同⼀个Job,则JOB_KEY⼀定不能相同,籍作业参数不能相同,如果不是同⼀个Job,JOB_KEY可以相同,也就是作业参数可以相同
BATCH_JOB_EXECUTION_PARAMS(作业参数表,⽤于存放每个JOB执⾏的时候的参数信息,即对于的JOB实例)
字段名称字段类型字段说明
1982年今年虚岁多大
JOB_EXECUTION_ID bigint外键ID,作业执⾏器ID编号,⼀个作业实例可能⼜会多⾏参数记录
TYPE_CD varchar参数的类型,总共有四种 String、Date、Double、Long
KEY_NAME varchar参数的名字
STRING_VAL varchar参数如果是String,就存放String类型的参数值
DATE_VAL datetime参数如果是Date,就存放Date类型的参数值
LONG_VAL bigint参数如果是Long,就存放Long类型的参数值
DOUBLE_VAL double参数如果是Double,就存放Double类型的参数值
IDENTIFYING char ⽤于标识作业参数是否标识作业实例
------------------------------------------------------------------------------------------------------------------------------------------------------------------
BATCH_JOB_EXECUTION(作业执⾏器表,存放当前作业的执⾏信息,创建时间、执⾏时间、执⾏结束时间、执⾏状态、执⾏Job实例等信息)
碗用英语怎么说字段名称字段类型字段说明
JOB_EXECUTION_ID bigint主键
VERSION bigint版本号
JOB_INSTANCE_ID bigint作业实例ID
CREATE_TIME datetime作业执⾏器创建时间
START_TIME datetime作业执⾏器开始执⾏时间
END_TIME datetime作业执⾏器结束时间
泸沽湖旅游攻略自由行STATUS varchar作业执⾏器执⾏的状态:COMPLETED、FAILED、STARTING、UNKNOW等
EXIT_CODE varchar作业执⾏器退出代码 如:UNKNOW、EXECUTING、COMPLETED等
EXIT_MESSAGE varchar作业执⾏器退出信息,通常存放异常信息
LAST_UPDATED datetime本条记录上次更新时间
JOB_CONFIGURATION_LOCATION varchar作业配置⽂件的位置
运⽤BATCH_JOB_INSTANCE和BATCH_JOB_EXECUTION两张表联合查询Job信息
SELECT bi.JOB_NAME,bi.JOB_KEY,be.CREATE_TIME,be.START_TIME,be.END_TIME,be.`STATUS`FROM batch_job_instance biLEFT JOIN batch_jo b_execution be ON bi.JOB_INSTANCE_ID = be.JOB_INSTANCE_IDwhere bi.JOB_NAME='billJob'
BATCH_JOB_EXECUTION_CONTENXT(作业执⾏上下⽂表,存放上下⽂信息)
字段名称字段类型字段说明
JOB_EXECUTION_ID bigint外键ID,作业执⾏器编号
SHORT_CONTEXT varchar作业执⾏器上下⽂字符串格式
SERIALIZED_CONTEXT text序列化的作业执⾏上下⽂
BATCH_JOB_EXECUTION_SEQ表(⽤于BATCH_JOB_EXECUTION和BATCH_JOB_EXECUTION_CONTEXT提供主键⽣成)BATCH_JOB_SEQ表(⽤于BATCH_JOB_INSTNCE和BATCH_JOB_EXECUTION_PARAMS提供主键⽣成)
BATCH_STEP_EXECUTION(作业步执⾏器,存放每个Step执⾏器的信息:开始时间、执⾏完成时间、读/写次数等)字段名称字段类型字段说明
STEP_EXECUTION_ID bigint 主键
VERSION bigint版本万岁高三二
STEP_NAME varchar作业步的名称
JOB_EXECUTION_ID bigint作业执⾏器ID
START_TIME datetime作业步执⾏器执⾏开始时间
END_TIME datetime作业步执⾏器执⾏结束时间
连篇累牍的意思STATUS varchar 作业步执⾏器执⾏状态
COMPLETED, STARTING, STARTED, STOPPING, STOPPED, FAILED, ABANDONED, UNKNOWN 存在于org.BatchStatus
COMMIT_COUNT bigint事务提交次数
保定钟楼READ_COUNT bigint读数据次数
FILTER_COUNT bigint过滤掉的数据次数
WRITE_COUNT bigint写数据次数
READ_SKIP_COUNT bigint读数据跳过的次数
WRITE_SKIP_COUNT bigint写数据跳过的次数
PROCESS_SKIP_COUNT bigint处理数据跳过的次数
魔兽单机秘籍
ROLLBACK_COUNT bigint事务回滚的次数
EXIT_CODE varchar作业步执⾏器退出编码,状态码存在于org.ExitStauts
在秋天的英文EXIT_MESSAGE varchar作业步执⾏器退出描述,⼀般是异常信息
LAST_UPDATED datetime本条记录上次更新时间
BATCH_STEP_EXECUTION_CONTEXT(作业步执⾏器上下⽂,存放作业步执⾏的上下⽂信息)
字段名称字段类型字段说明
STEP_EXECUTION_ID bigint作业步执⾏器ID
SHORT_CONTEXT varchar作业步执⾏器上下⽂字符串格式
SERIALIZED_CONTEXT text序列化作业步执⾏器上下⽂
BATCH_STEP_EXECUTION_SEQ(⽤于BATCH_STEP_EXECUTION和BATCH_STEP_EXECUTION_CONTEXT提供主键)