- Spring注解实现:Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz。
- maven配置:
<!--quartz-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
- applicationContext.xml配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd ">
<!--启用注解,加载定时任务-->
<task:annotation-driven />
<context:annotation-config />
<!--定时任务扫描的包-->
<context:component-scan base-package="*.*.*"/>
</beans>
- 实现:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TimeTest {
@Scheduled(cron="*/3 * * * * ?")//每10秒跑一次任务
public void tesTime(){
System.out.println("定时器执行成功!---------66666----------");
}
}
- 配置Quartz实现定时器功能:
- applicationContext.xml配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描包 -->
<context:component-scan base-package="com.*.*" />
<!-- 要调用的工作类 -->
<bean id="timeTestBean" class="com.*.*.TimeTest"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="timeTestBean"></property>
<!-- 调用类中的方法 -->
<property name="targetMethod" value="tesTime"></property>
</bean>
<!-- 定义触发时间 -->
<bean id="time" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="job"></property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0/2 * * * * ?</value><!-- 每2秒执行一次 -->
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<!-- lazy-init="false" autowire="no" -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="time" />
</list>
</property>
</bean>
</beans>
示例如下:
0 0 12 * * ?---------------在每天中午12:00触发
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:00至2:59之间每分钟触发一次
0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和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, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发