Quartz列出排程器所有作業


下面是兩個程式碼片段展示如何列出所有Quartz的作業。Quartz2 API都發生了很大變化,所以語法和Quartz1.x是不同的

1. Quartz 2.2.1 範例

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

2. Quartz 1.8.6 範例

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
 
    //loop all group
    for (String groupName : scheduler.getJobGroupNames()) {
 
	//loop all jobs by groupname
	for (String jobName : scheduler.getJobNames(groupName)) {
 
          //get job's trigger
	  Trigger[] triggers = scheduler.getTriggersOfJob(jobName,groupName);
	  Date nextFireTime = triggers[0].getNextFireTime();
 
	  System.out.println("[jobName] : " + jobName + " [groupName] : "
			+ groupName + " - " + nextFireTime);
 
	}
 
    }