[Java]代码
01 | GenericKeyedObjectPool objectPool = new GenericKeyedObjectPool( new KeyedPoolableObjectFactory() { |
02 |
03 | @Override |
04 | public boolean validateObject(Object key, Object obj) { |
05 | return obj != null && (key.equals(JSONParser. class ) || key.equals(XmlRpcClient. class )); |
06 | } |
07 |
08 | @Override |
09 | public void passivateObject(Object key, Object obj) throws Exception { |
10 | if (key.equals(JSONParser. class )) { |
11 | ((JSONParser) obj).reset(); |
12 | } |
13 | } |
14 |
15 | @Override |
16 | public Object makeObject(Object key) throws Exception { |
17 | if (key.equals(JSONParser. class )) |
18 | return new JSONParser(); |
19 | if (key.equals(XmlRpcClient. class )) { |
20 | XmlRpcClient client = new XmlRpcClient(); |
21 | return client; |
22 | } |
23 | throw new IllegalArgumentException( "unknown key type for KeyObjectPool!" ); |
24 | } |
25 |
26 | @Override |
27 | public void destroyObject(Object key, Object obj) throws Exception { |
28 | } |
29 |
30 | @Override |
31 | public void activateObject(Object key, Object obj) throws Exception { |
32 | } |
33 | }, poolSize, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, - 1 ); |