【问题标题】:Spring boot and Quartz - Job not executing immediatelySpring boot 和 Quartz - 作业没有立即执行
【发布时间】:2019-01-14 07:27:16
【问题描述】:

我正在使用 Spring Boot 配置 Quartz 作业。要求是立即执行作业而不附加任何计划。

这是我的代码的样子

JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();

String jobName = jobName(taskContext);

factoryBean.setJobClass(MyJobClass.class);
factoryBean.setDurability(true);
factoryBean.setApplicationContext(applicationContext);
factoryBean.setName("Hello job");
factoryBean.setGroup("Hello job group");

JobDataMap jobData = new JobDataMap(new HashMap<>());
factoryBean.setJobDataMap(jobData);
factoryBean.afterPropertiesSet();

JobDetail job = factoryBean.getObject();
Scheduler scheduler = schedulerFactoryBean.getScheduler();
scheduler.addJob(job, replace);
scheduler.triggerJob(job.getKey());

这就是quartz.properties 的样子

org.quartz.scheduler.instanceName=springBootQuartzApp
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.threadCount=10
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.misfireThreshold=2000
org.quartz.jobStore.tablePrefix=qrtz_
org.quartz.jobStore.isClustered=false
org.quartz.plugin.shutdownHook.class=org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownHook.cleanShutdown=TRUE

问题是该作业没有立即触发,而是作为失火指令被拾取。它正好在 misfireThreshold 之后执行。

如果我遗漏了配置中的某些内容或没有调用任何适当的 API,请告诉我。

【问题讨论】:

  • 如果您的要求是立即执行作业而不附加任何时间表,那么您为什么要使用 Quartz 调度程序?您可以使用其他方式来完成这项工作!
  • 重试、任务跟踪、异步执行、计划任务和非计划任务的统一设计。这些功能中的大多数已经内置。简而言之,干。
  • org.quartz.SchedulerListener 接口的实现怎么样,然后你感兴趣的方法是'void schedulerStarted();'。因此,当您的调度程序启动时,您将收到此事件并立即执行此操作。请注意,它可能会在您每次在暂停调度程序后启动它时调用此方法 - 所以您可能需要一种机制来解决这个问题!
  • org.quartz.threadPool.threadCount=10,那么当前运行计划中还有超过 10 个的其他作业吗?您是否在应用程序启动时执行了此代码?还是从其他 API 调用它?

标签: java spring-boot quartz-scheduler jobs


【解决方案1】:

我遇到了同样的问题。

如果您的石英正在使用具有事务的数据源:@EnableTransactionManagement。 请在代码的方法中添加@Transactional,然后立即提交事务。 稍后调度程序线程再次查找数据库并最终触发它。

【讨论】:

  • 感谢您的回复。事务管理已通过注释启用。
  • 那你就可以调试了,是在quartz add trigger上慢还是调度器从DB中读取?
猜你喜欢
  • 2012-02-22
  • 2021-10-26
  • 2017-10-16
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 2018-08-29
相关资源
最近更新 更多