Third party lib for object pool with expiration time in Java
Asked Answered
A

3

5

I'm on a webservice server and I have objects with an internal connection.
Initializing this connection takes really long so my idea was to use an object pool to reuse the connections among different requests.

The objects are connected to each user, so i prefer to use the username as key and the connection as value. But I don't want to have the connection open forever. Maybe after a while it should be destroyed if the user does not start requests any more.

I thought about using the apache object pool but i didn't see expiration there (correct me if i'm wrong)

The ehcache offers me notifications about eviction and expiration, but not triggered after the timeout was over, only if the cached object was touched again.

Does someone know a lib which can do this job for me?

Arezzo answered 16/7, 2013 at 13:46 Comment(2)
code.google.com/p/guava-libraries/wiki/CachesExplained (see timed eviction)Kollwitz
yeah totally forgot guava caches :/ can you write that as answer? I think this is exactly what i was looking for.Arezzo
H
4

Take a look at http://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html

From the javadoc:

Optionally, one may configure the pool to examine and possibly evict objects
as they sit idle in the pool and to ensure that a minimum number of idle
objects are available. This is performed by an "idle object eviction" thread,
which runs asynchronously. Caution should be used when configuring this
optional feature. Eviction runs contend with client threads for access to
objects in the pool, so if they run too frequently performance issues may
result.

.... 

minEvictableIdleTimeMillis specifies the minimum amount of time that 
an object may sit idle in the pool before it is eligible for eviction
due to idle time. When non-positive, no object will be dropped from 
the pool due to idle time alone. This setting has no effect unless
timeBetweenEvictionRunsMillis > 0. The default setting for this 
parameter is 30 minutes.

Implement a PoolableObjectFactory that creates your connections and also implement the PoolableObjectFactory.destroyObject(T object) method to close your connection. This method will be invoked by the GenericObejctPool when an objects gets evicted.

Hindgut answered 16/7, 2013 at 13:59 Comment(3)
ah that's how they meant to do it, thanks! i think the guava way is better to useArezzo
@Arezzo From a conceptual point of view a cache is not an object pool. I also think that a cache is what fits to your requirements, because your connections are bound to a specific user and a pool contains objects that normally can be used by everyone. Of course you can create a pool for every user, but I guess this is not what you want. Therefore I also think that the guava cache is right. +1 for posting the guava example.Steinke
I totally agree with you, my requirement is kind of a mixture of both. thanks to the google guys! they always know what we want ;)Arezzo
A
4

Inspired by assylia's idea i used the guava way here my solution

final RemovalListener<Integer, Connection> removalListener = new RemovalListener<Integer, Connection>() {
    @Override
    public void onRemoval(final RemovalNotification<Integer, Connection> notification) {
        disconnect(notification.getValue());
    }
};

Cache<Integer, Connection> cache = CacheBuilder.newBuilder().maximumSize(20)
        .expireAfterAccess(30, TimeUnit.SECONDS)
        .removalListener(removalListener).build();
final ScheduledExecutorService cacheMaintanance = Executors.newSingleThreadScheduledExecutor();
cacheMaintanance.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        cache.cleanUp();
    }
}, 10, 10, TimeUnit.SECONDS);
Arezzo answered 16/7, 2013 at 14:44 Comment(0)
S
0

New Generic interface was added to this recently:

http://commons.apache.org/proper/commons-pool/

Sheriesherif answered 8/7, 2014 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.