KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > server > ThreadPoolInvokeStrategy


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

17
18 package org.apache.sandesha.server;
19
20 import org.apache.axis.components.logger.LogFactory;
21 import org.apache.axis.components.threadpool.ThreadPool;
22 import org.apache.commons.logging.Log;
23 import org.apache.sandesha.Constants;
24
25 import java.util.Collections JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * Use the Axis thread pool for handling web service invokes. Implementations
30  * of the <code>RMInvokerWork</code> are wrapped in a <code>Runnable</code>
31  * and passed into the pool.
32  *
33  * @author Patrick Collins
34  */

35
36 public class ThreadPoolInvokeStrategy implements InvokeStrategy {
37     private static final Log log = LogFactory.getLog(ThreadPoolInvokeStrategy.class.getName());
38     private Map JavaDoc params = Collections.EMPTY_MAP;
39     private ThreadPool tPool;
40
41     /**
42      * Creates the axis thread pool.
43      *
44      * @see org.apache.sandesha.server.InvokeStrategy#start()
45      */

46     public void start() {
47         final int numberOfThreads = getThreadPoolSize();
48         tPool = new ThreadPool(numberOfThreads);
49         for (int i = 0; i < numberOfThreads; i++) {
50             RMRunnableInvoker rmWorker = new RMRunnableInvoker(new RMInvokerWork());
51             tPool.addWorker(rmWorker);
52         }
53     }
54
55     /**
56      * @see org.apache.sandesha.server.InvokeStrategy#stop()
57      */

58     public void stop() {
59         tPool.shutdown();
60     }
61
62     /**
63      * Determine the size of the thread pool. Defaults to value defined
64      * in <code>Constants.INVOKER_THREADS</code> if none was explicitly set
65      * via config.
66      */

67     protected int getThreadPoolSize() {
68         int threadSize = Constants.INVOKER_THREADS;
69         String JavaDoc value = (String JavaDoc) getParams().get(Constants.THREAD_POOL_SIZE);
70         if (value != null && value.length() > 0) {
71             try {
72                 threadSize = Integer.parseInt(value);
73             } catch (NumberFormatException JavaDoc nfe) {
74                 // eat it
75
}
76         }
77         return threadSize;
78     }
79
80     /**
81      * @see org.apache.sandesha.server.InvokeStrategy#addParams(java.util.Map)
82      */

83     public void addParams(Map JavaDoc aParams) {
84         params = aParams;
85     }
86
87     protected Map JavaDoc getParams() {
88         return params;
89     }
90
91     /**
92      * A <code>Runnable</code> wrapper for embedding <code>RMInvokerWork</code>
93      * <p/>
94      * objects in their own threads.
95      */

96     protected class RMRunnableInvoker implements Runnable JavaDoc {
97         private RMInvokerWork rmInvokerWork;
98
99         public RMRunnableInvoker(RMInvokerWork aRMInvokerWork) {
100             rmInvokerWork = aRMInvokerWork;
101         }
102
103         public void run() {
104             while (true) {
105                 try {
106                     Thread.sleep(Constants.RMINVOKER_SLEEP_TIME);
107                     getRMInvokerWorker().executeInvoke();
108                 } catch (InterruptedException JavaDoc ex) {
109                     log.error(ex);
110                 } catch (Exception JavaDoc e) {
111                     log.error(e);
112                 }
113             }
114         }
115
116         /**
117          * @return Returns the rmInvokerWork.
118          */

119         protected RMInvokerWork getRMInvokerWorker() {
120             return rmInvokerWork;
121         }
122     }
123 }
124
Popular Tags