001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.resources;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.types.CronType;
034: import com.caucho.jca.AbstractResourceAdapter;
035: import com.caucho.log.Log;
036: import com.caucho.util.Alarm;
037: import com.caucho.util.AlarmListener;
038: import com.caucho.util.L10N;
039:
040: import javax.annotation.PostConstruct;
041: import javax.annotation.Resource;
042: import javax.resource.spi.BootstrapContext;
043: import javax.resource.spi.work.Work;
044: import javax.resource.spi.work.WorkManager;
045: import java.util.concurrent.Executor;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: /**
050: * The cron resources starts application Work tasks at cron-specified
051: * intervals.
052: */
053: public class CronResource extends AbstractResourceAdapter implements
054: AlarmListener {
055: private static final L10N L = new L10N(CronResource.class);
056: private static final Logger log = Log.open(CronResource.class);
057:
058: @Resource
059: private Executor _threadPool;
060:
061: private ClassLoader _loader;
062:
063: private CronType _cron;
064:
065: private Runnable _work;
066:
067: private WorkManager _workManager;
068: private Alarm _alarm;
069:
070: private volatile boolean _isActive;
071:
072: /**
073: * Constructor.
074: */
075: public CronResource() {
076: _loader = Thread.currentThread().getContextClassLoader();
077: }
078:
079: /**
080: * Sets the cron interval.
081: */
082: public void setCron(CronType cron) {
083: _cron = cron;
084: }
085:
086: /**
087: * Sets the work task.
088: */
089: public void setWork(Runnable work) {
090: _work = work;
091: }
092:
093: /**
094: * Initialization.
095: */
096: @PostConstruct
097: public void init() throws ConfigException {
098: if (_cron == null)
099: throw new ConfigException(L
100: .l("CronResource needs a <cron> interval."));
101:
102: if (_work == null)
103: throw new ConfigException(L
104: .l("CronResource needs a <work> task."));
105: }
106:
107: /**
108: * Starting.
109: */
110: public void start(BootstrapContext ctx) {
111: _workManager = ctx.getWorkManager();
112:
113: long now = Alarm.getCurrentTime();
114:
115: long nextTime = _cron.nextTime(now);
116:
117: _isActive = true;
118:
119: _alarm = new Alarm("cron-resource", this , nextTime - now);
120: }
121:
122: /**
123: * The runnable.
124: */
125: public void handleAlarm(Alarm alarm) {
126: if (!_isActive)
127: return;
128:
129: Thread thread = Thread.currentThread();
130: ClassLoader oldLoader = thread.getContextClassLoader();
131: try {
132: thread.setContextClassLoader(_loader);
133:
134: log.fine("cron work scheduled: " + _work);
135:
136: if (_work instanceof Work)
137: _workManager.scheduleWork((Work) _work);
138: else
139: _threadPool.execute(_work);
140: } catch (Throwable e) {
141: log.log(Level.WARNING, e.toString(), e);
142: } finally {
143: thread.setContextClassLoader(oldLoader);
144: }
145:
146: long now = Alarm.getCurrentTime();
147: long nextTime = _cron.nextTime(now);
148:
149: _alarm.queue(nextTime - now);
150: }
151:
152: /**
153: * Stopping.
154: */
155: public void stop() {
156: _isActive = false;
157: _alarm.dequeue();
158: }
159: }
|