关于springmvc 和 quartz 我就不介绍了,有许多东西可以搜索到。废话不多说直接上代码:
我用的springMvc版本是4.0.6,quartz版本是2.2.1
1, 导入相关jar,我的项目采用mevan管理,所有直接配置即可
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
2 web.xml 配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<pre name="code" class="html"><servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3 applicationContext.xml配置
<!-- quartz配置 -->
<span style="white-space:pre"> </span><import resource="quartzConfig.xml"/>
<!-- quartz配置结束 -->
4 quartzConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 添加调度的任务bean 配置对应的class-->
<bean id="myPrintSchedule" class="com.framework.app.action.intentApply.RemiseDataAction" />
<!--配置调度具体执行的方法-->
<bean id="myPrintDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myPrintSchedule" />
<property name="targetMethod" value="setTaskTimeDataStatistics" />
<property name="concurrent" value="false" />
</bean>
<!--配置调度执行的触发的时间-->
<bean id="myPrintTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myPrintDetail" />
<property name="cronExpression">
<!-- 每天早上8点执行任务调度 -->
<value>0 0 8 * * ?</value>
<!-- 每个10秒触发
<value>0/10 * * * * ?</value>
-->
</property>
</bean>
<!-- quartz的调度工厂 调度工厂只能有一个,多个调度任务在list中添加 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 所有的调度列表-->
<ref local="myPrintTrigger" />
</list>
</property>
</bean>
</beans>
5 com.framework.app.action.intentApply.RemiseDataAction 为要定时运行的方法类:
public class RemiseDataAction{
public void setTaskTimeDataStatistics(){
System.out.println("输出时间为:"+new Date().toString());
}
}