Showing posts with label Quartz. Show all posts
Showing posts with label Quartz. Show all posts

Cron triggers

Posted on by Kim

Fire every five minutes. It should fire on 0, 5, 10 ... 55 minute mark
"0 0/5 * * * ?"

Fire every hour
"0 0 * * * ?"

Fire every 2 hours. It should fire on the hours 0, 2, 4 ... 22
"0 0 0/2 * * ?"

Fire every 10 minutes. It should fire on the minutes 0, 10, 20 ... 50
"20 0/10 * * * ?"

Fire every half an hour between 8AM AM to 12PM
"0 0/30 8-12 * * ?"

Fire every tuesday at 12:15pm
"0 15 12 ? * TUE"

Fire everyday at 1:30am
"0 30 1 ? * MON-SUN"

Quartz

Posted on by Kim

Example of creating multiple triggers for the same job. Plus example of job that just to run once

Maven Dependency

 <dependency>
  <groupId>org.quartz-scheduler</groupId>
  <artifactId>quartz</artifactId>
  <version>2.2.2</version>
 </dependency>

The Alarm job


package as.moes.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AlarmJob implements Job {

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        if (null != jobExecutionContext.getNextFireTime())
            System.out.println("Trigger key..:" + jobExecutionContext.getTrigger().getKey().getName() + " "
                    + sdf.format(new Date()) + " "
                    + sdf.format(jobExecutionContext.getNextFireTime()) + " "
            );
        else
            System.out.println("Trigger key..:" + jobExecutionContext.getTrigger().getKey().getName() + " "
                    + sdf.format(new Date()) + " "
                    + "null "
            );

    }
}

The setup


    public static void setupAlarmJob(Date date, String identity, String... cronScheduleArray) throws SchedulerException {
        JobDetail job = JobBuilder.newJob(AlarmJob.class)
                .withIdentity(identity, identity)
                .build();

        Set triggerList = new HashSet();
        if (null != date)
            triggerList.add(
            TriggerBuilder
                    .newTrigger()
                    .startAt(date)
                    .build()
            );


        for (String cronSchedule : cronScheduleArray) {
            triggerList.add(setupAlarmTrigger(cronSchedule));
        }

        Map> map = new HashMap>();
        map.put(job, triggerList);

        scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJobs(map, true);
    }

    public static void setupAlarmJob(String identity, String... cronScheduleArray) throws SchedulerException {
        setupAlarmJob(null, identity, cronScheduleArray);
    }

    public static Trigger setupAlarmTrigger(String cronSchedule) throws SchedulerException {
        return TriggerBuilder
                .newTrigger()
                .withIdentity(cronSchedule)
                .withSchedule(CronScheduleBuilder.cronSchedule(cronSchedule))
                .build();
    }