你的位置:首页 > 软件开发 > Java > Spring定时任务之使用

Spring定时任务之使用

发布时间:2017-06-30 00:00:48
一.分类从实现的技术上来分类,目前主要有三种技术:Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。使用Quartz,这是一个功能比较强大的的调度器, ...

一.分类

  • 从实现的技术上来分类,目前主要有三种技术:

  1. Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
  2. 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。
  3. Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。
  • 从作业类的继承方式来讲,可以分为两类:

  1. 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中需要继承自java.util.TimerTask。
  2. 作业类即普通的java类,不需要继承自任何基类。

注:个人推荐使用第二种方式,因为这样所以的类都是普通类,不需要事先区别对待。

 

  • 从任务调度的触发时机来分,这里主要是针对作业使用的触发器,主要有以下两种:
  1. 每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean
  2. 每到指定时间则触发一次,在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

注:并非每种任务都可以使用这两种触发器,如java.util.TimerTask任务就只能使用第一种。Quartz和spring task都可以支持这两种触发条件。

 

 

二.用法说明

详细介绍每种任务调度工具的使用方式,包括Quartz和spring task两种。

Quartz

第一种,作业类继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean。

第一步:定义作业类

 

Java代码  Spring定时任务之使用
  1. import org.quartz.JobExecutionContext;  
  2. import org.quartz.JobExecutionException;  
  3. import org.springframework.scheduling.quartz.QuartzJobBean;  
  4. public class Job1 extends QuartzJobBean {  
  5.   
  6. private int timeout;  
  7. private static int i = 0;  
  8. //调度工厂实例化后,经过timeout时间开始执行调度  
  9. public void setTimeout(int timeout) {  
  10. this.timeout = timeout;  
  11. }  
  12.   
  13. /** 
  14. * 要调度的具体任务 
  15. */  
  16. @Override  
  17. protected void executeInternal(JobExecutionContext context)  
  18. throws JobExecutionException {  
  19.   System.out.println("定时任务执行中…");  
  20. }  
  21. }  
  1. <bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">  
  2. <property name="jobClass" value="com.gy.Job1" />  
  3. <property name="jobDataAsMap">  
  4. <map>  
  5. <entry key="timeout" value="0" />  
  6. </map>  
  7. </property>  
  8. </bean>  
  1. <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
  2. <property name="jobDetail" ref="job1" />  
  3. <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->  
  4. <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->  
  5. </bean>  
  1. <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  2. <property name="jobDetail" ref="job1" />  
  3. <!—每天12:00运行一次 -->  
  4. <property name="cronExpression" value="0 0 12 * * ?" />  
  5. </bean>  
  1. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  2. <property name="triggers">  
  3. <list>  
  4. <ref bean="cronTrigger" />  
  5. </list>  
  6. </property>  
  7. </bean>  
  1. public class Job2 {  
  2. public void doJob2() {  
  3. System.out.println("不继承QuartzJobBean方式-调度进行中...");  
  4. }  
  5. }  
  1. <bean id="job2"  
  2. class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  3. <property name="targetObject">  
  4. <bean class="com.gy.Job2" />  
  5. </property>  
  6. <property name="targetMethod" value="doJob2" />  
  7. <property name="concurrent" value="false" /><!-- 作业不并发调度 -->  
  8. </bean>  
  1. <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
  2. <property name="jobDetail" ref="job2" />  
  3. <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->  
  4. <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->  
  5. </bean>  
  1. <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  2. <property name="jobDetail" ref="job2" />  
  3. <!—每天12:00运行一次 -->  
  4. <property name="cronExpression" value="0 0 12 * * ?" />  
  5. </bean>  
  1. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  2. <property name="triggers">  
  3. <list>  
  4. <ref bean="cronTrigger" />  
  5. </list>  
  6. </property>  
  7. </bean>  
  1. import org.springframework.stereotype.Service;  
  2. @Service  
  3. public class TaskJob {  
  4.       
  5.     public void job1() {  
  6.         System.out.println(“任务进行中。。。”);  
  7.     }  
  8. }  
  1. <beans "http://www.springframework.org/schema/beans"  
  2.     "http://www.springframework.org/schema/task"   
  3.     。。。。。。  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  1.  <task:scheduled-tasks>   
  2.         <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
  3. </task:scheduled-tasks>  
  4.   
  5. <context:component-scan base-package=" com.gy.mytask " />  
  1. @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. public @interface Scheduled  
  5. {  
  6.   public abstract String cron();  
  7.   
  8.   public abstract long fixedDelay();  
  9.   
  10.   public abstract long fixedRate();  
  11. }  
  1. import org.springframework.scheduling.annotation.Scheduled;    
  2. import org.springframework.stereotype.Component;  
  3.   
  4. @Component(“taskJob”)  
  5. public class TaskJob {  
  6.     @Scheduled(cron = "0 0 3 * * ?")  
  7.     public void job1() {  
  8.         System.out.println(“任务进行中。。。”);  
  9.     }  
  10. }  
  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.w3.org/2001/"http://www.springframework.org/schema/aop"  
  4.     "http://www.springframework.org/schema/context"  
  5.     "http://www.springframework.org/schema/tx"  
  6.     "http://www.springframework.org/schema/task"  
  7.     xsi:schemaLocation="  
  8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  10.         http://www.springframework.org/schema/context   
  11. http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
  12.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
  14.     default-lazy-init="false">  
  15.   
  16.   
  17.     <context:annotation-config />  
  18.     <!—spring扫描注解的配置   -->  
  19.     <context:component-scan base-package="com.gy.mytask" />  
  20.       
  21. <!—开启这个配置,spring才能识别@Scheduled注解   -->  
  22.     <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
  23.     <task:scheduler id="qbScheduler" pool-size="10"/>  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Spring定时任务之使用

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录