1.通过JDK自带的类实现,即:java.util.Timer结合java.util.TimerTask,使用这种方式可以让你的程序按照某一个频度执行,但调度控制非常不方便,需要大量代码,不推荐
2.使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,推荐
3.Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。
二.实现代码
第一种,java自带的
定时任务执行类
import java.util.TimerTask; public class TestTask extends TimerTask{ @Override public void run() { System.out.println("定时任务测试"); } }
调用定时任务类
import java.util.Date; import java.util.Timer; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.springframework.web.context.ContextLoaderListener; public class TimerContext extends ContextLoaderListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TestTask(), new Date(), 2000); } }
如果spring框架,可以继承ContextLoaderListener类,在实现ServletContextListener,然后把web.xml文件配置ContextLoaderListener改为:
<listener> <listener-class>com.hebradio.wechat.task.TimerContext</listener-class> </listener>
如果直接用servlet,那就直接实现ServletContextListener,再在web.xml文件配置上面的监听器
第二种,使用Quartz
第一步:.配置job,有两种方式
(1).继承org.springframework.scheduling.quartz.QuartzJobBean
package com.test.task; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class TestTask extends QuartzJobBean{ @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { System.out.println("定时任务测试"); } }
配置文件中配置作业类
spring4.2没有JobDetailBean,改为:JobDetailFactoryBean
<bean id="jobDetailFactory" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.test.task.TestTask" /> <!-- <property name="jobDataMap"> <map> <entry key="timeout" value="5" /> </map> </property> --> </bean>
(2).使用MethodInvokingJobDetailFactoryBean,此时类不用继承任何接口或者类,方法名称可以随便定义
public class TestTask{ public void test(){ System.out.println("定时任务测试"); } }
<bean id="jobDetailFactory" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 这个就是具体实现类,如果是注解,则必须为component指定value --> <property name="targetObject"> <bean class="com.test.task.TestTask"/> </property> <!-- targetMethod是Spring定时器的特殊属性 --> <property name="targetMethod" value="test" /> <!-- false表示job不会并发执行,默认为true --> <property name="concurrent" value="false" /> </bean>
第二步:配置调度任务执行的触发的时间,两种方式
(1) 使用SimpleTriggerFactoryBean
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="jobDetailFactory" /> <!-- 第一次执行时间多少毫秒之后 --> <property name="startDelay" value="1000" /> <!-- 每过多少毫秒执行一次定时任务 --> <property name="repeatInterval" value="5000" /> </bean>
(2) 使用CronTriggerFactoryBean
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetailFactory" /> <!-- 每天早上六点执行一次 <property name="cronExpression" value="0 0 6 * * ?" /> --> <!--每分执行一次 <property name="cronExpression" value="0 0/1 * * * ?" /> --> <!--每5秒执行一次--> <property name="cronExpression" value="0/5 * * * * ?" /> </bean>
第三步:配置Quartz的调度工厂,调度工厂只能有一个,多个调度任务在list中添加
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- <ref bean="cronTrigger" /> --> <ref bean="simpleTrigger" /> </list> </property> </bean>
第三种,spring自带的
这种方式在一台电脑上配置成功,但是将代码拷贝到另外两台电脑后,定时任务不执行了,没有找到原因,后来改为上面第二种,如果有知晓的朋友,请告知一下,谢谢
在spring配置文件头引入下面两行
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="... http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd"
定时任务配置
<!-- 定时任务执行 driven-->
<task:annotation-driven/>
<context:annotation-config/>
<!-- 定时任务所在包 -->
<context:component-scan base-package="com.test.task"/>
package com.test.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component("testTask") public class TestTask{ @Scheduled(cron="0/5 * * * * ?")//每5秒钟执行一次 public void testCron(){ System.out.println("定时任务测试"); } @Scheduled(fixedRate=5000, initialDelay=1000)//第一次服务器启动一秒后执行,以后每过5秒执行一次,具体情况请查看Scheduled的方法 public void test() { System.out.println("定时任务测试"); } }
记得在类上面加@Component,在方法上加@Scheduled
Scheduled的表达式有两种,即代码中,一种即可,用这种方式,定时任务与定时任务之间是顺序执行的,并没有为每一个定时任务分一个线程