KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > PollingComponentSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.util;
18
19 import java.util.Date JavaDoc;
20
21 import javax.jbi.JBIException;
22 import javax.resource.spi.work.Work JavaDoc;
23 import javax.resource.spi.work.WorkManager JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.servicemix.components.varscheduler.ScheduleIterator;
28 import org.apache.servicemix.components.varscheduler.Scheduler;
29 import org.apache.servicemix.components.varscheduler.SchedulerTask;
30 import org.apache.servicemix.jbi.framework.ComponentContextImpl;
31
32 /**
33  * An implementation inheritence class for a component which polls some resource at periodic intervals to decide if
34  * there is an event to process.
35  *
36  * @version $Revision: 427469 $
37  */

38 public abstract class PollingComponentSupport extends ComponentSupport implements Work JavaDoc {
39     private static final Log log = LogFactory.getLog(PollingComponentSupport.class);
40     private WorkManager JavaDoc workManager;
41     private Scheduler scheduler;
42     private Date JavaDoc firstTime;
43     private long period = 5000;
44     private long delay;
45     private SchedulerTask schedulerTask;
46     private ScheduleIterator scheduleIterator;
47     private boolean started;
48     private boolean scheduleExecutedFlag;
49
50     /**
51      * Polls the underlying resource to see if some event is required
52      *
53      * @throws JBIException
54      */

55     public abstract void poll() throws Exception JavaDoc;
56     
57     public void release() {
58     }
59
60     public void run() {
61         try {
62             poll();
63         }
64         catch (Exception JavaDoc e) {
65             log.error("Caught exception while polling: " + e, e);
66         }
67     }
68
69     // Properties
70
// -------------------------------------------------------------------------
71
public WorkManager JavaDoc getWorkManager() {
72         return workManager;
73     }
74
75     public void setWorkManager(WorkManager JavaDoc workManager) {
76         this.workManager = workManager;
77     }
78
79     public long getDelay() {
80         return delay;
81     }
82
83     public void setDelay(long delay) {
84         this.delay = delay;
85     }
86
87     public Date JavaDoc getFirstTime() {
88         return firstTime;
89     }
90
91     public void setFirstTime(Date JavaDoc firstTime) {
92         this.firstTime = firstTime;
93     }
94
95     public long getPeriod() {
96         return period;
97     }
98
99     public void setPeriod(long period) {
100         this.period = period;
101     }
102
103     public Scheduler getScheduler() {
104         return scheduler;
105     }
106
107     public void setScheduler(Scheduler scheduler) {
108         this.scheduler = scheduler;
109     }
110     
111     public synchronized void start() throws JBIException {
112         if (!started) {
113             started = true;
114             if (schedulerTask != null) {
115                 schedulerTask.cancel();
116             }
117             schedulerTask = new PollSchedulerTask();
118             this.scheduler.schedule(schedulerTask,scheduleIterator);
119         }
120         super.start();
121     }
122
123     public synchronized void stop() throws JBIException {
124         if (schedulerTask != null) {
125             schedulerTask.cancel();
126             schedulerTask = null;
127         }
128         scheduleExecutedFlag = false;
129         started = false;
130         super.stop();
131     }
132
133     public synchronized void shutDown() throws JBIException {
134         stop();
135         scheduler.cancel();
136         scheduler = null;
137         scheduleIterator = null;
138         workManager = null;
139         super.shutDown();
140     }
141
142     // Implementation methods
143
// -------------------------------------------------------------------------
144
protected void init() throws JBIException {
145         if (scheduler == null) {
146             scheduler = new Scheduler(true);
147         }
148         if (scheduleIterator == null) {
149             scheduleIterator = new PollScheduleIterator();
150         }
151         if (workManager == null) {
152             ComponentContextImpl context = (ComponentContextImpl) getContext();
153             workManager = context.getWorkManager();
154         }
155         super.init();
156        
157     }
158     
159     private class PollSchedulerTask extends SchedulerTask {
160         public void run() {
161             try {
162                 // lets run the work inside the JCA worker pools to ensure
163
// the threads are setup correctly when we actually do stuff
164
getWorkManager().doWork(PollingComponentSupport.this);
165             }
166             catch (Throwable JavaDoc e) {
167                 log.error("Failed to schedule work: " + e, e);
168             }
169         }
170     }
171     
172     private class PollScheduleIterator implements ScheduleIterator {
173         public Date JavaDoc nextExecution() {
174             long nextTime = System.currentTimeMillis();
175             if (scheduleExecutedFlag) {
176                 nextTime += period;
177             } else {
178                 if (firstTime != null) {
179                     nextTime = firstTime.getTime();
180                 }
181                 nextTime += delay;
182                 scheduleExecutedFlag = true;
183             }
184             return (started) ? new Date JavaDoc(nextTime) : null;
185         }
186     }
187 }
Popular Tags