tags:

views:

868

answers:

5

Based on an answer I got here, I started to give commons-pool a serious look. My last experience of using it was around 2003, probably version 1.1 or 1.2. Its main user, DBCP, is considered by many as flawed and to be avoided.

Does anyone uses commons pool in production to write pool of your own? What is the best pool type to use? I plan to store client TCP sockets in it.

Is there another generic pool that replaces it?

A: 

Check out MultiThreadedHttpConnectionManager - it's an Apache Commons HttpClient connection pool manager that will probably fit your need right out of the box.

Gandalf
As I wrote, I need the pool to contain TCP sockets, not HTTP connections.
David Rabinowitz
A: 

First don't use commons-pool 1.3, it has some major issues with multi threaded applications.

Second, Java 5 concurency package has decent pool implementations (see sample here)

Ehrann Mehdan
Does c3p0 let you use its pool without the JDBC stuff?
David Rabinowitz
I stand corrected, removed c3p0, but my 1.3 commons-pool remark is priceless, it took me ages to find it in production. performance jumped substrantially after upgrading to 1.4...
Ehrann Mehdan
We are using 1.5... Java 5 indeed has a nice *Thread* Pool but we were looking for an *Object* Pool
David Rabinowitz
A: 

You should check that the instantation costs more or the fetching from the pool. Because the only valid situation to use the pool is the first.

KARASZI István
I cannot instantiate new objects, as the objects are client TCP sockets that need to be alive as long as possible. Closing and opening connections is considered to be DOS attack.
David Rabinowitz
FYI: we are using commons-pool's GenericObjectPool in a different area but it works very well.
KARASZI István
A: 

Have you looked into Netty or Apache MINA? They will both keep track of your TCP connections and should make implementing whatever communications protocol those TCP sockets will use easier as well.

Stefan L
As far as I know they are used to implement servers, not clients. Can I use them to implement CLIENT tcp pools?
David Rabinowitz
Yes. Both Netty and MINA allow you to write a client, too.
Trustin Lee
+3  A: 

Does anyone uses commons pool in production to write pool of your own?

Yes, I do and the pool holds TCP connections, like you intend it to. It's wired up via Spring, so assuming you understand Spring configuration:

<bean class="com.company.ConnectionSupplier">
<constructor-arg>
  <!-- The ConnectionSupplier wraps an object pool -->
  <bean class="org.apache.commons.pool.impl.GenericObjectPool">
    <constructor-arg>
       <!-- The ObjectPool uses a ConnectionFactory to build new connections -->
       <bean class="com.company.ConnectionFactory">
         <constructor-arg value="server" />
         <constructor-arg value="3000" />  
       </bean>  
    </constructor-arg>
    <property name="maxActive" value="20" />
    <property name="testOnBorrow" value="true" />
  </bean>
</constructor-arg>
</bean>  

The ConnectionFactory extends BasePoolableObjectFactory and is a small wrapper around a SocketFactory.

@First comment: The ConnectionFactory constructor takes a server and a port. In the overriden makeObject(), it creates sockets that connect to that server and port. It returns 'Connection' objects that wrap the created socket with some convenience methods for communicating through the socket.

The connection is tested using a sort of 'ping' or 'echo' provided by the protocol used to communicate over the socket. Should that not have been available, validation/testing of the connection is not really possible, except for asking the socket whether it has been closed. In that case, a Connection in the pool would have been invalidated if it threw an exception and every method using Connections should be prepared for that kind of failure and attempt the same operation with another connection.

Confusion
How have you implemented the connection factory? How do you test the connection for being alive?
David Rabinowitz