KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > pool > SimpleObjectPool


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.pool;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class SimpleObjectPool extends AbstractObjectPool {
19     //已从池中取出的对象数目
20
private volatile int numWorking = 0;
21     private List JavaDoc pool = Collections.synchronizedList(new ArrayList JavaDoc());
22
23     /**
24      * the number of "sleeping" instances in the pool.
25      */

26     private int maxRest = 10;
27
28     private int initNum = 0;
29
30     /**
31      * Poolable object will remove when it timeout
32      * if timeout occured, present thant system is not busy
33      */

34     private long timeout = 5 * 60 * 1000; // 5m
35

36     public SimpleObjectPool(ObjectFactory factory) {
37         this(factory, 0, 10);
38     }
39
40     public SimpleObjectPool(ObjectFactory factory, int initNum, int maxRest) {
41         this(factory, initNum, maxRest, 5 * 60 * 1000);
42     }
43
44     public SimpleObjectPool(ObjectFactory factory, int initNum, int maxRest, long timeout) {
45         super(factory);
46         this.factory = factory;
47         this.initNum = initNum;
48         this.maxRest = maxRest;
49         this.timeout = timeout;
50     }
51
52     public SimpleObjectPool(Class JavaDoc factoryClass, Class JavaDoc poolableClass) {
53         this(factoryClass, poolableClass, 0, 10);
54     }
55
56     public SimpleObjectPool(Class JavaDoc factoryClass, Class JavaDoc poolableClass, int initNum, int maxRest) {
57         this(factoryClass, poolableClass, initNum, maxRest, 5 * 60 * 1000);
58     }
59
60     public SimpleObjectPool(Class JavaDoc factoryClass, Class JavaDoc poolableClass, int initNum, int maxRest, long timeout) {
61         super(factoryClass, poolableClass);
62         this.initNum = initNum;
63         this.maxRest = maxRest;
64         this.timeout = timeout;
65     }
66
67     /**
68      * retrieve Object from object pool
69      *
70      * @return
71      */

72     public synchronized PoolableObject retrieveObject() throws Exception JavaDoc {
73         logger.debug("retrieve object, pool size is " + pool.size() + ", max rest is " + maxRest);
74         PoolableObject obj = null;
75         synchronized(pool) {
76             while(pool.size() > 0) {
77                 obj = (PoolableObject) pool.remove(0);
78                 if(!pool.isEmpty()) {
79                     // check timeout, get the first poolable object not timeout
80
if(obj.getSleepTime() > timeout) {
81                         factory.destroyObject(obj);
82                         obj = null;
83                     }
84                     else {
85                         // just return the last one whether it timeout or not
86
break;
87                     }
88                 }
89             }
90 /*
91       if(pool.size() > 0){
92         obj = (PoolableObject)pool.remove(0);
93       }
94 */

95         }
96         if(obj == null) { // 池中已经没有了
97
obj = factory.makeObject();
98         }
99         if(obj != null) {
100             obj.activate();
101         }
102         numWorking++;
103         return obj;
104     }
105
106     /**
107      * restore the retrived object to object pool
108      *
109      * @return true if object passivate && push success, false if failed
110      */

111     public boolean restoreObject(PoolableObject obj) {
112         logger.debug("restore object " + obj + ", pool size " + pool.size());
113         numWorking--;
114         PoolableObject realObject = obj;
115
116 // Proxy PoolableObject 也支持 destory 等操作,所以不用特殊对待
117
// if(Proxy.isProxyClass(obj.getClass())) {
118
// realObject = ((PoolableObjectInvocationHandler)Proxy.getInvocationHandler(obj)).getPoolableObject();
119
// }
120

121         if(factory.validateObject(realObject)) {
122             try {
123                 obj.passivate();
124                 synchronized(pool) {
125                     // 未达初始值或者系统忙且还没有达到上限,返回池
126
// if(pool.size() < initNum || (pool.size() < maxRest && numWorking > 0)) {
127
if(pool.size() < maxRest) {
128                         pool.add(realObject);
129                         return true;
130                     }
131                     else {
132                         factory.destroyObject(realObject);
133                         return false;
134                     }
135                 }
136             }
137             catch(Exception JavaDoc e) {
138                 try {
139                     factory.destroyObject(realObject);
140                 }
141                 catch(Exception JavaDoc ex) {
142                     logger.warn(e);
143                 }
144                 return false;
145                 // do nothing , just jump to below
146
}
147         }
148         else {
149             try {
150                 factory.destroyObject(realObject);
151             }
152             catch(Exception JavaDoc e) {
153                 logger.warn(e.getMessage(), e);
154             }
155             return false;
156         }
157     }
158
159     public boolean removeObject(PoolableObject obj) {
160         logger.debug("remove object " + obj);
161         return pool.remove(obj);
162     }
163
164     // clear the pool
165
public synchronized void clear() {
166         logger.debug("clear object pool");
167         Iterator JavaDoc iter = pool.iterator();
168         while(iter.hasNext()) {
169             try {
170                 factory.destroyObject((PoolableObject) iter.next());
171             }
172             catch(Exception JavaDoc e) {
173                 logger.warn(e);
174             }
175         }
176         synchronized(pool) {
177             pool.clear();
178         }
179     }
180
181     public int getWorking() {
182         return numWorking;
183     }
184
185     public int getRest() {
186         return pool.size();
187     }
188
189     protected void doInit() throws Exception JavaDoc {
190         logger.debug("make " + initNum + " poolable object");
191         int i = initNum;
192         while(i-- > 0) {
193             try {
194                 pool.add(factory.makeObject());
195             }
196             catch(Exception JavaDoc e) {
197                 throw new RuntimeException JavaDoc(e);
198             }
199         }
200     }
201
202     // just do clear
203
protected void doDestroy() throws Exception JavaDoc {
204         this.clear();
205     }
206
207     public int getInitNum() {
208         return initNum;
209     }
210
211     public int getMaxRest() {
212         return maxRest;
213     }
214
215     public static void main(String JavaDoc[] args) {
216
217     }
218 }
Popular Tags