001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.job;
022:
023: import com.liferay.util.Time;
024:
025: import java.util.Date;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import org.quartz.JobDetail;
031: import org.quartz.Scheduler;
032: import org.quartz.SchedulerException;
033: import org.quartz.SimpleTrigger;
034: import org.quartz.Trigger;
035: import org.quartz.impl.StdSchedulerFactory;
036:
037: /**
038: * <a href="JobScheduler.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class JobScheduler {
044:
045: public static void schedule(IntervalJob intervalJob)
046: throws SchedulerException {
047:
048: Date startTime = new Date(System.currentTimeMillis()
049: + Time.MINUTE * 3);
050: Date endTime = null;
051:
052: JobDetail jobDetail = new JobDetail(intervalJob.getClass()
053: .getName(), Scheduler.DEFAULT_GROUP, intervalJob
054: .getClass());
055:
056: Trigger trigger = new SimpleTrigger(intervalJob.getClass()
057: .getName()
058: + "_TRIGGER", Scheduler.DEFAULT_GROUP, startTime,
059: endTime, SimpleTrigger.REPEAT_INDEFINITELY, intervalJob
060: .getInterval());
061:
062: scheduleJob(jobDetail, trigger);
063: }
064:
065: public static void scheduleJob(JobDetail jobDetail, Trigger trigger)
066: throws SchedulerException {
067:
068: _getScheduler().scheduleJob(jobDetail, trigger);
069: }
070:
071: public static void scheduleJob(Trigger trigger)
072: throws SchedulerException {
073: _getScheduler().scheduleJob(trigger);
074: }
075:
076: public static void shutdown() {
077: _instance._shutdown();
078: }
079:
080: public static void triggerJob(String jobName, String groupName)
081: throws SchedulerException {
082:
083: _getScheduler().triggerJob(jobName, groupName);
084: }
085:
086: public static void unscheduleJob(String className)
087: throws SchedulerException {
088:
089: unscheduleJob(className + "_TRIGGER", Scheduler.DEFAULT_GROUP);
090: }
091:
092: public static void unscheduleJob(String triggerName,
093: String groupName) throws SchedulerException {
094:
095: _getScheduler().unscheduleJob(triggerName, groupName);
096: }
097:
098: private static Scheduler _getScheduler() {
099: return _instance._scheduler;
100: }
101:
102: private JobScheduler() {
103: _start();
104: }
105:
106: private void _start() {
107: StdSchedulerFactory sf = new StdSchedulerFactory();
108:
109: try {
110: _scheduler = sf.getScheduler();
111:
112: _scheduler.start();
113: } catch (SchedulerException se) {
114: _log.error(se);
115: }
116: }
117:
118: private void _shutdown() {
119: try {
120: if (!_scheduler.isShutdown()) {
121: _scheduler.shutdown();
122: }
123: } catch (SchedulerException se) {
124: _log.error(se);
125: }
126: }
127:
128: private static Log _log = LogFactory.getLog(JobScheduler.class);
129:
130: private static JobScheduler _instance = new JobScheduler();
131:
132: private Scheduler _scheduler;
133:
134: }
|