?? 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;import java.util.Calendar;import java.util.Date;import java.util.LinkedList;import java.util.List;import java.util.TimeZone;/** * <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 * * @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; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 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> * * @param trig the tigger to change name to * @param name the new trigger name */ public static void setTriggerIdentity(Trigger trig, String name) { setTriggerIdentity(trig, name, Scheduler.DEFAULT_GROUP); } /** * <p> * Set the given <code>Trigger</code>'s name to the given value, and its * group to the given group. * </p> * * @param trig the tigger to change name to * @param name the new trigger name * @param group the new trigger group */ 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 not have its name, group, * or end-time set. The Start time defaults to 'now'. * </p> * * @param hour the hour (0-23) upon which to fire * @param minute the minute (0-59) upon which to fire * @return the new trigger */ 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... */ } trig.setStartTime(new Date()); return trig; } /** * <p> * Make a trigger that will fire every day at the given time. * </p> * * <p> * The generated trigger will not have its group or end-time set. * The Start time defaults to 'now'. * </p> * * @param trigName the trigger's name * @param hour the hour (0-23) upon which to fire * @param minute the minute (0-59) upon which to fire * @return the newly created trigger */ public static Trigger makeDailyTrigger( String trigName, int hour, int minute) { Trigger trig = makeDailyTrigger(hour, minute); trig.setName(trigName); return trig; } /** * <p> * Make a trigger that will fire every week at the given day and time. * </p> * * <p> * The generated trigger will not have its name, group, * or end-time set. The Start time defaults to 'now'. * </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 * @return the new trigger * * @see #SUNDAY * @see #MONDAY * @see #TUESDAY * @see #WEDNESDAY * @see #THURSDAY * @see #FRIDAY * @see #SATURDAY */ 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... */ } trig.setStartTime(new Date()); return trig; } /** * <p> * Make a trigger that will fire every week at the given day and time. * </p> * * <p> * The generated trigger will not have its group, * or end-time set. The Start time defaults to 'now'. * </p> * * @param trigName the trigger's name * @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 * @return the newly created trigger * * @see #SUNDAY * @see #MONDAY * @see #TUESDAY * @see #WEDNESDAY * @see #THURSDAY * @see #FRIDAY * @see #SATURDAY */ public static Trigger makeWeeklyTrigger( String trigName, int dayOfWeek, int hour, int minute) { Trigger trig = makeWeeklyTrigger(dayOfWeek, hour, minute); trig.setName(trigName); return trig; } /** * <p> * Make a trigger that will fire every month at the given day and time. * </p> * * <p> * The generated trigger will not have its name, group, * or end-time set. The Start time defaults to 'now'. * </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 * @return the newly created trigger */ 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... */ } trig.setStartTime(new Date()); return trig; } /** * <p> * Make a trigger that will fire every month at the given day and time. * </p> * * <p> * The generated trigger will not have its group, * or end-time set. The Start time defaults to 'now'. * </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 trigName the trigger's name * @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 * @return the newly created trigger */ public static Trigger makeMonthlyTrigger( String trigName, int dayOfMonth, int hour, int minute) { Trigger trig = makeMonthlyTrigger(dayOfMonth, hour, minute); trig.setName(trigName); return trig; } /* * <p> Make a trigger that will fire every N days at the given time. </p> * * <p> TThe generated trigger will not 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 <code>repeatCount</code> times, waiting * <code>repeatInterval</code> milliseconds between each fire. * </p> * * <p> * The generated trigger will not have its name, group, * or end-time set. The Start time defaults to 'now'. * </p> * * @param repeatCount the number of times to fire the trigger * @param repeatInterval the number of milliseconds to wait between fires * @return the newly created trigger */ public static Trigger makeImmediateTrigger( int repeatCount, long repeatInterval) { SimpleTrigger trig = new SimpleTrigger(); trig.setStartTime( new Date() ); trig.setRepeatCount(repeatCount); trig.setRepeatInterval(repeatInterval); return trig; } /** * <p> * Make a trigger that will fire <code>repeatCount</code> times, waiting * <code>repeatInterval</code> milliseconds between each fire. * </p> * * <p> * The generated trigger will not have its name, group, * or end-time set. The Start time defaults to 'now'. * </p> * * @param trigName the trigger's name * @param repeatCount the number of times to fire the trigger * @param repeatInterval the number of milliseconds to wait between fires * @return the new trigger */ public static Trigger makeImmediateTrigger( String trigName, int repeatCount, long repeatInterval) { Trigger trig = makeImmediateTrigger(repeatCount, repeatInterval); trig.setName(trigName); return trig; } /** * <p> * Make a trigger that will fire every second, indefinitely. * </p> * * <p> * The generated trigger will not have its name, group, * or end-time set. The Start time defaults to 'now'. * </p> * @return the new trigger */ public static Trigger makeSecondlyTrigger() { return makeSecondlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); } /** * <p> * Make a trigger that will fire every second, indefinitely. * </p> * * <p> * The generated trigger will not have its group, * or end-time set. The Start time defaults to 'now'. * </p> * * @param trigName the trigger's name * @return the new trigger */ public static Trigger makeSecondlyTrigger(String trigName) { return makeSecondlyTrigger(
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -