
第一步:导入相关jar包
(注:单单是在后台执行需要的jar包,若是经过tomcat执行,需额外添加一个jar包——jta-1.1.jar)
不同版本需要依赖的jar:
quartz-all-1.6.0.jar版本需要的jar包:
commons-collections-3.2.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
spring.jar
quartz-1.8.4.jar版本需要的jar包:
commons-collections-3.2.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-1.8.4.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
spring.jar
第二步: 新建立一个业务bean-->cn.yulon.service.MessageService
package cn.yulon.service;
public class MessageService {
int i;
public void printLog(){
i++;
System.out.println("this is my timer:" +i);
}
第三步:在Spring配置文件time-bean.xml,如下
相关介绍:
在xml里配置值得关注的是
0 0 10,14,16 * * 每天上午10点,下午2点和下午4点
0 0,15,30,45 * 1-10 * 每月前10天每隔15分钟
30 0 0 1 1 2012 在2012年1月1日午夜过30秒时
0 0 8-5 * MON-FRI 每个工作日的工作时间
- 区间
* 通配符 你不想设置那个字段
第四步:新建测试类SpringTest
package cn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("time-bean.xml");
}
}
运行结果如下 :
this is my timer:1
this is my timer:2
this is my timer:3
this is my timer:4
this is my timer:5
web.xml的配置:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> org.springframework.web.context.ContextLoaderListener
应用场合: 如做一些定时提醒,定时发送邮件、短信,日志定时备份等应用
