📄 triggerutils.java
字号:
/* * Copyright 2004-2005 OpenSymphony * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * *//* * Previously Copyright (c) 2001-2004 James House */package org.quartz.helpers;import java.util.Calendar;import java.util.Date;import java.util.LinkedList;import java.util.List;import java.util.TimeZone;import org.quartz.CronTrigger;import org.quartz.Scheduler;import org.quartz.SimpleTrigger;import org.quartz.Trigger;/** * <p> * Convenience and utility methods for simplifying the construction and * configuration of <code>{@link Trigger}s</code>. * </p> * * <p> * Please submit suggestions for additional convenience methods to either the * Quartz user forum or the developer's mail list at * <a href="http://www.sourceforge.net/projects/quartz">source forge</a>. * </p> * * @see CronTrigger * @see SimpleTrigger * * @deprecated use org.quartz.TriggerUtils instead! * * @author James House */public class TriggerUtils { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constants. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ public static final int SUNDAY = 1; public static final int MONDAY = 2; public static final int TUESDAY = 3; public static final int WEDNESDAY = 4; public static final int THURSDAY = 5; public static final int FRIDAY = 6; public static final int SATURDAY = 7; public static final int LAST_DAY_OF_MONTH = -1; public static final long MILLISECONDS_IN_MINUTE = 60l * 1000l; public static final long MILLISECONDS_IN_HOUR = 60l * 60l * 1000l; public static final long SECONDS_IN_DAY = 24l * 60l * 60L; public static final long MILLISECONDS_IN_DAY = SECONDS_IN_DAY * 1000l; /** * Private constructor because this is a pure utility class. */ private TriggerUtils() { } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private static void validateDayOfWeek(int dayOfWeek) { if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) { throw new IllegalArgumentException("Invalid day of week."); } } private static void validateHour(int hour) { if (hour < 0 || hour > 23) { throw new IllegalArgumentException( "Invalid hour (must be >= 0 and <= 23)."); } } private static void validateMinute(int minute) { if (minute < 0 || minute > 59) { throw new IllegalArgumentException( "Invalid minute (must be >= 0 and <= 59)."); } } private static void validateSecond(int second) { if (second < 0 || second > 59) { throw new IllegalArgumentException( "Invalid second (must be >= 0 and <= 59)."); } } private static void validateDayOfMonth(int day) { if ((day < 1 || day > 31) && day != LAST_DAY_OF_MONTH) { throw new IllegalArgumentException("Invalid day of month."); } } private static void validateMonth(int month) { if (month < 1 || month > 12) { throw new IllegalArgumentException( "Invalid month (must be >= 1 and <= 12."); } } private static void validateYear(int year) { if (year < 1970 || year > 2099) { throw new IllegalArgumentException( "Invalid year (must be >= 1970 and <= 2099."); } } /** * <p> * Set the given <code>Trigger</code>'s name to the given value, and its * group to the default group (<code>Scheduler.DEFAULT_GROUP</code>). * </p> * * @deprecated use org.quartz.TriggerUtils instead! * */ public static void setTriggerIdentity(Trigger trig, String name) { trig.setName(name); trig.setGroup(Scheduler.DEFAULT_GROUP); } /** * <p> * Set the given <code>Trigger</code>'s name to the given value, and its * group to the given group. * </p> * * @deprecated use org.quartz.TriggerUtils instead! * */ public static void setTriggerIdentity(Trigger trig, String name, String group) { trig.setName(name); trig.setGroup(group); } /** * <p> * Make a trigger that will fire every day at the given time. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set. * </p> * * @param hour * the hour (0-23) upon which to fire * @param minute * the minute (0-59) upon which to fire * * @deprecated use org.quartz.TriggerUtils instead! * */ public static Trigger makeDailyTrigger(int hour, int minute) { validateHour(hour); validateMinute(minute); CronTrigger trig = new CronTrigger(); try { trig.setCronExpression("0 " + minute + " " + hour + " ? * *"); } catch (Exception ignore) { return null; /* never happens... */ } return trig; } /** * <p> * Make a trigger that will fire every day at the given time. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set. * </p> * * @param dayOfWeek * (1-7) the day of week upon which to fire * @param hour * the hour (0-23) upon which to fire * @param minute * the minute (0-59) upon which to fire * * @see #SUNDAY * @see #MONDAY * @see #TUESDAY * @see #WEDNESDAY * @see #THURSDAY * @see #FRIDAY * @see #SATURDAY * * @deprecated use org.quartz.TriggerUtils instead! * */ public static Trigger makeWeeklyTrigger(int dayOfWeek, int hour, int minute) { validateDayOfWeek(dayOfWeek); validateHour(hour); validateMinute(minute); CronTrigger trig = new CronTrigger(); try { trig.setCronExpression("0 " + minute + " " + hour + " ? * " + dayOfWeek); } catch (Exception ignore) { return null; /* never happens... */ } return trig; } /** * <p> * Make a trigger that will fire every day at the given time. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set. * </p> * * <p> * If the day of the month specified does not occur in a given month, a * firing will not occur that month. (i.e. if dayOfMonth is specified as * 31, no firing will occur in the months of the year with fewer than 31 * days). * </p> * * @param dayOfMonth * (1-31, or -1) the day of week upon which to fire * @param hour * the hour (0-23) upon which to fire * @param minute * the minute (0-59) upon which to fire * * @deprecated use org.quartz.TriggerUtils instead! * */ public static Trigger makeMonthlyTrigger(int dayOfMonth, int hour, int minute) { validateDayOfMonth(dayOfMonth); validateHour(hour); validateMinute(minute); CronTrigger trig = new CronTrigger(); try { if (dayOfMonth != LAST_DAY_OF_MONTH) { trig.setCronExpression("0 " + minute + " " + hour + " " + dayOfMonth + " * ?"); } else { trig.setCronExpression("0 " + minute + " " + hour + " L * ?"); } } catch (Exception ignore) { return null; /* never happens... */ } return trig; } /* * <p> Make a trigger that will fire every N days at the given time. </p> * * <p> The generated trigger will still need to have its name, group, * start-time and end-time set. </p> * * @param hour the hour (0-23) upon which to fire @param minute the minute * (0-59) upon which to fire @param interval the number of days between * firings public static Trigger makeDailyTrigger(int interval, int hour, * int minute) { * * SimpleTrigger trig = new SimpleTrigger(); * * MILLISECONDS_IN_DAY); * trig.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); * * return trig; * } */ /** * <p> * Make a trigger that will fire every second, indefinitely. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set. * </p> * * * @deprecated use org.quartz.TriggerUtils instead! * */ public static Trigger makeSecondlyTrigger() { return makeSecondlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); } /** * <p> * Make a trigger that will fire every N seconds, indefinitely. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set. * </p> * * @param intervalInSeconds * the number of seconds between firings * * @deprecated use org.quartz.TriggerUtils instead! * */ public static Trigger makeSecondlyTrigger(int intervalInSeconds) { return makeSecondlyTrigger(intervalInSeconds, SimpleTrigger.REPEAT_INDEFINITELY); } /** * <p> * Make a trigger that will fire every N seconds, with the given number of * repeats. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set. * </p> * * @param intervalInSeconds * the number of seconds between firings * @param repeatCount * the number of times to repeat the firing * * @deprecated use org.quartz.TriggerUtils instead! * */ public static Trigger makeSecondlyTrigger(int intervalInSeconds, int repeatCount) { SimpleTrigger trig = new SimpleTrigger(); trig.setRepeatInterval(intervalInSeconds * 1000l); trig.setRepeatCount(repeatCount); return trig; } /** * <p> * Make a trigger that will fire every minute, indefinitely. * </p> * * <p> * The generated trigger will still need to have its name, group, * start-time and end-time set.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -