第一篇讲到普通job 配置 那么spring batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等。
下面看一个动态设置读取文件的配置
1.动态文件读取
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<bean:import resource="classpath:ch04/job-context.xml"/> <!-- 账单作业 -->
<job id="billJob">
<step id="billStep">
<tasklet transaction-manager="transactionManager">
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval="">
</chunk>
</tasklet>
</step>
</job> <!-- 读取信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<bean:property name="resource"
value="#{jobParameters['inputResource']}"/> 获取动态参数文件
<bean:property name="lineMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<bean:property name="lineTokenizer" ref="lineTokenizer"/>
<bean:property name="fieldSetMapper">
<bean:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="creditBill">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <!-- lineTokenizer -->
<bean:bean id="lineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="delimiter" value=","/>
<bean:property name="names">
<bean:list>
<bean:value>accountID</bean:value>
<bean:value>name</bean:value>
<bean:value>amount</bean:value>
<bean:value>date</bean:value>
<bean:value>address</bean:value>
</bean:list>
</bean:property>
</bean:bean> <!-- 写信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter"
scope="step">
<bean:property name="resource" value="file:target/ch04/outputFile.csv"/>
<bean:property name="lineAggregator">
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<bean:property name="delimiter" value=","></bean:property>
<bean:property name="fieldExtractor">
<bean:bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<bean:property name="names"
value="accountID,name,amount,date,address">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <bean:bean id="creditBill" scope="prototype"
class="com.juxtapose.example.ch04.CreditBill">
</bean:bean>
<bean:bean id="creditBillProcessor" scope="step" 给定生命周期 只在当前步骤
class="com.juxtapose.example.ch04.CreditBillProcessor">
</bean:bean>
</bean:beans>
下面看测试类。 省略基础配置和实体。
import java.util.Date; import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class JobLaunchStepScope { /**
* 执行批处理作业.<br>
* @param jobPath 作业配置文件
* @param jobName 作业名
* @param builder 作业参数构造器
*/
public static void executeJob(String jobPath, String jobName, JobParametersBuilder builder) {
ApplicationContext context = new ClassPathXmlApplicationContext(jobPath);
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean(jobName);
try {
JobExecution result = launcher.run(job, builder.toJobParameters());
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
} /**
* @param args
*/
// adddString 添加一个参数 在配置文件读取
public static void main(String[] args) {
executeJob("ch04/job/job-stepscope.xml", "billJob",
new JobParametersBuilder().addDate("date", new Date())
.addString("inputResource", "classpath:ch04/data/credit-card-bill-201303.csv"));
}
}
2.继承父类,包括监听,自定义自己监听。包括方便结合定时
<!-- 异步作业调度器 -->
<task:executor id="executor" pool-size="1" />
<bean:bean id="jobLauncherAsyn"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<bean:property name="jobRepository" ref="jobRepository"/>
<bean:property name="taskExecutor" ref="executor" />
</bean:bean>
<task:scheduler id="scheduler" pool-size="10" /> 开启定时
<!-- 每一秒钟,执行对象schedulerLauncher的launch方法一次 -->
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="schedulerLauncher" method="launch" fixed-rate="1000" />
</task:scheduled-tasks>
<bean:bean id="schedulerLauncher"
class="com.juxtapose.example.ch04.scheduler.SchedulerLauncher">
<bean:property name="job" ref="helloworldJob" />
<bean:property name="jobLauncher" ref="jobLauncher" />
</bean:bean>
<!-- 参数校验 --> 采用默认校验 传入的时候必须携带date参数。最多传入两个
还有一个类支持一组校验 compositionJobParametersValitor
<bean:bean id="validator" class="org.springframework.batch.core.job.DefaultJobParametersValidator" >
<bean:property name="requiredKeys" >
<bean:set>
<bean:value>date</bean:value>
</bean:set>
</bean:property>
<bean:property name="optionalKeys" >
<bean:set>
<bean:value>name</bean:value>
</bean:set>
</bean:property>
</bean:bean>
//省略头
<!-- 抽象基础Job 与java中类似-->
<job id="baseJob" abstract="true"> //定义父类监听 注意abstract
<listeners>
<listener ref="sysoutListener"></listener>
</listeners>
</job>
<!-- 账单作业 -->
<job id="billJob" parent="baseJob">
<step id="billStep">
<tasklet transaction-manager="transactionManager">
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval="">
</chunk>
</tasklet>
</step>
<listeners merge="true"> //代表可以一起使用 但是如果监听异常 执行会返回失败
<listener ref="sysoutAnnotationListener"></listener>
</listeners>
</job> <!-- 读取信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<bean:property name="resource"
value="classpath:ch04/data/credit-card-bill-201303.csv"/>
<bean:property name="lineMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<bean:property name="lineTokenizer" ref="lineTokenizer"/>
<bean:property name="fieldSetMapper">
<bean:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="creditBill">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <!-- lineTokenizer -->
<bean:bean id="lineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="delimiter" value=","/>
<bean:property name="names">
<bean:list>
<bean:value>accountID</bean:value>
<bean:value>name</bean:value>
<bean:value>amount</bean:value>
<bean:value>date</bean:value>
<bean:value>address</bean:value>
</bean:list>
</bean:property>
</bean:bean> <!-- 写信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter"
scope="step">
<bean:property name="resource" value="file:target/ch04/outputFile.csv"/>
<bean:property name="lineAggregator">
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<bean:property name="delimiter" value=","></bean:property>
<bean:property name="fieldExtractor">
<bean:bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<bean:property name="names"
value="accountID,name,amount,date,address">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <bean:bean id="creditBill" scope="prototype"
class="com.juxtapose.example.ch04.CreditBill">
</bean:bean>
<bean:bean id="creditBillProcessor" scope="step"
class="com.juxtapose.example.ch04.CreditBillProcessor">
</bean:bean>
<bean:bean id="sysoutListener"
class="com.juxtapose.example.ch04.listener.SystemOutJobExecutionListener">
</bean:bean>
<bean:bean id="sysoutAnnotationListener"
class="com.juxtapose.example.ch04.listener.SystemOut">
</bean:bean>
使用的实体
import java.util.Map; import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus; public class HelloWorldTasklet implements Tasklet { /* (non-Javadoc)
* @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)
*/
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
String jobName = chunkContext.getStepContext().getJobName();
System.out.println("Execute job :" + jobName +".");
JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
System.out.println("JobParameters:" + jobParameterToString(jobParameters));
return RepeatStatus.FINISHED;
} /**
* 转换为String类型格式.<br>
* @param jobParameters
* @return
*/
private String jobParameterToString(JobParameters jobParameters){
StringBuffer sb = new StringBuffer();
for(Map.Entry<String, JobParameter> param : jobParameters.getParameters().entrySet()) {
sb.append(String.format(
"%s = %s (%s);",
param.getKey(),param.getValue().getValue(),param.getValue().getType()
));
}
return sb.toString();
} }
接下来看下基础配置中的参数值
回顾下基础参数 包含三个类
jobRepository 作业工厂
datasource 数据源 当选择持久化是需要
transactionManager 事务管理器
jobLauncher job执行者
先从jobrespository说起 摘自springbatch一书
ItemReader 流读取摘自书本
ItemProcessor 对读取数据处理 比如清晰转换校验等
itemwriter
job说明
子元素
我偷懒了 对不起。。。