当前位置: 首页 > 知识库问答 >
问题:

重新启动quartz调度程序而不出错

邵鸿福
2023-03-14

语境

我试图使用JDBC在集群模式下使用quartz调度器。

问题

在开始使用集群模式下的jdbc之前,我只是用RAM存储测试了调度器。这没有问题,我能够重新启动调度程序(主类)没有任何错误。我现在遇到的问题是,当我停止执行(CTRL+C)然后重新启动它时,我总是得到错误消息:

org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'MyTestJob', because one already exists with this identification.

我不明白这是怎么回事。quartz不支持重新启动调度程序吗?我的意思是,如果出现崩溃,调度程序在恢复后重新启动会发生什么?从quartz数据库中删除作业是唯一的选择吗?也许还有另一种方法或什么我错过了。我觉得使用一个不能处理重启的库不太舒服。

下面是我的配置:

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================

org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = Test-Scheduler
org.quartz.scheduler.instanceId = AUTO

#============================================================================
# Configure ThreadPool  
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5

#============================================================================
# Configure JobStore  
#============================================================================

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = true
org.quartz.jobStore.dataSource = quartzDS
org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000

下面是我的代码:

    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler scheduler = sf.getScheduler();

    scheduler.start();

                JobDetail jobDetail = newJob(job.getClass())
                    .withIdentity("test-name", "test-group")
                    .build();

                CronTrigger trigger = newTrigger()
                    .withIdentity("test-name-trigger", "test-group")
                    .withSchedule(cronSchedule("0 0/1 * * * ?"))
                    .build();

                scheduler.scheduleJob(jobDetail, trigger);

                System.out.println(trigger.getNextFireTime());

编辑

19:57:29,913  INFO StdSchedulerFactory:1184 - Using default implementation for ThreadExecutor
19:57:29,936  INFO SchedulerSignalerImpl:61 - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
19:57:29,936  INFO QuartzScheduler:240 - Quartz Scheduler v.2.2.1 created.
19:57:29,938  INFO JobStoreTX:667 - Using db table-based data access locking (synchronization).
19:57:29,940  INFO JobStoreTX:59 - JobStoreTX initialized.
19:57:29,941  INFO QuartzScheduler:305 - Scheduler meta-data: Quartz Scheduler (v2.2.1) 'Test-Scheduler' with instanceId 'Michael-PC1405447049916'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 25 threads.
  Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is clustered.

19:57:29,941  INFO StdSchedulerFactory:1339 - Quartz scheduler 'Test-Scheduler' initialized from default resource file in Quartz package: 'quartz.properties'
19:57:29,941  INFO StdSchedulerFactory:1343 - Quartz scheduler version: 2.2.1
19:57:29,995  INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgeby993gf1xpdmdc44s|7ec4d0, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgeby993gf1xpdmdc44s|7ec4d0, idleConnectionTestPeriod -> 50, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/scheduler, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 5, maxStatements -> 0, maxStatementsPerConnection -> 120, minPoolSize -> 1, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> SELECT 1 FROM QRTZ_JOB_DETAILS, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> true, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
19:57:30,243 DEBUG StdRowLockSemaphore:107 - Lock 'TRIGGER_ACCESS' is desired by: main
19:57:30,262 DEBUG StdRowLockSemaphore:92 - Lock 'TRIGGER_ACCESS' is being obtained: main
19:58:21,328 DEBUG StdRowLockSemaphore:141 - Lock 'TRIGGER_ACCESS' was not obtained by: main - will try again.
19:58:22,329 DEBUG StdRowLockSemaphore:92 - Lock 'TRIGGER_ACCESS' is being obtained: main
19:59:13,389 DEBUG StdRowLockSemaphore:141 - Lock 'TRIGGER_ACCESS' was not obtained by: main - will try again.
19:59:14,389 DEBUG StdRowLockSemaphore:92 - Lock 'TRIGGER_ACCESS' is being obtained: main
Exception in thread "main" org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: Lock wait timeout exceeded; try restarting transaction [See nested exception: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction]
    at org.quartz.impl.jdbcjobstore.StdRowLockSemaphore.executeSQL(StdRowLockSemaphore.java:157)
    at org.quartz.impl.jdbcjobstore.DBSemaphore.obtainLock(DBSemaphore.java:113)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3780)
    at org.quartz.impl.jdbcjobstore.JobStoreTX.executeInLock(JobStoreTX.java:93)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.clearAllSchedulingData(JobStoreSupport.java:1956)
    at org.quartz.core.QuartzScheduler.clear(QuartzScheduler.java:1572)
    at org.quartz.impl.StdScheduler.clear(StdScheduler.java:239)
    at com.scs.core.cron.TaskRunner.main(TaskRunner.java:52)
  20:04:15,993 DEBUG SimpleSemaphore:132 - Lock 'TRIGGER_ACCESS' retuned by: main
  20:04:15,993 DEBUG JobStoreTX:703 - JobStore background threads started (as scheduler was started).
  20:04:15,994  INFO QuartzScheduler:575 - Scheduler Test-Scheduler_$_NON_CLUSTERED started.
  20:04:15,994 DEBUG JobStoreTX:3933 - MisfireHandler: scanning for misfires...
  20:04:16,000 DEBUG JobStoreTX:3182 - Found 0 triggers that missed their scheduled fire-time.
  20:04:16,004 DEBUG QuartzSchedulerThread:276 - batch acquisition of 0 triggers
  20:04:16,008 DEBUG SimpleSemaphore:81 - Lock 'TRIGGER_ACCESS' is desired by: main
  20:04:16,008 DEBUG SimpleSemaphore:88 - Lock 'TRIGGER_ACCESS' is being obtained: main
  20:04:16,008 DEBUG SimpleSemaphore:105 - Lock 'TRIGGER_ACCESS' given to: main
        20:04:16,052 DEBUG SimpleSemaphore:132 - Lock 'TRIGGER_ACCESS' retuned by: main
        Found job: class to.test.cron.ImportProducts
        Tue Jul 15 20:05:00 CEST 2014
        isStarted=true
        isShutdown=false
        isInStandbyMode=false
        20:04:16,058 DEBUG QuartzSchedulerThread:276 - batch acquisition of 0 triggers
        20:04:42,961 ERROR ErrorLogger:2425 - An error occurred while scanning for the next triggers to fire.
        org.quartz.JobPersistenceException: Couldn't acquire next trigger: to.test.cron.ImportProducts [See nested exception: java.lang.ClassNotFoundException: to.test.cron.ImportProducts]
            at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTrigger(JobStoreSupport.java:2848)
            at org.quartz.impl.jdbcjobstore.JobStoreSupport$40.execute(JobStoreSupport.java:2759)
            at org.quartz.impl.jdbcjobstore.JobStoreSupport$40.execute(JobStoreSupport.java:2757)
            at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3787)
            at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTriggers(JobStoreSupport.java:2756)
            at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:272)
        Caused by: java.lang.ClassNotFoundException: to.test.cron.ImportProducts
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at org.quartz.simpl.InitThreadContextClassLoadHelper.loadClass(InitThreadContextClassLoadHelper.java:72)
            at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:114)
            at org.quartz.simpl.CascadingClassLoadHelper.loadClass(CascadingClassLoadHelper.java:138)
            at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.selectJobDetail(StdJDBCDelegate.java:852)
            at org.quartz.impl.jdbcjobstore.JobStoreSupport.acquireNextTrigger(JobStoreSupport.java:2816)
            ... 5 more

