1. Java自带的java.util.Timer类,这个类容许你调度一个java.util.TimerTask任务。使用这种方式能够让你的程序按照某一个频度执行,但不能在指定时间运行。通常用的较少,这篇文章将不作详细介绍。java
2. 使用Quartz,这是一个功能比较强大的的调度器,能够让你的程序在指定时间执行,也能够按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。spring
3. Spring3.0之后自带的task,能够将它当作一个轻量级的Quartz,并且使用起来比Quartz简单许多,稍后会介绍ide
Quartz
this
继承org.springframework.scheduling.quartz.QuartzJobBean spa
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class Job1 extends QuartzJobBean { private int timeout; private static int i = 0; //调度工厂实例化后,通过timeout时间开始执行调度 public void setTimeout(int timeout) { this.timeout = timeout; } /** * 要调度的具体任务 */ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("定时任务执行中…"); } }
2. 配置JobDetailBean
code
说明:JobDetailBean有两个属性,jobClass属性即咱们在java代码中定义的任务类,jobDataAsMap属性即该任务类中须要注入的属性值component
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.gy.Job1" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="0" /> </map> </property> </bean>
3. 配置调度触发方式
orm
Quartz的做业触发器有两种,分别是xml
org.springframework.scheduling.quartz.SimpleTriggerBean继承
org.springframework.scheduling.quartz.CronTriggerBean
第一种SimpleTriggerBean,只支持按照必定频度调用任务,如每隔30分钟运行一次。
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="job1" /> <property name="startDelay" value="0" /><!-- 调度工厂实例化后,通过0秒开始执行调度 --> <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 --> </bean>
第二种CronTriggerBean,支持到指定时间运行一次,如天天12:00运行一次等
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job1" /> <!—天天12:00运行一次 --> <property name="cronExpression" value="0 0 12 * * ?" /> </bean>
4. 配置调度工厂,说明:该参数指定的就是以前配置的触发器的名字
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean>
Quartz还有一种不用继承QuartzJobBean 实现的方式
Spring-task实现
spring task,能够将它比做一个轻量级的Quartz,并且使用起来很简单,除spring相关的包外不须要额外的包,并且支持注解和配置文件两种
1.编写pojo
mport org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component(“taskJob”) public class TaskJob { @Scheduled(cron = "0 0 3 * * ?") public void job1() { System.out.println(“任务进行中。。。”); } }
2. 添加task相关的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="false"> <context:annotation-config /> <!—spring扫描注解的配置 --> <context:component-scan base-package="com.gy.mytask" /> <!—开启这个配置,spring才能识别@Scheduled注解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>