我在 Web 服务服务器上,并且有具有内部连接的对象。
初始化此连接需要很长时间,因此我的想法是使用对象池来重用不同请求之间的连接。
这些对象连接到每个用户,因此我更喜欢使用用户名作为键,使用连接作为值。但我不想让连接永远打开。也许一段时间后,如果用户不再启动请求,它应该被销毁。
我考虑过使用阿帕奇对象池 http://commons.apache.org/pool/但我没有看到过期(如果我错了请纠正我)
ehcache 为我提供了通知 http://ehcache.org/apidocs/net/sf/ehcache/event/CacheEventListener.html#notifyElementRemoved%28net.sf.ehcache.Ehcache,%20net.sf.ehcache.Element%29关于驱逐和过期,但超时后不会触发,只有再次触及缓存对象时才会触发。
有人知道可以为我完成这项工作的库吗?
看一眼http://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html 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.
实施一个PoolableObjectFactory
创建您的联系并实施PoolableObjectFactory.destroyObject(T object)
方法来关闭连接。当对象被驱逐时,GenericObejctPool 将调用此方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)