简体   繁体   English

Spring boot 和 Quartz - 作业不会立即执行

[英]Spring boot and Quartz - Job not executing immediately

I am configuring Quartz job with Spring boot.我正在使用 Spring Boot 配置 Quartz 作业。 The requirement is to execute the job immediately without attaching any schedule.要求是立即执行作业而不附加任何计划。

Here is what my code looks like这是我的代码的样子

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());

And here is how quartz.properties looks like这是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

The problem is that the job is not firing immediately and is getting picked up as misfire instruction.问题是该作业不会立即触发,而是作为失火指令被接收。 It is executed right exactly after the misfireThreshold.它恰好在 misfireThreshold 之后执行。

Please let me know, if I have missed something in the configuration or didn't call any appropriate API.如果我遗漏了配置中的某些内容或没有调用任何适当的 API,请告诉我。

I got the same issue.我遇到了同样的问题。

If your quartz is using a data source with transaction: @EnableTransactionManagement.如果您的石英使用带有事务的数据源:@EnableTransactionManagement。 Please add @Transactional to the method of your code, then the transaction is committed immediately.请将@Transactional 添加到您代码的方法中,然后立即提交事务。 Later the scheduler thread looks up the db again and fire it finally.稍后调度程序线程再次查找数据库并最终触发它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM