KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > resources > CronResource


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.resources;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.CronType;
34 import com.caucho.jca.AbstractResourceAdapter;
35 import com.caucho.log.Log;
36 import com.caucho.util.Alarm;
37 import com.caucho.util.AlarmListener;
38 import com.caucho.util.L10N;
39
40 import javax.annotation.PostConstruct;
41 import javax.annotation.Resource;
42 import javax.resource.spi.BootstrapContext JavaDoc;
43 import javax.resource.spi.work.Work JavaDoc;
44 import javax.resource.spi.work.WorkManager JavaDoc;
45 import java.util.concurrent.Executor JavaDoc;
46 import java.util.logging.Level JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * The cron resources starts application Work tasks at cron-specified
51  * intervals.
52  */

53 public class CronResource extends AbstractResourceAdapter
54   implements AlarmListener
55 {
56   private static final L10N L = new L10N(CronResource.class);
57   private static final Logger JavaDoc log = Log.open(CronResource.class);
58
59   @Resource
60   private Executor JavaDoc _threadPool;
61
62   private ClassLoader JavaDoc _loader;
63   
64   private CronType _cron;
65   
66   private Runnable JavaDoc _work;
67   
68   private WorkManager JavaDoc _workManager;
69   private Alarm _alarm;
70
71   private volatile boolean _isActive;
72
73   /**
74    * Constructor.
75    */

76   public CronResource()
77   {
78     _loader = Thread.currentThread().getContextClassLoader();
79   }
80
81   /**
82    * Sets the cron interval.
83    */

84   public void setCron(CronType cron)
85   {
86     _cron = cron;
87   }
88
89   /**
90    * Sets the work task.
91    */

92   public void setWork(Runnable JavaDoc work)
93   {
94     _work = work;
95   }
96
97   /**
98    * Initialization.
99    */

100   @PostConstruct
101   public void init()
102     throws ConfigException
103   {
104     if (_cron == null)
105       throw new ConfigException(L.l("CronResource needs a <cron> interval."));
106     
107     if (_work == null)
108       throw new ConfigException(L.l("CronResource needs a <work> task."));
109   }
110
111   /**
112    * Starting.
113    */

114   public void start(BootstrapContext JavaDoc ctx)
115   {
116     _workManager = ctx.getWorkManager();
117
118     long now = Alarm.getCurrentTime();
119     
120     long nextTime = _cron.nextTime(now);
121
122     _isActive = true;
123
124     _alarm = new Alarm("cron-resource", this, nextTime - now);
125   }
126
127   /**
128    * The runnable.
129    */

130   public void handleAlarm(Alarm alarm)
131   {
132     if (! _isActive)
133       return;
134
135     Thread JavaDoc thread = Thread.currentThread();
136     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
137     try {
138       thread.setContextClassLoader(_loader);
139       
140       log.fine("cron work scheduled: " + _work);
141
142       if (_work instanceof Work JavaDoc)
143     _workManager.scheduleWork((Work JavaDoc) _work);
144       else
145     _threadPool.execute(_work);
146     } catch (Throwable JavaDoc e) {
147       log.log(Level.WARNING, e.toString(), e);
148     } finally {
149       thread.setContextClassLoader(oldLoader);
150     }
151
152     long now = Alarm.getCurrentTime();
153     long nextTime = _cron.nextTime(now);
154
155     _alarm.queue(nextTime - now);
156   }
157
158   /**
159    * Stopping.
160    */

161   public void stop()
162   {
163     _isActive = false;
164     _alarm.dequeue();
165   }
166 }
167
Popular Tags