导读
本文讲述的是spring+quartz环境下的定时任务调度,不涉及动态。动态定时调度将会放在另一篇文章中(未编辑)。
环境:
spring4.x.x+springMVC+mybatis框架+quartz2.2所需Jar包下载地址
1.使用quartz,配置在spring.xml中
定时调度较为简单,只需在配置文件中写好相关的触发器,定时器就好了
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
<!-- 在这里添加不同的定时器 -->
</list>
</property>
</bean>
<!-- 启动触发器的配置结束 -->
定时器
<!-- quartz-2.x的配置 -->
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<!-- 在这里添加调用的类 -->
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<!-- 定时表达式 ,格式在最后-->
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- 调度的配置结束 -->
被调用的类与方法
<!-- job的配置开始 -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<!-- 被调用的类 -->
<ref bean="myJob" />
</property>
<property name="targetMethod">
<!-- 调用类中的方法 -->
<value>work</value>
</property>
<property name="concurrent" value="false"></property>
</bean>
<!-- job的配置结束 -->
<!-- 工作的bean -->
<bean id="myJob" class="com.cwj.demo.MyJob" />
这里会每隔1s执行MyJob下的work方法。
//测试类
public class MyJob {
public void work() {
System.out.println("date: " + new Date().getTime());
}
}
最后在web.xml中加载spring.xml启动服务器就会开始调用work()方法了。
2.使用spring自带的定时调度
spring自带有定时调度的方法,并且可以用注解的方式在配置时间表达式。
spring.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- 自动扫描包 -->
<context:component-scan base-package="com.cwj.task"/>
<task:annotation-driven scheduler="myScheduler"/>
<!-- 配置线程池 -->
<task:scheduler id="myScheduler" pool-size="20"/>
</beans>
线程池也可以不配置,那样的话在多任务时,下一个任务必须等上一个运行结束之后才能运行,不能同步。
注解也很简单
package com.cwj.task;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ATask{
@Scheduled(cron="0/1 * * * * ? ")
public void task() {
try {
TimeUnit.SECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())+"*********A任务每1秒执行一次进入测试");
}
}
@Scheduled(cron=”0/1 * * * * ? “)中直接写入时间表达式就好了。
cronExpression表达式
字段 允许值 允许的特殊字符
秒 0-59 , – * /
分 0-59 , – * /
小时 0-23 , – * /
日期 1-31 , – * ? / L W C
月份 1-12 或者 JAN-DEC , – * /
星期 1-7 或者 SUN-SAT , – * ? / L C #
年(可选) 留空, 1970-2099 , – * /
表达式意义
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
0 6 * * * 每天早上6点
0 */2 * * * 每两个小时
0 23-7/2,8 * * * 晚上11点到早上8点之间每两个小时,早上八点
0 11 4 * 1-3 每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 1 1 * 1月1日早上4点