本文档主要对Spring内置的Quartz如何使用进行一个简单的介绍,在阅读文档之前,请首先确认你的项目里面是否引入了Spring框架。
1.首先我们写好需要被调用的方法,做为示例,所以我做了两个方法,一个方法每隔五秒被调用一次,另一个方法每小时的45分00秒被调用一次,两个方法都是在控制台上打印一句话,以表示方法已经被调用,在实际运用过程中,你需要根据实际需求来写你的方法。(在这里,方法名需要与配置文件中的targetMethod一致)
public class QuartzDemo {
public void lunxun(){
System.out.println("这是轮询任务(每5秒调用一次)————————————————————");
}
public void dingshi(){
System.out.println("这是定时任务(每小时的45分调用一次)————————————————————");
}
}
2.现在你需要写一个配置文件,并在applicationContext中引入它(只要能保证这个配置文件在项目启动时能够被Spring所加载就够了,在哪里引入并不重要),我在项目里新建了一个spring-config-quartz.xml做为quartz的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd