所以还是用老办法,在web.xml中添加job
代码如下:
quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
#处理的线程个数
org.quartz.threadPool.threadCount = 3
#线程优先级别,一般为5
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.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = job.xml
#如果jobs.xml中存在调度器中已经有的job,true为覆盖
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
#扫描jobs.xml的时间间隔
org.quartz.plugin.jobInitializer.scanInterval = 10000
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
job.xml:
<?xml version="1.0" encoding="UTF-8"?>
<quartz>
<job>
<job-detail>
<name>job1</name>
<group>job</group>
<job-class>com.cal.quartz.Job1</job-class>
</job-detail>
<trigger>
<cron>
<name>t1</name>
<group>job</group>
<job-name>job1</job-name>
<job-group>job</job-group>
<cron-expression>0/10 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
<job>
<job-detail>
<name>job2</name>
<group>job</group>
<job-class>com.cal.quartz.Job2</job-class>
</job-detail>
<trigger>
<cron>
<name>t2</name>
<group>job</group>
<job-name>job2</job-name>
<job-group>job</job-group>
<cron-expression>0/20 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>
调用的java类
package com.cal.quartz;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Job1 implements Job {
public Job1() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("job1-" + new Date());
}
}
package com.cal.quartz;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Job2 implements Job {
public Job2() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("job2-" + new Date());
}
}
在web.xml中加入此servlet
<servlet>
<servlet-name>quartz</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>1</load-on-startup>
</servlet>