Right now I'm searching for functionality to get job keys by job name in Quartz library.
I see that Scheduler
has method to get job keys by group name using GroupMatcher<JobKey>
also Quartz
has NameMatcher
which can be user in same way as GroupMatcher
Set<JobKey> getJobKeys(NameMatcher<JobKey> matcher) throws SchedulerException;
Do you know is this functionality already exists in Quartz library?
An API for getting the JobKey
by job's name using a NameMatcher
does not exist AFAIK.
However, it's actually easy to get a job's key from its name just by iterating the keys:
public JobKey findJobKey(String jobName) {
// Check running jobs first
for (JobExecutionContext runningJob : scheduler.getCurrentlyExecutingJobs()) {
if (Objects.equals(jobName, runningJob.getJobDetail().getKey().getName())) {
return runningJob.getJobDetail().getKey();
}
}
// Check all jobs if not found
for (String groupName : scheduler.getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
if (Objects.equals(jobName, jobKey.getName())) {
return jobKey;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With