gpt4 book ai didi

java - 在 Tomcat 启动时自动启动 Quarz Scheduler

转载 作者:行者123 更新时间:2023-11-28 22:17:31 34 4
gpt4 key购买 nike

我正在玩弄 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 从未执行过?

谢谢

最佳答案

在尝试了各种记录器组合等一天半后,我通过仔细阅读 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!");
}
}

现在一切正常。如果您觉得此信息有用,请点赞。

谢谢,托马斯

关于java - 在 Tomcat 启动时自动启动 Quarz Scheduler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39606794/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com