配置式Quartz实例

1、加包:

quartz-all-1.5.2.jar、commons-logging.jar、commons-digester.jar、commons-beanutils-1.7.0.jar到随便一个位置随便一个目录下,比如把这些包放到e盘底下的mylib目录下。那在工程中将此mylib目录路径加到工程classpath路径下,即可。

2、src下写个作业调度类,功能是每个一秒钟打印系统当前时间到控制台:


import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class TestQuartz implements Job{

public void execute(JobExecutionContext arg0) throws JobExecutionException {
  System.out.println(new Date());
}
}

3、web.xml配置:

<?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>
     </servlet>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


这样,在启动Tomcat的时候,QuartzInitializerServlet这个Servlet就会自动读取quartz.properties这个配置文件,并初始化调度信息,启动Scheduler。


4、quartz.properties文件:

这个文件的默认名称就是quartz.properties,如果启动项目的时候,Quartz没有在工程中找到该文件,就会从自己的jar包下面读取其默认的properties文件,其内容如下:

org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = one

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 2
org.quartz.threadPool.threadPriority = 4

org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}
org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_job.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true

上面的org.quartz.plugin.jobInitializer.fileName = quartz_job.xml是用来配置定义job文件的名称。

5、然后编写quartz_job.xml,

<?xml version="1.0" encoding="UTF-8"?>
<quartz>
<job>
    <job-detail>
      <name>testQuartz</name>
      <group>group1</group>
      <job-class>TestQuartz</job-class>
    </job-detail>
    <trigger>
      <cron>
        <name>test</name>
        <group>group1</group>
        <job-name>testQuartz</job-name>
        <job-group>group1</job-group>
        <cron-expression>0 0/1 * * * ?</cron-expression>
     </cron>
    </trigger>
</job>
</quartz>

可以看到,在配置文件中把jobdetail和trigger都作了完整的定义,并组合成一个job。下面,我们把上面两个文件都放入/WEB-INF/classes目录下,然后按照api中的说明修改一下web.xml。

7、运行结果:

2011-4-13 17:23:00 org.quartz.plugins.history.LoggingTriggerHistoryPlugin triggerFired
信息: Trigger group1.test fired job group1.testQuartz at:  17:23:00 04/13/2011
Wed Apr 13 17:23:00 CST 2011
2011-4-13 17:23:00 org.quartz.plugins.history.LoggingTriggerHistoryPlugin triggerComplete
信息: Trigger group1.test completed firing job group1.testQuartz at  17:23:00 04/13/2011 with resulting trigger instruction code: DO NOTHING
2011-4-13 17:24:00 org.quartz.plugins.history.LoggingTriggerHistoryPlugin triggerFired
信息: Trigger group1.test fired job group1.testQuartz at:  17:24:00 04/13/2011
Wed Apr 13 17:24:00 CST 2011
2011-4-13 17:24:00 org.quartz.plugins.history.LoggingTriggerHistoryPlugin triggerComplete
信息: Trigger group1.test completed firing job group1.testQuartz at  17:24:00 04/13/2011 with resulting trigger instruction code: DO NOTHING
......

8、总结:配置的好处是不必手工创建Trigger和Scheduler,Quartz可以通过配置文件来完成后台的作业调度。

你可能感兴趣的:(quartz)