如何在java / struts2中列出Quartz运行的作业

时间:2014-03-14 18:15:07

标签: java struts2 quartz-scheduler

我想列出所有在后台运行的Quartz作业。我使用了以下代码,但我没有获得任何列表。

 public class RunningJobs {

    public void getJobList() throws SchedulerException {

        Scheduler schedule = new StdSchedulerFactory().getScheduler();
        System.out.println(schedule.getJobGroupNames().size()+" "+schedule.isShutdown());
        for (String groupName : schedule.getJobGroupNames()) {
            for (JobKey jobKey : schedule.getJobKeys(GroupMatcher
                    .jobGroupEquals(groupName))) {
                String jobName = jobKey.getName();
                String jobGroup = jobKey.getGroup();
                List<Trigger> triggers = (List<Trigger>) schedule
                        .getTriggersOfJob(jobKey);
                Date nextFireTime = triggers.get(0).getNextFireTime();
                System.out.println("[jobName] : " + jobName + " [groupName] : "
                        + jobGroup + " - " + nextFireTime);
            }

        }

    }
}

虽然很少有作业继续使用web应用程序struts2运行。

1 个答案:

答案 0 :(得分:0)

getScheduler的调用为您提供了一个新的调度程序,它不包含作业。您需要通过以下两种方式之一获取包含实际作业的调度程序的句柄。

遍历所有调度程序:

  for(Scheduler schedule :StdSchedulerFactory().getAllSchedulers()){
            System.out.println(schedule.getJobGroupNames().size()+" "+schedule.isShutdown());
            for (String groupName : schedule.getJobGroupNames()) {
                for (JobKey jobKey : schedule.getJobKeys(GroupMatcher
                        .jobGroupEquals(groupName))) {
                    String jobName = jobKey.getName();
                    String jobGroup = jobKey.getGroup();
                    List<Trigger> triggers = (List<Trigger>) schedule
                            .getTriggersOfJob(jobKey);
                    Date nextFireTime = triggers.get(0).getNextFireTime();
                    System.out.println("[jobName] : " + jobName + " [groupName] : "
                            + jobGroup + " - " + nextFireTime);
                }

            }
    }

或按名称获取调度程序:

Scheduler schedule = new StdSchedulerFactory().getScheduler("schedulerName");