我正在尝试构建一个应用程序,该应用程序可以使用spring-boot-starter-quartz运行两个基于cron的简单作业。 到目前为止,这是我的代码:
application.yml-石英配置
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: never
properties:
org.quartz.scheduler.instanceName : instanceName
org.quartz.scheduler-factory.auto-startup : true
org.quartz.scheduler.instanceId : AUTO
org.quartz.threadPool.threadCount : 10
org.quartz.jobStore.driverDelegateClass : org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.isClustered : true
这是我触发执行作业的方式:
QuartzScheduler.java
@Value("${cloud.aws.queues.queueName}")
private String queueName;
@Autowired
private Scheduler scheduler;
@Override
public void schedule(JobDTO jobDTO) {
JobDetail job = JobBuilder.newJob()
.ofType(WriteToQueueQuartzJob.class)
.withIdentity("scheduleUnlockJob", "schedule")
.build();
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("queueName", queueName);
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("scheduleTrigger", "schedule")
.startNow()
.withSchedule(SimpleScheduleBuilder.repeatMinutelyForever())
.usingJobData(jobDataMap)
.build();
try {
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
基本上,我正在尝试创建一个触发器,该触发器将调用作业以发送到队列中。因此,我要在JobData中添加目标队列名称,在我的工作中,我有这个名称:
WriteToQueueQuartzJob。
@Setter
@NoArgsConstructor
public class WriteToQueueQuartzJob extends QuartzJobBean {
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
private String queueName;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("job executed");
queueMessagingTemplate.convertAndSend(queueName, "test");
}
}
我得到的日志:
2018-10-31 10:27:00.612 INFO 17232 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-10-31 10:27:00.613 INFO 17232 --- [ main] o.s.i.endpoint.EventDrivenConsumer : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2018-10-31 10:27:00.613 INFO 17232 --- [ main] o.s.i.channel.PublishSubscribeChannel : Channel 'application-1.errorChannel' has 1 subscriber(s).
2018-10-31 10:27:00.613 INFO 17232 --- [ main] o.s.i.endpoint.EventDrivenConsumer : started _org.springframework.integration.errorLogger
2018-10-31 10:27:00.613 INFO 17232 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-10-31 10:27:00.614 INFO 17232 --- [ main] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2018-10-31 10:27:00.629 INFO 17232 --- [ main] o.s.s.quartz.LocalDataSourceJobStore : ClusterManager: detected 1 failed or restarted instances.
2018-10-31 10:27:00.629 INFO 17232 --- [ main] o.s.s.quartz.LocalDataSourceJobStore : ClusterManager: Scanning for instance "CI000214961540974125118"'s failed in-progress jobs.
2018-10-31 10:27:00.638 INFO 17232 --- [ main] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_CI000214961540974411756 started.
2018-10-31 10:27:00.917 INFO 17232 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9020 (http) with context path ''
2018-10-31 10:27:00.933 INFO 17232 --- [ main] c.w.SchedulerMicroserviceApplication : Started SchedulerMicroserviceApplication in 27.471 seconds (JVM running for 30.915)
基本上我想要的是我的应用程序自动调用调度程序以触发作业。我创建了一个虚拟控制器来手动调用Scheduler方法,并且一切都按预期工作,作业正在注册到石英表中,并且消息到达队列中。但是,我找不到任何无需使其自动运行的东西。