Quartz Calendars can be used by the scheduler to block of a list of days, range of time or particular days of the year/month/week from the scheduler fire timings. Attaching a calendar onto a trigger ensures that the trigger does not get fired on date/time as defined by the Calendar.
There are different types of Calendar already available or a new Calendar can be using the Quartz calendar interface. List of available calendars on quartz can be got here
The below sample shows the use of one such Calendar – WeeklyCalendar that disables job fire on weekends – perfect for our Scheduler application. The method of using it is to first create an object of the WeeklyCalendar and then add it onto the scheduler along with a string name through which it can be further referenced. Then, this string name is used as an argument in setting the calendar name for the trigger.
package com.opensourzesupport.schedule; import com.opensourzesupport.schedule.jobs.MyJob; import com.opensourzesupport.schedule.listeners.MyJobListener; import java.util.Calendar; import java.util.Date; import java.util.Properties; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerUtils; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.calendar.WeeklyCalendar; /** * * @author prasobh */ public class Schedular { public Schedular() { try { //Create the weekly calendar object - //This by default handles disaabling job fire on weekends so no need //to explicitly set WeeklyCalendar weeklyOff = new WeeklyCalendar(); //AnnualCalendar annualCalendar = new AnnualCalendar(); //MonthlyCalendar monthlyCalendar = new MonthlyCalendar(); //monthlyCalendar.setDayExcluded(5, true); //example of adding an excluded day of the week - This excludes fridays from job firing schedule weeklyOff.setDayExcluded(Calendar.FRIDAY, true); Properties prop = new Properties(); prop.setProperty("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore"); prop.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); prop.setProperty("org.quartz.threadPool.threadCount", "4"); SchedulerFactory schdFact = new StdSchedulerFactory(prop); Scheduler schd = schdFact.getScheduler(); //add the Calendar object created to the scheduler with a string identifier to it schd.addCalendar("weeklyOff", weeklyOff, false, true); schd.start(); JobDetail jobDetails = new JobDetail("myjob", Scheduler.DEFAULT_GROUP, MyJob.class); Trigger trigger = TriggerUtils.makeMinutelyTrigger(); trigger.setName("every minutes"); //set the calendar associated with the trigger trigger.setCalendarName("weeklyOff"); trigger.getJobDataMap().put("auth_name", "Prasobh"); trigger.setStartTime(new Date()); schd.addJobListener(new MyJobListener()); jobDetails.addJobListener("My Job"); schd.scheduleJob(jobDetails, trigger); System.out.println(schd.getSchedulerName()); schd.start(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new Schedular(); } }
excellent!!!but how to exclude 2nd and 4th Saturday of every month?
ReplyDelete