QuartzPendingJobsReportingTask { Map<String, PendingJob> fetchResults() { List<PendingJob> result = jdbcTemplate.query( "SELECT job_class_name, job_group, COUNT(*) FROM QRTZ_JOB_DETAILS GROUP BY job_class_name, job_group", (rs, num) -> new PendingJob(extractClassName(rs.getString(1)), rs.getString(2), rs.getLong(3)) ); return result.stream().collect(Collectors.toMap(PendingJob::getKey, Function.identity())); } QuartzPendingJobsReportingTask(@Autowired DataSource dataSource,
                                          @Autowired MeterRegistry meterRegistry); @Scheduled(fixedRateString = "${l10n.management.metrics.quartz.sql-queue-monitoring.execution-rate}") void reportPendingJobs();  }
@Test public void testFetchResults() throws Exception { JobDetail job; TriggerBuilder<Trigger> builder; QuartzPendingJobsReportingTask.PendingJob pendingJob1, pendingJob2; String key1 = "QuartzPendingJobsReportingTaskTest$Test1Job-" + DYNAMIC_GROUP_NAME; String key2 = "EmptyTestJob-" + DYNAMIC_GROUP_NAME; for (int i = 1; i <= 5; i++) { job = newJob(Test1Job.class).withIdentity(testIdWatcher.getEntityName("Test1Job_" + i), DYNAMIC_GROUP_NAME) .build(); builder = TriggerBuilder.newTrigger() .forJob(job) .startAt(Date.from(ZonedDateTime.now().plusHours(i).toInstant())); scheduler.scheduleJob(job, builder.build()); job = newJob(EmptyTestJob.class).withIdentity(testIdWatcher.getEntityName("EmptyJob_" + i), DYNAMIC_GROUP_NAME) .build(); builder = TriggerBuilder.newTrigger() .forJob(job) .startAt(Date.from(ZonedDateTime.now().plusHours(i).toInstant())); scheduler.scheduleJob(job, builder.build()); } Map<String, QuartzPendingJobsReportingTask.PendingJob> pendingJobs = task.fetchResults(); assertThat(pendingJobs).hasSize(2); assertThat(pendingJobs).containsKey(key1); assertThat(pendingJobs).containsKey(key2); pendingJob1 = pendingJobs.get(key1); pendingJob2 = pendingJobs.get(key2); assertThat(pendingJob1.jobClass).isEqualTo("QuartzPendingJobsReportingTaskTest$Test1Job"); assertThat(pendingJob1.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob1.count).isEqualTo(5L); assertThat(pendingJob2.jobClass).isEqualTo("EmptyTestJob"); assertThat(pendingJob2.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob2.count).isEqualTo(5L); for (int i = 6; i <= 9; i++) { job = newJob(Test1Job.class).withIdentity(testIdWatcher.getEntityName("Test1Job_" + i), DYNAMIC_GROUP_NAME) .build(); builder = TriggerBuilder.newTrigger() .forJob(job) .startAt(Date.from(ZonedDateTime.now().plusHours(i).toInstant())); scheduler.scheduleJob(job, builder.build()); job = newJob(EmptyTestJob.class).withIdentity(testIdWatcher.getEntityName("EmptyJob_" + i), DYNAMIC_GROUP_NAME) .build(); builder = TriggerBuilder.newTrigger() .forJob(job) .startAt(Date.from(ZonedDateTime.now().plusHours(i).toInstant())); scheduler.scheduleJob(job, builder.build()); } pendingJobs = task.fetchResults(); assertThat(pendingJobs).hasSize(2); assertThat(pendingJobs).containsKey(key1); assertThat(pendingJobs).containsKey(key2); pendingJob1 = pendingJobs.get(key1); pendingJob2 = pendingJobs.get(key2); assertThat(pendingJob1.jobClass).isEqualTo("QuartzPendingJobsReportingTaskTest$Test1Job"); assertThat(pendingJob1.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob1.count).isEqualTo(9L); assertThat(pendingJob2.jobClass).isEqualTo("EmptyTestJob"); assertThat(pendingJob2.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob2.count).isEqualTo(9L); for (int i = 11; i <= 21; i++) { job = newJob(Test1Job.class).withIdentity(testIdWatcher.getEntityName("Test1Job_" + i), DYNAMIC_GROUP_NAME) .build(); builder = TriggerBuilder.newTrigger() .forJob(job) .startAt(Date.from(ZonedDateTime.now().plusHours(i).toInstant())); scheduler.scheduleJob(job, builder.build()); } pendingJobs = task.fetchResults(); assertThat(pendingJobs).hasSize(2); assertThat(pendingJobs).containsKey(key1); assertThat(pendingJobs).containsKey(key2); pendingJob1 = pendingJobs.get(key1); pendingJob2 = pendingJobs.get(key2); assertThat(pendingJob1.jobClass).isEqualTo("QuartzPendingJobsReportingTaskTest$Test1Job"); assertThat(pendingJob1.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob1.count).isEqualTo(20L); assertThat(pendingJob2.jobClass).isEqualTo("EmptyTestJob"); assertThat(pendingJob2.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob2.count).isEqualTo(9L); scheduler.clear(); for (int i = 1; i <= 5; i++) { job = newJob(EmptyTestJob.class).withIdentity(testIdWatcher.getEntityName("EmptyJob_" + i), DYNAMIC_GROUP_NAME) .build(); builder = TriggerBuilder.newTrigger() .forJob(job) .startAt(Date.from(ZonedDateTime.now().plusHours(i).toInstant())); scheduler.scheduleJob(job, builder.build()); } pendingJobs = task.fetchResults(); assertThat(pendingJobs).hasSize(1); assertThat(pendingJobs).doesNotContainKey(key1); assertThat(pendingJobs).containsKey(key2); pendingJob2 = pendingJobs.get(key2); assertThat(pendingJob2.jobClass).isEqualTo("EmptyTestJob"); assertThat(pendingJob2.jobGroup).isEqualTo(DYNAMIC_GROUP_NAME); assertThat(pendingJob2.count).isEqualTo(5L); }