Spring Batch 中 Scheduler 定时任务

Spring batch 是一个优秀的专业的批处理框架,但是并不是一个任务调度框架。但是Spring 中也带有一个轻量级的Scheduler来帮助我们做一些事情。除此之外我们还可以选择比较有效的任务调度框架Quartz [quartz ] 可以很好的与Spring Batch 进行结合做一些更加优秀的东西。对于刚刚开始我选择用Spring 自带的轻量级的Scheduler来做个简单的demo,后续将会引入quartz来做一些东西。

这里简单配置一个job任务来讲述如何使用
此处spring batch 的reader process writer三个部分的配置在此省略,但是不影响定时任务执行的效果

job_context.xml

<beans    
    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: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/task http://www.springframework.org/schema/task/spring-task.xsd"> 
   <!-- Import our beans -->  
  <import resource="classpath:/applicationContext.xml"/> 
   <batch:job id="job1"> 
       <batch:step id="importFileStep">    
        <tasklet>           
              <chunk reader="productReader" writer="productWriter" commit-interval="5"/>    
        </tasklet>   
     </batch:step>  
  </batch:job>
   <!--执行器-->
    <bean id="runScheduler" class="com.sushou.demo.BatchJob" />   
<!--定时任务  每5秒执行一次-->               
   <task:scheduled-tasks> 
       <task:scheduled ref="runScheduler" method="run" cron="*/5 * * * * *" />  
  </task:scheduled-tasks>
</beans>

运行定时任务

BatchJob.java

package com.sushou.demo;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
/** * Created by maple on 16/5/14. */
public class BatchJob {    
  @Autowired   
  private JobLauncher jobLauncher;  
  @Autowired
  private Job job;  

  public void run() {       
         try {          
              String dateParam = new Date().toString();       
              JobParameters param =    new JobParametersBuilder().addString("date", dateParam).toJobParameters();  
              System.out.println(dateParam);  
              JobExecution execution = jobLauncher.run(job, param);             //执行job      
              System.out.println("Exit Status : " + execution.getStatus());    

          } catch (Exception e) {   
                  e.printStackTrace();   
          } 
     }
}

增加一个运行的主方法

App.java

package com.sushou.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** * Created by maple on 16/5/14. */
public class App {   
    public static void main(String[] args) {  
          ApplicationContext context = new ClassPathXmlApplicationContext("job-context.xml"); 
   }
}

运行app.java 就可以看到运行结果

定时任务运行结果 2016-05-14 下午2.03.14.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容