Java中具有过期时间的对象池的第三方库

Sor*_*tur 5 java object-pooling

我在一个网络服务服务器上,我有一个内部连接的对象。
初始化这个连接需要很长时间,所以我的想法是使用对象池来重用不同请求之间的连接。

对象连接到每个用户,所以我更喜欢使用用户名作为键和连接作为值。但我不想永远打开连接。也许一段时间后,如果用户不再开始请求,它应该被销毁。

我想过使用apache 对象池,但我没有在那里看到过期(如果我错了,请纠正我)

ehcache 为我提供有关驱逐和到期的通知,但不会在超时结束后触发,仅当再次触及缓存对象时才触发。

有人知道可以为我完成这项工作的库吗?

Ren*_*ink 4

看看http://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html

来自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.
Run Code Online (Sandbox Code Playgroud)

实现PoolableObjectFactory创建连接的方法,并实现PoolableObjectFactory.destroyObject(T object)关闭连接的方法。当对象被驱逐时,GenericObejctPool 将调用此方法。