Quartz Scheduler:如何将作业组合在一起?

时间:2016-07-19 11:58:09

标签: java quartz-scheduler scheduling

我想问一下是否有人对Quartz调度程序有同样的问题。我用Trigger和JobKeys创建了Jobs,我设置了Groupnames。但是当我打印出已设置的组时,它总是默认的。

如何设置此组名最终能够将作业组合在一起,最重要的是只取消指定的组?使用类似的代码:

public void unscheduleByGroupname(String groupName) throws SchedulerException {
    for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
        scheduler.unscheduleJob(new TriggerKey(jobKey.getName(), jobKey.getGroup()));
    }
}

输入:

    TriggerKey tKey = new TriggerKey("Trigger:" + jobName + "-Somename:" + object.toString(),
            "Group:" + jobName + "-Somename:" + object.toString());
    JobKey jKey = new JobKey("Job:" + jobName + "-Somename:" + object.toString(),
            "Group:" + jobName + "-Somename:" + object.toString());
    JobDetail job = JobBuilder.newJob(Somename.class).withDescription("Somename")
            .withIdentity(jKey).build();
    Trigger trigger = TriggerBuilder.newTrigger().forJob(jKey).startAt(new Date()).withIdentity(tKey).build();

输出功能:

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);}

输出:

[jobName] : Job:-Somename:13 [groupName] : DEFAULT - Tue Jul 19 13:48:40 CEST 2016
[jobName] : Job:-Somename:14 [groupName] : DEFAULT - Tue Jul 19 13:49:11 CEST 2016
[jobName] : Job:-Somename:15 [groupName] : DEFAULT - Tue Jul 19 13:49:41 CEST 2016
[jobName] : Job:-Somename:16 [groupName] : DEFAULT - Tue Jul 19 13:50:11 CEST 2016

1 个答案:

答案 0 :(得分:0)

当您看到工作标识时,您可以添加组信息。我链接下面的方法,它适用于我(我可以看到该组是我设置的所需名称):

JobDetail job = JobBuilder.newJob(ScheduledJob.class)
            .withIdentity("JOB KEY", "GROUP NAME")
            .withDescription("Job description")
            .usingJobData(dataMap)
            .build();