使用XML来配置需要设置“quartz.properties” 和 “quartz-config.xml” , 在“quartz.properties”中指定xml文件的名称。
quartz.properties
1
| org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
|
一,完整实例
HelloJob.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.devnp.job;
import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException;
public class HelloJob implements Job{
public void sayHello(){ System.out.println("Hello World."); }
public void execute(JobExecutionContext context) throws JobExecutionException { sayHello(); } }
|
quartz-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?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>JobA</name> <group>GroupDummy</group> <description>This is Job A</description> <job-class>com.devnp.job.HelloJob</job-class> </job>
<trigger> <cron> <name>dummyTriggerNameA</name> <job-name>JobA</job-name> <job-group>GroupDummy</job-group> <cron-expression>0/5 * * * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
|
quartz.properties
1 2 3 4 5 6
| org.quartz.scheduler.instanceName = MySchedulerTest org.quartz.threadPool.threadCount = 3 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true
|
web.xml
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<listener> <listener-class> org.quartz.ee.servlet.QuartzInitializerListener </listener-class> </listener> </web-app>
|
二,代码下载
QuartzXMLDemo.zip
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE