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

Java中具有过期时间的对象池的第三方库,java,object-pooling,Java,Object Pooling,我在一个webservice服务器上,我有一个具有内部连接的对象。 初始化这个连接需要很长时间,所以我的想法是使用一个对象池来重用不同请求之间的连接 对象连接到每个用户,所以我更喜欢使用用户名作为键,连接作为值。但我不想让连接永远打开。也许过了一段时间,如果用户不再启动请求,它应该被销毁 我考虑过使用,但我没有看到过期(如果我错了,请纠正我) ehcache向我提供了退出和过期的信息,但只有在再次触摸缓存对象时,才会在超时结束后触发 有人知道可以帮我做这项工作的lib吗?看看 从javadoc:

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

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

我考虑过使用,但我没有看到过期(如果我错了,请纠正我)

ehcache向我提供了退出和过期的信息,但只有在再次触摸缓存对象时,才会在超时结束后触发

有人知道可以帮我做这项工作的lib吗?

看看

从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将调用此方法。

受assylia想法的启发,我在这里使用了番石榴方法来解决问题

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);
final RemovalListener RemovalListener=new RemovalListener(){
@凌驾
移除时公共作废(最终移除通知){
断开连接(notification.getValue());
}
};
Cache Cache=CacheBuilder.newBuilder().maximumSize(20)
.expireAfterAccess(30,时间单位。秒)
.removalListener(removalListener.build();
final ScheduledExecutorService CacheMainance=Executors.newSingleThreadScheduledExecutor();
CacheMainance.scheduleAtFixedRate(新的Runnable(){
@凌驾
公开募捐{
cache.cleanUp();
}
},10,10,时间单位为秒);

最近,新的通用接口被添加到该界面中:


(请参阅定时驱逐)是的,完全忘记了番石榴贮藏室:/你能写下这个答案吗?我想这正是我想要的。啊,他们就是这么想的,谢谢!我认为番石榴的做法更好use@Sorontur从概念上看,缓存不是对象池。我还认为缓存适合您的需求,因为您的连接绑定到特定的用户,而池中包含通常每个人都可以使用的对象。当然,您可以为每个用户创建一个池,但我想这不是您想要的。因此,我也认为番石榴贮藏是正确的+我完全同意你的意见,我的要求是两者兼而有之。感谢谷歌的家伙们!他们总是知道我们想要什么;)