Get a list of all currently executing jobs in a cluster with Quartz

jakarta-ee, quartz-scheduler

Solution

Looks like overhauling the scheduling mechanism isn't happening anytime soon.

So, here's how I'm checking the table directly - add group support if you want it:

class QuartzClusterJobStatusService
{
    def quartzScheduler

    boolean isJobRunning(String job) {
        return isJobRunningHere(job) || isJobRunningElsewhere(job)
    }

    boolean isJobRunningHere(String job) {
        for (JobExecutionContext j : quartzScheduler.getCurrentlyExecutingJobs()) {
            if (new JobKey(job,"GRAILS_JOBS").equals(j.jobDetail.key)) {
                return true
            }
        }
        return false
    }

    boolean isJobRunningElsewhere(String job) {
        JobStoreSupport js = quartzScheduler.sched.resources.jobStore
        if (!js.isClustered()) {
            return false
        }
        Connection conn = DBConnectionManager.getInstance().getConnection(js.getDataSource());
        PreparedStatement stmt = null
        try {
            stmt = conn.prepareStatement("SELECT 1 FROM " + js.getTablePrefix() + "FIRED_TRIGGERS where JOB_NAME = ?")
            stmt.setString(1, job)
            ResultSet rs = stmt.executeQuery()
            return rs.next()
        } finally {
            if (stmt != null)
                stmt.close()
        }
    }
}

Problem

The method Scheduler.getCurrentlyExecutingJobs() in quartz apparently is not cluster aware. What method are people using to get a list of all executing jobs?

Original source