最近因项目需要使用了quartz做任务调度,现将quartz部署过程记录如下:
1.下载quartz包,解压后,如下图:
1)将quartz-2.1.7.jar包添加到项目中。
2)找到examples\example10目录下的quartz.properties 、quartz_data.xml文件拷贝到项目源代码根目录下。
2.在web.xml中添加如下信息:
<!-- start --> <context-param> <param-name>quartz:config-file</param-name> <param-value>quartz.properties</param-value> </context-param> <context-param> <param-name>quartz:shutdown-on-unload</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>quartz:wait-on-shutdown</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>quartz:start-on-load</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class> org.quartz.ee.servlet.QuartzInitializerListener </listener-class> </listener> <!-- end -->
3.修改quartz.properties文件:
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName: LCGLScheduler org.quartz.scheduler.instanceId: AUTO org.quartz.scheduler.skipUpdateCheck: true #============================================================================ # 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: 60000 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore #org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX #org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.oracle.OracleDelegate #org.quartz.jobStore.useProperties: false #org.quartz.jobStore.dataSource: myDS #org.quartz.jobStore.tablePrefix: QRTZ_ #org.quartz.jobStore.isClustered: false #============================================================================ # Configure Datasources #============================================================================ org.quartz.dataSource.myDS.driver: oracle.jdbc.driver.OracleDriver org.quartz.dataSource.myDS.URL: jdbc:oracle:thin:@10.28.19.120:1521:orcl org.quartz.dataSource.myDS.user: lcgl org.quartz.dataSource.myDS.password: lcgl org.quartz.dataSource.myDS.maxConnections: 5 #============================================================================ # Configure Plugins #============================================================================ org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin org.quartz.plugin.jobInitializer.class: org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames: quartz_data.xml org.quartz.plugin.jobInitializer.failOnFileNotFound: true org.quartz.plugin.jobInitializer.scanInterval: 120 org.quartz.plugin.jobInitializer.wrapInUserTransaction: false
4.开发job类。可参考上述下载包中的例子,本处使用的是Contrigger 类型的trigger。
5.在quartz_data.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_2_0.xsd" version="2.0"> <pre-processing-commands> <delete-jobs-in-group>*</delete-jobs-in-group> <!-- clear all jobs in scheduler --> <delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler --> </pre-processing-commands> <processing-directives> <!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them --> <overwrite-existing-data>true</overwrite-existing-data> <!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error --> <ignore-duplicates>false</ignore-duplicates> </processing-directives> <schedule> <job> <name>SMSWarnJob1</name> <group>Job-Group1</group> <job-class>com.xx.xx.xx.job.xxxxJob</job-class> </job> <trigger> <cron> <name>SMSWarnCronTrigger1</name> <group>Trigger-Group1</group> <job-name>SMSWarnJob1</job-name> <job-group>Job-Group1</job-group> <!-- start-time>2010-02-09T12:26:00.0</start-time --> <!--end-time>2012-02-09T12:26:00.0</end-time--> <misfire-instruction>MISFIRE_INSTRUCTION_DO_NOTHING</misfire-instruction> <cron-expression>0 16 11 ? * *</cron-expression> <!--time-zone>America/Los_Angeles</time-zone --> </cron> </trigger> <trigger> <cron> <name>SMSWarnCronTrigger2</name> <group>Trigger-Group1</group> <job-name>SMSWarnJob1</job-name> <job-group>Job-Group1</job-group> <!-- start-time>2010-02-09T12:26:00.0</start-time --> <!--end-time>2012-02-09T12:26:00.0</end-time--> <misfire-instruction>MISFIRE_INSTRUCTION_DO_NOTHING</misfire-instruction> <cron-expression>0 15 11 ? * *</cron-expression> <!--time-zone>America/Los_Angeles</time-zone --> </cron> </trigger> </schedule> </job-scheduling-data>
6.启动tomcat服务器测试任务。
转载于:https://blog.51cto.com/yangxianhong/1229835