gpt4 book ai didi

java - quartz 触发器不触发

转载 作者:行者123 更新时间:2023-12-05 06:12:35 26 4
gpt4 key购买 nike

我有一个在 tomcat 上运行的 java web 应用程序。我正在使用 Quartz 2.3.0(已更新至 2.3.2,仍然无法正常工作)我正在用 Properties 初始化 quartz 如下:

Properties properties = new Properties();
properties.put("org.quartz.scheduler.instanceName", hostname);
properties.put("org.quartz.scheduler.instanceId", hostname);
properties.put("org.quartz.threadPool.threadCount", "5");
properties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
properties.put("org.quartz.jobStore.isClustered", "true");
properties.put("org.quartz.jobStore.misfireThreshold", "600000");//until 10 minutes, dont count as a misfire
properties.put("org.quartz.jobStore.clusterCheckinInterval", "100000");
properties.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
properties.put("org.quartz.jobStore.dataSource", "QCP");
properties.put("org.quartz.dataSource.QCP.connectionProvider.class", "mypackage.quartz.QuartzConnectionProvider");
properties.put("org.quartz.plugin.shutdownhook.class", "org.quartz.plugins.management.ShutdownHookPlugin");//This plugin catches the event of the JVM terminating (such as upon a CRTL-C) and tells the scheuler to shutdown.
properties.put("org.quartz.plugin.shutdownhook.cleanShutdown", "false");
properties.put("org.quartz.scheduler.skipUpdateCheck", "true");

Scheduler quartzScheduler quartzScheduler = new StdSchedulerFactory(properties).getScheduler();

quartzScheduler.start();

然后我添加一个工作:

JobDetail runnableDetail = JobBuilder.newJob(RunnableJob.class)
.withIdentity("runnable_JOB", "runnable")
.storeDurably(true)
.requestRecovery(false)
.build();
quartzScheduler.addJob(runnableDetail , true);

然后像这样为它创建一个触发器:

String uuid = UUID.randomUUID().toString();
Trigger trigg = TriggerBuilder.newTrigger()
.withIdentity("runnable_TRIGGER_"+uuid, "runnable")
.forJob("runnable_JOB", "runnable")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow().withRepeatCount(0))
.startAt(DateBuilder.futureDate(startDelaySeconds , IntervalUnit.SECOND)) //startDelaySeconds is between 1-300
.build();

quartzScheduler.scheduleJob(trigg);

这是我的 RunnableJob类:

public class RunnableJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
//some code
} catch (Exception e) {
//log the exception
};
}
}

但是触发器永远不会触发,当我看到 QRTZ_TRIGGERS 时表我的 MySQL 数据库,我可以看到触发器和作业已成功添加,但是当触发触发器的正确​​时间到来时,我看到几秒钟后 NEXT_FIRE_TIME列更改。

我已经读过this并检查了这些值:

quartzScheduler.isStarted();
quartzScheduler.isInStandbyMode();

是:truefalse这意味着调度程序处于正确状态,也是我的 Trigger状态是 WAITING .

奇怪的是,它在我的系统上运行良好,但当我将它部署到生产系统时却失败了。

我有 Windows 10在我的系统上和 CentosTomcat 9在服务器上,我正在使用 Java 14 .

更新:经过一些更改(不是触发器和作业的创建方式),现在 NEXT_FIRE_TIME不更新,我检查了catalina.out它充满了这个异常(exception):

31-Aug-2020 11:04:56.986 INFO [QuartzScheduler_demo123.domain.co-demo123.domain.co_ClusterManager] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.naming.spi.InitialContextFactory]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.naming.spi.InitialContextFactory]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1385)
at org.apache.catalina.loader.WebappClassLoaderBase.findResources(WebappClassLoaderBase.java:985)
at org.apache.catalina.loader.WebappClassLoaderBase.getResources(WebappClassLoaderBase.java:1086)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1197)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1222)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1266)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1301)
at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1386)
at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:697)
at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
at java.naming/javax.naming.InitialContext.<init>(InitialContext.java:184)
at mypackage.quartz.QuartzConnectionProvider.getConnection(QuartzConnectionProvider.java:25)
at org.quartz.utils.DBConnectionManager.getConnection(DBConnectionManager.java:108)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:780)
at org.quartz.impl.jdbcjobstore.JobStoreTX.getNonManagedTXConnection(JobStoreTX.java:71)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.doCheckin(JobStoreSupport.java:3307)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3920)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.run(JobStoreSupport.java:3957)
- ClusterManager: Error managing cluster: Failed to obtain DB connection from data source 'QCP': java.lang.NoClassDefFoundError: java/lang/Thread
org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'QCP': java.lang.NoClassDefFoundError: java/lang/Thread [See nested exception: java.lang.NoClassDefFoundError: java/lang/Thread]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:789)
at org.quartz.impl.jdbcjobstore.JobStoreTX.getNonManagedTXConnection(JobStoreTX.java:71)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.doCheckin(JobStoreSupport.java:3307)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3920)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.run(JobStoreSupport.java:3957)
Caused by: java.lang.NoClassDefFoundError: java/lang/Thread
at mypackage.quartz.QuartzConnectionProvider.getConnection(QuartzConnectionProvider.java:25)
at org.quartz.utils.DBConnectionManager.getConnection(DBConnectionManager.java:108)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:780)
... 4 more

重启tomcat服务器问题仍未解决。

更新 2:

问题似乎出在我试图从中获取连接的数据源。我的 tomcat 实例在 server.xml 中定义不同的文件 <Host>标签及其资源在 <Context> 中定义我的应用程序的每个实例的标记:

<Resource name="jdbc/mysql/hamkelasi" auth="Container" type="javax.sql.DataSource"

initialSize="1" maxTotal="50" maxIdle="1"
maxWaitMillis="20000" removeAbandonedOnBorrow="true" removeAbandonedTimeout="600"
validationQuery="select now();" timeBetweenEvictionRunsMillis="1800000"

username="user" password="pass" driverClassName="com.mysql.jdbc.Driver"
url="..."
/>

在我的 ContextListener我实例化一个 javax.naming.InitialContext然后使用 .lookup(poolLookupString) .我在这里通过缓存 InitialContext 尝试了不同的方法或缓存数据源但未成功。

仍然,问题只出现在具有相同配置和相同库的生产服务器上。

当我启动 QuartsScheduler 时发生错误以及它何时尝试从数据库获取连接。服务器和应用程序都工作正常,只有 quartz 失败并出现一个非常奇怪的错误:

java.lang.NoClassDefFoundError: java/lang/Thread

而且我只使用 QuartzScheduler在我的一个应用程序上(用于测试 porpuses)

最佳答案

当 quartz 作业线程拾取 a 触发器时,它将锁定该行并更新其 nextfire 时间,否则它将进入 missfire 状态。

如果您想深入研究代码并检查发生了什么( https://github.com/quartz-scheduler/quartz/blob/master/quartz-core/src/main/java/org/quartz/impl/jdbcjobstore/JobStoreSupport.java#L2831 )

尝试为作业和触发器注册监听器,并检查它们是否被接收

http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-07.html

关于java - quartz 触发器不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63595758/

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