quartz调度框架在web中应用实例
1. 在web.xml中配置 初始化quartz 相关servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<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>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意:其中配置参数为 /quartz.properties文件
2. 在 quartz.properties 中配置相关参数
如下
org.quartz.scheduler.instanceName = PushDBScheduler
org.quartz.scheduler.instanceId = one
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 4
org.quartz.threadPool.threadPriority = 4
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_job.xml
注意:org.quartz.plugin.jobInitializer.fileName=quartz_job.xml
3. 在quartz_job.xml中配置调度任务相关内容
<?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://www.quartzscheduler.org/ns/quartz"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartzscheduler.org/ns/quartz http://www.quartzscheduler.org/ns/quartz/job_scheduling_data_1_1.xsd"
version="1.1">
<job>
<job-detail>
<name>ScanItemsInDB</name>
<group>Scanning</group>
<job-class>com.artron.base.task.ScanDB</job-class>
<job-data-map allows-transient-data="true">
<entry>
<key>testmode</key>
<value>true</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>t1</name>
<group> Scanning </group>
<job-name>ScanItemsInDB</job-name>
<job-group>Scanning</job-group>
<cron-expression>0/3 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
<job>
<job-detail>
<name>Mail</name>
<group>MailScanning</group>
<job-class>com.artron.base.task.MailJob</job-class>
<job-data-map allows-transient-data="true">
<entry>
<key>ttt</key>
<value>true</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>Mailt1</name>
<group>MailScanning</group>
<job-name>Mail</job-name>
<job-group>MailScanning</job-group>
<cron-expression>0/3 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>
可以配置多个任务类。重复job节点即可
4. 编写 需要调度的任务类:
这里配置了两个类:
package com.artron.base.task;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class ScanDB implements Job {
public ScanDB() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("time:" + System.currentTimeMillis());
}
}
package com.artron.base.task;
import java.util.Hashtable;
import java.util.Vector;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.artron.base.db.DBBean;
import com.artron.base.log.LogException;
import com.artron.base.log.LogWriter;
public class MailJob implements Job {
public MailJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("time:"+System.currentTimeMillis());
DBBean dbbean=new DBBean();
String select_sql="select id from t_yh where state=0 and rownum<20";
try {
Vector ids=dbbean.execute(select_sql);
for (int i=0;i<ids.size();i++)
{
Hashtable h=(Hashtable)ids.elementAt(i);
String id=(String)h.get("ID");
String sql="update t_yh set state=1 where id="+id;
dbbean.executeSql(sql);
System.out.println("处理内容id:"+id);
}
if (ids.size()==0)
{
System.out.println("暂无任务内容");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
注意:lib包括quartz-1.6.6.jar ,common相关包,jta.jar包等。
然后启动tomcat就可以看到执行效果了。
<?xml version='1.0' encoding='utf-8'?>
<quartz xmlns="http://www.quartzscheduler.org/ns/quartz"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartzscheduler.org/ns/quartz http://www.quartzscheduler.org/ns/quartz/job_scheduling_data_1_1.xsd"
version="1.1">
<job>
<job-detail>
<name>CollectJob</name>
<group>CollectJobs</group>
<job-class>com.xxx.CollectJob</job-class>
<volatility>false</volatility>
<durability>false</durability>
<recover>false</recover>
</job-detail>
<trigger>
<simple>
<name>CollectTrigger</name>
<group>DEFAULT</group>
<job-name>CollectJob</job-name>
<job-group>DEFAULT</job-group>
<start-time>2008-11-30T10:00:00 AM</start-time>
<!-- repeat indefinitely every day -->
<repeat-count>-1</repeat-count>
<repeat-interval>7200000</repeat-interval>
</simple>
</trigger>
</job>
</quartz>