Quartz定时机制
首先导入jar包到程序内 quartz-all-1.6.0.jar
然后创建一个XML
TimeConfig.xml 名字可以自己定义
<beans>
<bean id="mainTask" class="net.timed.MainTask"/> //你要执行的任务类
//jar类
<bean id="mainJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="mainTask"/>//将你的类添加到定时器当中
property>
<property name="targetMethod">
<value>executevalue> //定时执行类里面的哪个方法
property>
bean>
<bean id="timeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="mainJob"/>
property>
<property name="cronExpression">
<value>0 0/5 * * * ?value> //定时的语法
property>
bean>
<bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="timeTrigger"/>
list>
property>
bean>
beans>
//下面这个类就是我在XML中引入的类
package net.timed;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainTask {
public void execute() throws IOException
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("do my job"+dateFormat.format(new Date()));
Runtime.getRuntime().exec("cmd /c start E:/mbl/BusinessOffice/MoneDB/bin/bakup.bat");
}
}
然后在web.xml中把这个TimeConfig.xml添加进去作为监听
系统启动的时候自动就监听这个事件
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
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">
<context-param>
<param-name>
contextConfigLocation
param-name>
<param-value>
/WEB-INF/TimerConfig.xml
param-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
这个是quartz spring的定时机制 请仔细的看看 如果可以请给分哦