使用Springboot+Springbatch+Quartz每隔3秒读取一次文件
4.2.1概述
Spring batch提供了强大的批处理功能,比如ItemReader、ItemProcess、ItemWriter,还有Tasklet,但定时功能不够强大,而Quartz提供了通过JobDetail、Trigger、SchedulerFactory提供了强大的定时器功能,但批处理功能不够强大,鉴于此,Spring对两者做了的整合.
4.2.2 pom.xml
4.0.0
com.mlsama
hello-springbatch
0.0.1-SNAPSHOT
jar
hello-springbatch
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-batch
org.springframework
spring-context-support
org.quartz-scheduler
quartz
2.3.0
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
4.2.3 配置文件(myJobApplication.xml)
xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd">
<batch:job id="jobExample">
<batch:step id="stepExample">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="reader"
processor="process"
writer="write"
commit-interval="3">
batch:chunk>
batch:tasklet>
batch:step>
batch:job>
4.2.4 application.properties
#整合c3p0,要自定义配置类,如:DataSourceConfiguration
spring.datasource.c3p0.driverClass=com.mysql.jdbc.Driver
spring.datasource.c3p0.jdbcUrl=jdbc:mysql://localhost:3306/springbatch
spring.datasource.c3p0.user=root
spring.datasource.c3p0.password=mlsama
spring.datasource.c3p0.maxPoolSize=30
spring.datasource.c3p0.minPoolSize=10
spring.datasource.c3p0.initialPoolSize=10
#关闭项目启动,job自动执行
spring.batch.job.enabled = false
4.2.5 pojo
@Data
public class User {
private String id;
private String name;
private String age;
public User(String id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
4.2.6 行映射器
@Slf4j
public class UserFieldSetMapper implements FieldSetMapper
public User mapFieldSet(FieldSet fieldSet) throws BindException {
log.info("****************helloWorld************************");
return new User(fieldSet.readString("id"),
fieldSet.readString("name"),
fieldSet.readString("age"));
}
}
4.2.7业务处理
public class UserItemProcessor implements ItemProcessor
@Override
public User process(User item) throws Exception {
if (Integer.parseInt(item.getAge()) % 2 == 0) {
return item;
}
return null;
}
}
4.2.8 持久化
@Slf4j
public class UserWriter implements ItemWriter
public void write(List extends User> items) throws Exception {
log.info("****************helloWorld************************");
for(User user : items){
log.info("处理的对象是:{}",user);
}
}
}
4.2.9 Quartz调度器
继承QuartzJobBean,重写executeInternal方法,在方法内启动job即可.
@Slf4j
public class QuartzJobLauncher extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
JobDetail jobDetail = context.getJobDetail();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
Job job = (Job)jobDataMap.get("job");
JobLauncher jobLauncher = (JobLauncher) jobDataMap.get("jobLauncher");
//MapJobRegistry jobRegistry = (MapJobRegistry)jobDataMap.get("jobRegistry");
log.info("job :{}",job);
log.info("jobLauncher :{}",jobLauncher);
//log.info("jobRegistry :{}",jobRegistry);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sf.format(new Date());
try {
log.info("Current Time :{},job:{}",date,job);
jobLauncher.run(
job,new JobParameters());
//new JobParametersBuilder().addString("date", date).toJobParameters());
log.info("**********************job:{}执行完毕****************",job);
} catch (Exception e) {
log.error("发生异常{}",e);
}
}
}
4.2.10 user.txt
id,name,age
1,mlsama,18
2,ss,20
3,dj,260
4,dn,199
5,jy,212
4.2.11项目启动器
@SpringBootApplication //项目启动
//@EnableBatchProcessing //加载所有的job,与quarter整合调度的时候不能有这个
@ImportResource(locations = "classpath:/myJobApplication.xml")
public class ApartApplication {
public static void main(String[] args) {
SpringApplication.run(ApartApplication.class, args);
}
}
运行main方法即可