我正在玩Quarz library,但我无法让调度程序执行这项工作。在这里设置:
我在启动时(Tomcat)使用 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/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>
以及 quartz.properties 文件的内容:
org.quartz.scheduler.interruptJobsOnShutdownWithWait=true
#===================================================
# Configure the ThreadPool
#===================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#===================================================
# Configure the Job
#===================================================
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
我可以从日志输出中看到调度程序与Tomcat一起启动:
INFO: QuartzInitializer: Scheduler has been started...
Sep 21, 2016 12:50:49 PM org.apache.catalina.core.ApplicationContext log
INFO: QuartzInitializer: Storing the Quartz Scheduler Factory in the servlet context at key: org.quartz.impl.StdSchedulerFactory.KEY
Sep 21, 2016 12:50:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 21, 2016 12:50:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2373 ms
问题是Quarz的工作永远不会被解雇。这里是 jobs.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_1_8.xsd" version="1.8">
<schedule>
<job>
<name>Job</name>
<group>Job</group>
<description>Testing the job</description>
<job-class>dev.dit.scheduler.TestJob</job-class>
</job>
<trigger>
<cron>
<name>Trigger</name>
<job-name>Trigger</job-name>
<job-group>TriggerGroup</job-group>
<cron-expression>0 0/5 * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
以及TestJob.java类的内容:
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("Job executed!");
}
}
有人可以告诉我为什么TestJob从未执行过吗?
由于
答案 0 :(得分:1)
经过一天半的尝试各种记录器等组合后,我通过仔细阅读 jobs.xml 文件找到了明显的结果。
正如您在我的原始示例中所看到的,我将作业和工作组命名为:
<name>Job</name>
<group>Job</group>
但我没有在触发器设置中引用名称和组:
<job-name>Trigger</job-name>
<job-group>TriggerGroup</job-group>
这里正确设置 jobs.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>PROCESS_LEAD_JOB_GROUP</delete-jobs-in-group>
<delete-triggers-in-group>PROCESS_LEAD_TRIGGER_GROUP</delete-triggers-in-group>
</pre-processing-commands>
<schedule>
<job>
<name>Job</name>
<group>Jobs</group>
<description>Testing the job</description>
<job-class>dev.dit.scheduler.TestJob </job-class>
</job>
<trigger>
<cron>
<name>Trigger</name>
<job-name>Job</job-name>
<job-group>Jobs</job-group>
<cron-expression>0/10 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
我还将log4j.properties放在类路径中:
log4j.rootLogger = INFO, STDOUT, FILE
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
我将记录器添加到作业文件中:
package dev.dit.scheduler;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestJob implements Job {
final static Logger logger = Logger.getLogger(TestJob.class);
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
logger.info("Job executed!");
}
}
现在一切正常。如果您发现此信息有用,请进行Upvote。
谢谢, 托马斯