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(); SettriggerList = 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(); }
0 Responses to "Quartz":
Post a Comment