项目开发中用到了quartz定时器,感觉很像javaJDK中的Timer类,但是不同的是,spring提供的定时器可以按每天的固定时刻触发,或者其他的时间。触发时间比较人性化。
我自己写了个小的项目,练练手。开发Spring的quartz定时器需要spring-context.jar和quartz.jar包,我最初没有导入quartz.jar包,导致报错。
quartz的maven依赖如下:
[html]
定时器类不需要继承自任何类。
[java]
package com.jd.service.meeting.worker;
public class MyWorker {
public void running(){
System.out.println("hello, world!");
}
}
然后在spring的配置文件中配置这个定时器即可。
[html]
< xml version="1.0" encoding="GBK" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
这个时间表达式指定了这个timer的触发时间规则,我指定的是每10秒跑一次。
[html]
property name="cronExpression">
我的程序本地运行通过,大家配好jar包即可。