第一篇讲到普通job 配置 那么spring batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等。
下面看一个动态设置读取文件的配置
1.动态文件读取
class="org.springframework.batch.item.file.FlatFileItemReader"scope="step">
获取动态参数文件
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
accountID
name
amount
date
address
class="org.springframework.batch.item.file.FlatFileItemWriter"scope="step">
class="com.juxtapose.example.ch04.CreditBill">
class="com.juxtapose.example.ch04.CreditBillProcessor">
下面看测试类。 省略基础配置和实体。
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 {
/**
* 执行批处理作业.
* @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.继承父类,包括监听,自定义自己监听。包括方便结合定时
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
开启定时
class="com.juxtapose.example.ch04.scheduler.SchedulerLauncher">
采用默认校验 传入的时候必须携带date参数。最多传入两个
还有一个类支持一组校验 compositionJobParametersValitor
date
name
//省略头
//定义父类监听 注意abstract
//代表可以一起使用 但是如果监听异常 执行会返回失败
class="org.springframework.batch.item.file.FlatFileItemReader"scope="step">
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
accountID
name
amount
date
address
class="org.springframework.batch.item.file.FlatFileItemWriter"scope="step">
class="com.juxtapose.example.ch04.CreditBill">
class="com.juxtapose.example.ch04.CreditBillProcessor">
class="com.juxtapose.example.ch04.listener.SystemOutJobExecutionListener">
class="com.juxtapose.example.ch04.listener.SystemOut">
使用的实体
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 classHelloWorldTasklet 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)*/
publicRepeatStatus 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));returnRepeatStatus.FINISHED;
}/**
* 转换为String类型格式.
* @param jobParameters
* @return*/
privateString jobParameterToString(JobParameters jobParameters){
StringBuffer sb= newStringBuffer();for(Map.Entryparam : jobParameters.getParameters().entrySet()) {
sb.append(String.format("%s = %s (%s);",
param.getKey(),param.getValue().getValue(),param.getValue().getType()
));
}returnsb.toString();
}
}
接下来看下基础配置中的参数值
回顾下基础参数 包含三个类
jobRepository 作业工厂
datasource 数据源 当选择持久化是需要
transactionManager 事务管理器
jobLauncher job执行者
先从jobrespository说起 摘自springbatch一书
ItemReader 流读取摘自书本
ItemProcessor 对读取数据处理 比如清晰转换校验等
itemwriter
job说明
子元素
我偷懒了 对不起。。。