KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > work > CommonWorkManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.connectors.work;
25
26 import com.sun.logging.LogDomains;
27 import com.sun.corba.ee.spi.orbutil.threadpool.*;
28 import com.sun.enterprise.connectors.ConnectorRuntimeException;
29 import com.sun.enterprise.connectors.ConnectorRuntime;
30 import com.sun.enterprise.connectors.work.monitor.*;
31 import com.sun.enterprise.util.S1ASThreadPoolManager;
32 import java.util.logging.*;
33 import javax.resource.spi.work.ExecutionContext JavaDoc;
34 import javax.resource.spi.work.Work JavaDoc;
35 import javax.resource.spi.work.WorkException JavaDoc;
36 import javax.resource.spi.work.WorkListener JavaDoc;
37 import javax.resource.spi.work.WorkManager JavaDoc;
38 import com.sun.enterprise.util.i18n.StringManager;
39
40 /**
41  * WorkManager implementation.
42  *
43  * @author Binod P.G
44  */

45
46 public class CommonWorkManager implements MonitorableWorkManager {
47
48     private static WorkManager JavaDoc wm = null;
49
50     private ThreadPoolManager tpm;
51     private ThreadPool tp;
52
53     private static Logger logger =
54     LogDomains.getLogger(LogDomains.RSR_LOGGER);
55
56     private boolean isMonitoringEnabled = false; //default = false;
57

58     private WorkStats workStats = null;
59
60     private StringManager localStrings = StringManager.getManager(
61                                             CommonWorkManager.class);
62     
63     /**
64      * Private constructor.
65      *
66      * @param threadpoolId Id of the thread pool.
67      * @throws ConnectorRuntimeException if thread pool is not accessible
68      */

69     public CommonWorkManager (String JavaDoc threadpoolId)
70                                throws ConnectorRuntimeException {
71         int env = ConnectorRuntime.getRuntime().getEnviron();
72
73         if (env == ConnectorRuntime.SERVER) {
74             tpm = S1ASThreadPoolManager.getThreadPoolManager();
75
76             if (threadpoolId == null) {
77                 tp = tpm.getDefaultThreadPool();
78             } else {
79                 try {
80                     tp = tpm.getThreadPool(threadpoolId);
81                     logger.info("Got the thread pool for :" + threadpoolId);
82                 } catch (NoSuchThreadPoolException e) {
83                     String JavaDoc msg = localStrings.getString("workmanager.threadpool_not_found");
84              
85                     logger.log(Level.SEVERE,msg, threadpoolId);
86                     throw new ConnectorRuntimeException(e.getMessage());
87                 }
88             }
89         }
90     }
91
92     /**
93      * Using the default thread pool.
94      *
95      * @throws ConnectorRuntimeException if thread pool is not accessible
96      */

97     public CommonWorkManager() throws ConnectorRuntimeException {
98         this(null);
99     }
100
101     /**
102      * Executes the work instance.
103      *
104      * @param work work instance from resource adapter
105      * @throws WorkException if there is an exception while executing work.
106      */

107     public void doWork (Work JavaDoc work)
108         throws WorkException JavaDoc {
109         doWork(work, -1, null, null);
110     }
111     
112     /**
113      * Executes the work instance. The calling thread will wait until the
114      * end of work execution.
115      *
116      * @param work work instance from resource adapter
117      * @param startTimeout Timeout for the work.
118      * @param execContext Execution context in which the work will be executed.
119      * @param workListener Listener from RA that will listen to work events.
120      * @throws WorkException if there is an exception while executing work.
121      */

122     public void doWork(Work JavaDoc work, long startTimeout,
123             ExecutionContext JavaDoc execContext, WorkListener JavaDoc workListener)
124     throws WorkException JavaDoc {
125         
126         if (logger.isLoggable(Level.FINEST)) {
127             String JavaDoc msg = "doWork for [" + work.toString() + "] START";
128             logger.log(Level.FINEST, debugMsg(msg));
129         }
130        
131         WorkCoordinator wc = new WorkCoordinator
132             (work, startTimeout, execContext, tp.getAnyWorkQueue(), workListener,
133                             this.workStats);
134         wc.submitWork(WorkCoordinator.WAIT_UNTIL_FINISH);
135         wc.lock();
136
137         WorkException JavaDoc we = wc.getException();
138         if (we != null) {
139             throw we;
140         }
141
142         if (logger.isLoggable(Level.FINEST)) {
143             String JavaDoc msg = "doWork for [" + work.toString() + "] END";
144             msg = "doWork for [" + work.toString() + "] END";
145             logger.log(Level.FINEST, debugMsg(msg));
146         }
147     }
148
149     /**
150      * Executes the work instance. The calling thread will wait until the
151      * start of work execution.
152      *
153      * @param work work instance from resource adapter
154      * @throws WorkException if there is an exception while executing work.
155      */

156     public long startWork(Work JavaDoc work) // startTimeout = INDEFINITE
157
throws WorkException JavaDoc {
158         //block the current application thread
159
//find a thread to run work
160
//notify the application thread when done
161

162         return startWork(work, -1, null, null);
163     }
164
165     /**
166      * Executes the work instance. The calling thread will wait until the
167      * start of work execution.
168      *
169      * @param work work instance from resource adapter
170      * @param startTimeout Timeout for the work.
171      * @param execContext Execution context in which the work will be executed.
172      * @param workListener Listener from RA that will listen to work events.
173      * @throws WorkException if there is an exception while executing work.
174      */

175     public long startWork(Work JavaDoc work, long startTimeout,
176             ExecutionContext JavaDoc execContext, WorkListener JavaDoc workListener)
177     throws WorkException JavaDoc {
178
179         if (logger.isLoggable(Level.FINEST)) {
180             String JavaDoc msg = "startWork for [" + work.toString() + "] START";
181             logger.log(Level.FINEST, debugMsg(msg));
182         }
183
184         long acceptanceTime = System.currentTimeMillis();
185
186         WorkCoordinator wc = new WorkCoordinator
187             (work, startTimeout, execContext, tp.getAnyWorkQueue(), workListener,
188                             this.workStats);
189         wc.submitWork(WorkCoordinator.WAIT_UNTIL_START);
190         wc.lock();
191
192         WorkException JavaDoc we = wc.getException();
193         if (we != null) {
194             throw we;
195         }
196
197         if (logger.isLoggable(Level.FINEST)) {
198             String JavaDoc msg = "startWork for [" + work.toString() + "] END";
199             logger.log(Level.FINEST, debugMsg(msg));
200         }
201         long startTime = System.currentTimeMillis();
202
203         return (startTime - acceptanceTime);
204     }
205
206     /**
207      * Executes the work instance. Calling thread will continue after scheduling
208      * the work
209      *
210      * @param work work instance from resource adapter
211      * @throws WorkException if there is an exception while executing work.
212      */

213     public void scheduleWork(Work JavaDoc work) // startTimeout = INDEFINITE
214
throws WorkException JavaDoc {
215         scheduleWork(work, -1, null, null);
216         return;
217     }
218
219     /**
220      * Executes the work instance. Calling thread will continue after scheduling
221      * the work
222      *
223      * @param work work instance from resource adapter
224      * @param startTimeout Timeout for the work.
225      * @param execContext Execution context in which the work will be executed.
226      * @param workListener Listener from RA that will listen to work events.
227      * @throws WorkException if there is an exception while executing work.
228      */

229     public void scheduleWork(Work JavaDoc work, long startTimeout,
230             ExecutionContext JavaDoc execContext, WorkListener JavaDoc workListener)
231     throws WorkException JavaDoc {
232
233         if (logger.isLoggable(Level.FINEST)) {
234             String JavaDoc msg = "scheduleWork for [" + work.toString() + "] START";
235             logger.log(Level.FINEST, debugMsg(msg));
236         }
237
238         WorkCoordinator wc = new WorkCoordinator
239             (work, startTimeout, execContext, tp.getAnyWorkQueue(), workListener,
240                             this.workStats);
241         wc.submitWork(WorkCoordinator.NO_WAIT);
242         wc.lock();
243
244         WorkException JavaDoc we = wc.getException();
245         if (we != null) {
246             throw we;
247         }
248
249         if (logger.isLoggable(Level.FINEST)) {
250             String JavaDoc msg = "scheduleWork for [" + work.toString() + "] END";
251             logger.log(Level.FINEST, debugMsg(msg));
252         }
253         return;
254     }
255
256     private String JavaDoc debugMsg (String JavaDoc message) {
257         String JavaDoc msg = "[Thread " + Thread.currentThread().getName()
258             + "] -- " + message;
259         return msg;
260     }
261
262     //SJSAS 8.1 Monitoring additions begins
263
public boolean isMonitoringEnabled() {
264         return this.isMonitoringEnabled;
265     }
266
267     public void setMonitoringEnabled(boolean isEnabled) {
268         this.isMonitoringEnabled = isEnabled;
269         if ( this.workStats == null ) {
270             this.workStats = new WorkStats();
271         }
272         //reset WorkStats when monitoring disabled
273
if (!isEnabled){
274             this.workStats.reset();
275         }
276     }
277
278     public long getWaitQueueLength(){
279         return (long)this.tp.getAnyWorkQueue().workItemsInQueue();
280     }
281     
282     public long getMaxWaitQueueLength() {
283         return this.workStats.maxWaitQueueLength;
284     }
285     
286     public long getMinWaitQueueLength() {
287         if (this.workStats.minWaitQueueLength != Long.MAX_VALUE){
288             return this.workStats.minWaitQueueLength;
289         } else {
290             return 0;
291         }
292     }
293     
294     public long getMaxWorkRequestWaitTime(){
295         return this.workStats.maxWorkRequestWaitTime;
296        
297     }
298     public long getMinWorkRequestWaitTime(){
299         return this.workStats.minWorkRequestWaitTime;
300     }
301
302     public long getSubmittedWorkCount() {
303         return this.workStats.submittedWorkCount;
304     }
305
306     public long getRejectedWorkCount() {
307         return this.workStats.rejectedWorkCount;
308     }
309
310     public long getCompletedWorkCount() {
311         return this.workStats.completedWorkCount;
312     }
313     
314     public long getCurrentActiveWorkCount() {
315         return this.workStats.currentActiveWorkCount;
316     }
317     public long getMaxActiveWorkCount() {
318         return this.workStats.maxActiveWorkCount;
319     }
320
321     public long getMinActiveWorkCount() {
322         if (this.workStats.minActiveWorkCount != Long.MAX_VALUE){
323             return this.workStats.minActiveWorkCount;
324         } else {
325             return 0;
326         }
327     }
328     //SJSAS 8.1 Monitoring additions end
329

330 }
331
332 /**
333  * A simple class that holds all statistics-related entries captured by the
334  * commonworkmanager together with the work-coordinator
335  *
336  * An instance of workStats is passed to the Work-Coordinator, during
337  * construction, so that the work-coordinator can update the stats of a
338  * work-manager
339  * @author Sivakumar Thyagarajan
340  */

