一、Spring3+Quartz2整合
1.quartz2所需jar包
http://download.csdn.net/detail/bobchao0730/9479097
2.spring-task.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法 -->
<bean id="taskJob" class="com.ky.task.MyTask"></bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="group" value="job_work"/>
<property name="name" value="job_work_name"/>
<!-- false表示等上一个任务执行完成后再开启新的任务 -->
<property name="concurrent" value="false"/>
<property name="targetObject">
<ref bean="taskJob"/>
</property>
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 调度触发器 -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="group" value="work_default"/>
<property name="name" value="work_default_name"/>
<property name="jobDetail">
<ref bean="jobDetail"/>
</property>
<!-- 每天0点执行 -->
<property name="cronExpression">
<value>0 0 0 * * ?</value>
</property>
</bean>
<!-- 调度工厂 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
</beans>
3.web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-ibatis.xml,/WEB-INF/classes/spring-task.xml,/WEB-INF/classes/spring-servlet.xml</param-value>
</context-param>
4.
MyTask
.java
package com.ky.task;
public class MyTask {
public void run() {
System.out.println("每天0点执行!");
}
}
二、Servlet+Quartz2整合
1.所需jar包
2.quartz_jobs.xml
<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>job_work_name</name>
<group>job_work</group>
<job-class>com.ky.task.MyTask</job-class>
</job>
<trigger>
<cron>
<name>work</name>
<job-name>job_work_name</job-name>
<job-group>job_work</job-group>
<cron-expression>0 0 0 * * ?</cron-expression><!--每天0点执行 -->
</cron>
</trigger>
</schedule>
</job-scheduling-data>
3.quartz.properties
#Configure Main Scheduler properties
#=============================================================================
org.quartz.scheduler.instanceName=QuartzScheduler
org.quartz.scheduler.instanceId=AUTO
#=============================================================================
#Configure ThreadPool
#=============================================================================
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=3
org.quartz.threadPool.threadPriority=5
#=============================================================================
#Configure JobStore
#=============================================================================
org.quartz.jobStore.misfireThreshold=6000
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
#=============================================================================
#Configure Plugins
#=============================================================================
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames=quartz_jobs.xml
org.quartz.plugin.jobInitializer.wrapInUserTransaction =false
org.quartz.plugin.jobInitializer.failOnFileNotFound=true
org.quartz.plugin.jobInitializer.scanInterval=60
4.web.xml
<!-- quartz config -->
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>config-file</param-name>
<param-value>/quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
5.MyTask.java
package com.ky.task;
public class MyTask {
public void run() {
System.out.println("每天0点执行!");
}
}