springbatch整合Quartz框架

使用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

      

          
        
           
com.mchange
          
c3p0
          
0.9.5.2
      

         
          

            
org.springframework.boot
            
spring-boot-starter-test
            
test
          
        
          
org.springframework.batch
           
spring-batch-test
        
test
        
     

     

        
org.projectlombok
        
lombok
     
     

     

        
org.slf4j
        
log4j-over-slf4j
     
  
  
     
        
           
org.springframework.boot
           
spring-boot-maven-plugin
        
     
  

4.2.3 配置文件(myJobApplication.xml)

xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
      
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"
>

   

   
id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
        name="resource" value="classpath:/data/user.txt">
        name="lineMapper" ref="lineMapper">
        name="linesToSkip" value="10">
   

    id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
        name="lineTokenizer" ref="lineTokenizer">
        name="fieldSetMapper" ref="fieldSetMapper">
   

    id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
        name="names" value="id,name,age">
   

    id="fieldSetMapper" class="com.mlsama.hellospringbatch.config.apart.UserFieldSetMapper">

   

   
id="process" class="com.mlsama.hellospringbatch.config.apart.UserItemProcessor">
   

   
id="write" class="com.mlsama.hellospringbatch.config.apart.UserWriter">

 

   
   
id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
        name="dataSource" ref="dataSource">
        name="transactionManager" ref="transactionManager">
        name="databaseType" value="MySQL">
   


   

   
id="executor" class="org.springframework.core.task.SimpleAsyncTaskExecutor">
   

   
id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        name="jobRepository" ref="jobRepository">
        name="taskExecutor" ref="executor">
   

   

   
id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
   

   
<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>

   

   
   
id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
    class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
        name="jobRegistry" ref="jobRegistry"/>
   

   

   
id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
        name="dataSource" ref="dataSource">
   

   

   
id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
       

       
name="jobDataMap">
           
               

               
key="job" value-ref="jobExample">
               

               
key="jobLauncher" value-ref="jobLauncher">
           
       

       

       
name="jobClass" value="com.mlsama.hellospringbatch.config.apart.QuartzJobLauncher">
   


   

   
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        name="triggers">
            id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
               

               
name="jobDetail" ref="jobDetail" />
               

               
name="cronExpression" value="0/3 * * * * ?" />
           

       

   


 

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 {

   
@Override
   
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, User> {

   
@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 {

   
@Override
   
public void write(Listextends 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方法即可

你可能感兴趣的:(springbatch整合Quartz框架)