341 class WorkStats {
342     long submittedWorkCount;
343     long completedWorkCount;
344     long rejectedWorkCount;
345     long maxWaitQueueLength;
346     long minWaitQueueLength;
347
348     long currentActiveWorkCount;
349     long minActiveWorkCount;
350     long maxActiveWorkCount;
351     
352     long maxWorkRequestWaitTime;
353     long minWorkRequestWaitTime;
354     long currWaitQueueLength;
355     
356     public void reset(){
357         this.submittedWorkCount = 0L;
358         this.rejectedWorkCount = 0L;
359         this.completedWorkCount = 0L;
360         
361         this.currWaitQueueLength = 0L;
362         this.maxWaitQueueLength = 0L;
363         this.minWaitQueueLength = Long.MAX_VALUE;
364
365         this.currentActiveWorkCount = 0L;
366         this.minActiveWorkCount = Long.MAX_VALUE;
367         this.maxActiveWorkCount= 0L;
368         
369         this.maxWorkRequestWaitTime = 0L;
370         this.minWorkRequestWaitTime = 0L;
371     }
372     
373     public synchronized void setWorkWaitTime(long waitTime){
374         //latch high
375
if (waitTime > maxWorkRequestWaitTime) {
376             this.maxWorkRequestWaitTime = waitTime;
377         }
378         
379         //latch low
380
if (waitTime < minWorkRequestWaitTime) {
381             this.minWorkRequestWaitTime = waitTime;
382         }
383     }
384     
385     public synchronized void incrementWaitQueueLength(){
386         setWaitQueueLength(this.currWaitQueueLength++);
387     }
388     
389     public synchronized void decrementWaitQueueLength(){
390         setWaitQueueLength(this.currWaitQueueLength--);
391     }
392
393     private void setWaitQueueLength(long waitQueueLength){
394         //latch high
395
if (waitQueueLength > maxWaitQueueLength) {
396             maxWaitQueueLength = waitQueueLength;
397         }
398         //latch low
399
if (waitQueueLength < minWaitQueueLength) {
400             minWaitQueueLength = waitQueueLength;
401         }
402     }
403     
404     public synchronized void setActiveWorkCount(long currentActiveWorkCount){
405         this.currentActiveWorkCount = currentActiveWorkCount;
406         //latch high
407
if (currentActiveWorkCount > maxActiveWorkCount) {
408             maxActiveWorkCount = currentActiveWorkCount;
409         }
410         //latch low
411
if (currentActiveWorkCount < minActiveWorkCount) {
412             minActiveWorkCount = currentActiveWorkCount;
413         }
414     }
415 }
416
Popular Tags