石英触发器不触发
Quartz Trigger Not Firing
我有一个在 tomcat 上运行的 java 网络应用程序。
我正在使用 Quartz 2.3.0
(已更新至 2.3.2
,仍然无法正常工作)
我正在用 Properties
初始化石英,如下所示:
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
class:
public class RunnableJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
//some code
} catch (Exception e) {
//log the exception
};
}
}
但是触发器从未触发,当我看到 QRTZ_TRIGGERS
table 我的 MySQL 数据库时,我可以看到触发器和作业已成功添加,但是当触发触发器的时机到了,我看到几秒钟后 NEXT_FIRE_TIME
列发生了变化。
我已经阅读 this 并检查了这些值:
quartzScheduler.isStarted();
quartzScheduler.isInStandbyMode();
这是:true
和 false
意味着调度程序处于正确的状态,而且我的 Trigger
状态是 WAITING
。
奇怪的是它在我的系统上运行良好,但是当我将它部署到生产系统上时却失败了。
我的系统上有 Windows 10
,服务器上有 Centos
和 Tomcat 9
,我正在使用 Java 14
。
更新:
经过一些更改(不是触发器和作业的创建方式),现在 NEXT_FIRE_TIME
没有更新,我检查了 catalina.out
并且它充满了这个异常:
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.hasNext(ServiceLoader.java:1301)
at java.base/java.util.ServiceLoader.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
并尝试从数据库获取连接时。服务器和应用程序都工作正常,只有石英失败并出现一个非常奇怪的错误:
java.lang.NoClassDefFoundError: java/lang/Thread
而且我只在我的一个应用程序(用于测试 porpuses)上使用 QuartzScheduler
当 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
通过将我的 MySQL 连接器版本更新到 8 并使用 com.mysql.cj.jdbc.Driver
,我的问题消失了,一切似乎都工作正常。
我有一个在 tomcat 上运行的 java 网络应用程序。
我正在使用 Quartz 2.3.0
(已更新至 2.3.2
,仍然无法正常工作)
我正在用 Properties
初始化石英,如下所示:
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
class:
public class RunnableJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
//some code
} catch (Exception e) {
//log the exception
};
}
}
但是触发器从未触发,当我看到 QRTZ_TRIGGERS
table 我的 MySQL 数据库时,我可以看到触发器和作业已成功添加,但是当触发触发器的时机到了,我看到几秒钟后 NEXT_FIRE_TIME
列发生了变化。
我已经阅读 this 并检查了这些值:
quartzScheduler.isStarted();
quartzScheduler.isInStandbyMode();
这是:true
和 false
意味着调度程序处于正确的状态,而且我的 Trigger
状态是 WAITING
。
奇怪的是它在我的系统上运行良好,但是当我将它部署到生产系统上时却失败了。
我的系统上有 Windows 10
,服务器上有 Centos
和 Tomcat 9
,我正在使用 Java 14
。
更新:
经过一些更改(不是触发器和作业的创建方式),现在 NEXT_FIRE_TIME
没有更新,我检查了 catalina.out
并且它充满了这个异常:
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.hasNext(ServiceLoader.java:1301)
at java.base/java.util.ServiceLoader.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
并尝试从数据库获取连接时。服务器和应用程序都工作正常,只有石英失败并出现一个非常奇怪的错误:
java.lang.NoClassDefFoundError: java/lang/Thread
而且我只在我的一个应用程序(用于测试 porpuses)上使用 QuartzScheduler
当 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
通过将我的 MySQL 连接器版本更新到 8 并使用 com.mysql.cj.jdbc.Driver
,我的问题消失了,一切似乎都工作正常。