开发环境:
springmvc
quartz-2.2.3在官网下载的。
步骤:
1.首先在web.xml中加入以下代码:
<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>2</load-on-startup>
</servlet>
2.在根目录下创建quartz.properties并添加如下代码
#============================================================================
# Configure Main Scheduler Properties
#============================================================================ # 实例名
org.quartz.scheduler.instanceName = QuartzScheduler
# 实例ID
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 = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
# org.quartz.plugins.xml.JobInitializationPlugin是Quartz自带的插件,
# 默认时,这个插件会在 classpath 中搜索名为 quartz_jobs.xml
# 的文件并从中加载 Job 和 Trigger 信息
# v1.8之前用JobInitializationPlugin
#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval =10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false # 关闭quartz新版本检测功能
org.quartz.scheduler.skipUpdateCheck = true
3.在根目录下创建quartz_jobs.xml因为当加载quartz.properties的时候会寻找该文件,其实也就是任务清单
<?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"> <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>
<overwrite-existing-data>true</overwrite-existing-data>
<ignore-duplicates>false</ignore-duplicates>
</processing-directives> <schedule>
<job>
<name>demo</name>
<group>group1</group>
<description>简单的quartz使用</description>
<job-class>com.seegot.dbms.scheduled.Demo</job-class>
</job>
<trigger>
<cron>
<name>trigger</name>
<group>group1</group>
<job-name>demo</job-name>
<job-group>group1</job-group>
<cron-expression>0/10 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
4.创建一个调用类Demo
package com.seegot.dbms.scheduled; import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; public class Demo implements Job{
private static Logger log = Logger.getLogger(Demo.class); @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
log.info("进来了~~~~~~~~~~~~~~~~··");
} }
结果如下:
2018-07-03 16:04:00,001 INFO com.seegot.dbms.scheduled.Demo.execute(Demo.java:14) - 进来了~~~~~~~~~~~~~~~~··