When using apache common pool, it can provide me a good implementation of Java object pooling. However their KeyedPoolableObjectFactory interface is not type safe. Is there any object pool library in Java that can provide a generics interface for us to create a type safe object pool?
Asked
Active
Viewed 1,002 times
4

palacsint
- 28,416
- 10
- 82
- 109

raymond.mh.ng
- 343
- 2
- 3
- 21
-
Sure you really need an object pool? In which context are you going to use it? – helpermethod Jun 07 '11 at 09:51
-
To hold a list of client information and a list of topic listener of AMQ. Hopefully, the client entry can be evicted after idle a period of time. – raymond.mh.ng Jun 07 '11 at 10:10
2 Answers
2
Not as far as I know, but it's easy enough to create a typed wrapper around the commons-pool implementation, so your calling code can use generics.

artbristol
- 32,010
- 5
- 70
- 103
-
Is is not so easy. For example, the following is not possible. public class X
implements GenericKeyedObjectPool { @Override public void activateObject(final Object arg0, final Object arg1) throws Exception { if (arg0 instanceof T) .... => error } – raymond.mh.ng Jun 07 '11 at 10:16 -
Or I can only use this, but it is not so good for creating the required object. public class X
implements KeyedPoolableObjectFactory { private Class – raymond.mh.ng Jun 07 '11 at 10:22type; public static X create(Class type) { return new X (type); } -
1@raymond Why artbristol means it that you can create a class which holds an instance of `KeyedObjectPool`. Then you are free to define your own API, delegating calls to the referenced `KeyedObjectPool`. See http://de.wikipedia.org/wiki/Adapter_%28Entwurfsmuster%29 for more details on how to implement this. – helpermethod Jun 07 '11 at 11:33
-
Thanks artbristol and Helper Method. It seems that I have no other choice at this moment but to implement a wrapper or adaptor to achieve this. :) – raymond.mh.ng Jun 08 '11 at 02:22