Johannes S.
Johannes S.

Reputation: 21

Quartz Scheduler: How to Group Jobs together?

I wanted to ask if anybody had the same problem with the Quartz scheduler. I created Jobs with Trigger and JobKeys where i set the Groupnames. But when i print out the group that has been set it is always DEFAULT.

How can i Set this groupname to finally be able to group Jobs together and most importantly cancel only specified groups? With a Code similar like this:

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

Input:

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

Output Function:

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

Output:

[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

Upvotes: 2

Views: 10602

Answers (1)

Jyotirmoy
Jyotirmoy

Reputation: 730

When you are seeing up the job identity, you can add the group info. I chain the methods like below, and it works for me (I can see that the group is the desired name that I set):

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

Upvotes: 2

Related Questions