我不太明白为什么我在三种不同的模式中得到了三种完全不同的行为。Surley如果类可以在RAM模式下找到,为什么不能在jdbc模式下找到呢?为什么不以集群模式记录?这个类实际上是在一个OSGi类型的模块中。这会导致问题吗(在JDBC模式下)?有什么我可以做的,这样可以找到类,像传递类加载器等到石英?

我现在很迷路,真的很感激任何帮助。如果不得不回到标准的cron工作岗位,那将是一种耻辱,尤其是在quartz有这么多东西可以提供的情况下。

提前感谢您提供的任何帮助,

共有1个答案

缪坚诚
2023-03-14

这是持久化作业存储的一般“问题”。您的应用程序显然试图添加一个已经存在于作业存储区中的作业,因为它已经由您的应用程序在过去添加过。您有两种选择:

>

  • 在尝试添加作业/触发器之前,在应用程序初始化期间清除作业存储区的内容。从Quartz2.x开始,您可以使用一个新方法scheduler.clear()。

    您可以修改应用程序代码,以处理您试图添加的作业/触发器可能已经存在于作业存储区中的事实。如果存在,则只需在必要时更新作业/触发器,或者完全跳过作业/触发器。

    希望这能有所帮助。

  •  类似资料:
    • 我们有一个Java应用程序,它使用Quartz来调度作业。我们使用的quartz版本是:Quartz-2.2.1 quartz配置使用JDBC作业存储。 如果在quartz scheduler对象上调用start方法时数据库连接关闭(由于断断续续的网络故障),它将以以下异常失败: 为了确保quartz scheduler成功启动,我们在代码中添加了重试,它在每1秒后调用quartz schedul

    • 希望有人能帮我找出,如果不是一个解决方案,至少是一个行为的解释。 问题: 在一些设备上,按下启动器图标会导致当前任务恢复,在其他设备上,会导致初始启动意图被激发(有效地重新启动应用程序)。为什么会这样? 细节: 当您按下“启动程序图标”时,应用程序会正常启动-也就是说,我假设,使用您的第一个和操作和类别。然而,情况并非总是如此: 在大多数设备上,如果您在应用程序已经运行后按下启动器图标,则会恢复该

    • 我希望它们按以下顺序运行:job3、job4、job1、job2 在20次运行中,只有大约3次我得到了预期的结果。对于其他任务,要运行的第一个任务几乎是随机的,其余任务的顺序是正确的。一次运行如下:job1,job3,job4,job2或job4,job3,job1,job2 我是不是没有正确理解Quartz中的优先级是如何工作的?我找不到到底发生了什么。

    • 我使用Quartz Scheduler(使用JobDetailFactoryBean)和Spring来安排一些作业。现在我通过XmlApplicationContext通过spring配置实现了这一点,我必须将其更改为AnnotationConfigApplicationContext。那么,我如何在AnnotationConfigApplicationContext中实现这个cronjob呢。这

    • 在Spring Boot中,在不重新启动应用程序的情况下更新前端部分?