spring batch 版本:3.0.7 环境:JDK1.6+
spring Batch 与 quartz 区别:
spring Batch:任务配置调度平台,通过手动调度。基本所有功能是通过Spring xml配置实现,API待调研。配置比较复杂。但实现功能多。
quartz:任务自动调度平台,通过cron表达式控制调度时间。
两种东西的面向对象不同,可结合使用,实现定时批量调度
spring Batch现有支持:
1、多数据库
数据库语句存在spring-batch-core.jar内
2、任务可按定义step顺序执行
<job id = “footballJob” > <step id = “playerload” parent = “s1” next = “gameLoad” /> <step id = “gameLoad” parent = “s2” next = “playerSummarization” /> <step id = “playerSummarization” parent = “s3” /> </ job>
3、任务执行时,可根据当前任务执行状态判断继续执行哪个任务
<job id="job"> <step id="stepA" parent="s1"> <next on="*" to="stepB" /> <next on="FAILED" to="stepC" /> </step> <step id="stepB" parent="s2" next="stepC" /> <step id="stepC" parent="s3" /> </job>
4、自定义事物管理,与spring相同
<job id="sampleJob" job-repository="jobRepository"> <step id="step1"> <tasklet transaction-manager="transactionManager"> <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/> </tasklet> </step> </job>
事物传播机制定义 Spring: 5.1.8 Transaction Attributes
<step id = “step1” > <tasklet> <chunk reader = “itemReader” writer = “itemWriter” commit-interval = “2” /> <transaction-attributes isolation = “DEFAULT” propagation = “REQUIRED” timeout = “30 “ /> </ tasklet> </ step>
5、当任务状态改变时(开始、完成等),可配置执行不同监听
<step id="step1"> <tasklet> <chunk reader="reader" writer="writer" commit-interval="10"/> <listeners> <listener ref="chunkListener"/> </listeners> </tasklet> </step>
监听可配置与父任务合并种类 Spring:4.1.3 Inheriting from a Parent Job
<job id = “baseJob” abstract = “true” > <listeners> <listener ref = “listenerOne” /> <listeners> </ job> <job id = “ job1 ” parent = “baseJob” > <step id = “step1” parent = “standaloneStep” /> <listeners merge = “true” > <listener ref = “listenerTwo” /> <listeners> </ job>
6、任务重试次数和重试时每个step是否运行 Spring:5.1.4 Configuring a Step for Restart
<job id="footballJob" restartable="true"> <step id="playerload" next="gameLoad"> <tasklet> <chunk reader="playerFileItemReader" writer="playerWriter" commit-interval="10" /> </tasklet> </step> <step id="gameLoad" next="playerSummarization"> <tasklet allow-start-if-complete="true"> <chunk reader="gameFileItemReader" writer="gameWriter" commit-interval="10"/> </tasklet> </step> <step id="playerSummarization"> <tasklet start-limit="3"> <chunk reader="playerSummarizationSource" writer="summaryWriter" commit-interval="10"/> </tasklet> </step> </job>
7、自定义异常过滤,可配置发生各种异常时是否回滚和忽视某些异常 Spring :5.1.7 Controlling Rollback
异常不回滚:
<step id="step1"> <tasklet> <chunk reader="itemReader" writer="itemWriter" commit-interval="2"/> <no-rollback-exception-classes> <include class="org.springframework.batch.item.validator.ValidationException"/> </no-rollback-exception-classes> </tasklet> </step>
跳过异常:
<step id = “step1” > <tasklet> <chunk reader = “flatFileItemReader” writer = “itemWriter” commit-interval = “10” skip-limit =“10” > <skippable-exception-classes> <include class = java.lang.Exception“/> <exclude class =”java.io.FileNotFoundException“/> </ skippable-exception-classes> </ chunk> </ tasklet> </ step>
重试异常:
<step id = “step1” > <tasklet> <chunk reader = “itemReader” writer = “itemWriter” commit-interval = “2” retry-limit =“3” > <retryable-exception-classes> <include class = org.springframework.dao.DeadlockLoserDataAccessException“/> </ retryable-exception-classes> </ chunk> </ tasklet> </ step>
8、step并流执行 Spring : 5.3.5 Split Flows
<split id="split1" next="step4"> <flow> <step id="step1" parent="s1" next="step2"/> <step id="step2" parent="s2"/> </flow> <flow> <step id="step3" parent="s3"/> </flow> </split> <step id="step4" parent="s4"/>
引用写法
<job id="job"> <flow id="job1.flow1" parent="flow1" next="step3"/> <step id="step3" parent="s3"/> </job> <flow id="flow1"> <step id="step1" parent="s1" next="step2"/> <step id="step2" parent="s2"/> </flow>
9、内容值动态绑定,只限于bean的属性
<bean id="flatFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="#{jobParameters['input.file.name']}" /> </bean>