ronanl
ronanl

Reputation: 11

Configure Apache Commons Pool to create objects on start up

I'm configuring a pool of objects using apache commons pool2. It seems the objects in the pool are only created when an attempt is made to borrow an object. I'd like the objects to be created up front, so I have a minimum number of objects ready when the first one needs to be borrowed.

My spring configuration looks something like this:

<bean id="webSocketConnectionPool" class="org.apache.commons.pool2.impl.GenericObjectPool">
    <constructor-arg ref="webSocketConnectionFactory"/>
    <constructor-arg ref="webSocketConnectionPoolConfig"/>
</bean>

<bean id="webSocketConnectionFactory" class="com.blah.WebSocketConnectionFactory" />    

<bean id="webSocketConnectionPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
    <property name="maxIdle" value="300"/>
    <property name="maxTotal" value="1000"/>
    <property name="minIdle" value="10"/>
</bean>

I can see the pool is created when the app starts, but the minIdle setting doesn't seem to result in my desired behaviour. The create() method on the factory is only called when the first object is borrowed.

Any tips would be appreciated.

Thanks

Upvotes: 1

Views: 1932

Answers (1)

ronanl
ronanl

Reputation: 11

My solution was to add some logic to the init method of the class with the connection pool as a member, to add objects. Hopefully this will help someone else in the future.

connectionPool.addObjects(connectionPool.getMinIdle());

Upvotes: 0

Related Questions