如何使用Apache Commons Pool通过超时将对象返回给Pool [英] How to return objects to the Pool by timeout using apache commons pool

查看:73
本文介绍了如何使用Apache Commons Pool通过超时将对象返回给Pool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Commons Pool库维护一个基于长沙发的连接池(可以看作是任何类型的连接,都没有关系)。

I'm using Apache Commons Pool library to maintaing a pool of couchbase connections (can be seen as any kind of connection, doesn't really matter).

我面临的问题是,我发现将对象返回到池中的唯一方法是通过从 GenericObjectPool 类调用 returnObject 方法以编程方式进行操作。这迫使应用程序保证一旦从池中借用了对象,无论应用程序中有任何异常或意外行为,都将返回该对象。

The problem I'm facing is that the only way I've found to return objects to the pool is to do it programmatically by calling the returnObject method from the GenericObjectPool class. This forces the application to guarantee the return of the object once is borrowed from the pool, regardless any exception or unexpected behavior in the application.

即使我控制了在整个应用程序中将对象返回到池中,我发现冒着完全依赖程序员返回对象的风险。是否有人知道一旦超过给定的超时就自动返回对象的方法(回收对象)。一旦借用的对象超过超时限制,它也可以使池创建新对象。

Even though I'm controlling the return of the objects to the pool in the whole application, I find risky to depend exclusively on the programmer to return the objects. Does anybody knows a way to automatically return objects once a given timeout is exceeded (reclaim objects). It would also work any way to make the pool create new objects, once a timeout is exceeded for the borrowed objects.

PS:我的应用程序公开了一组REST Web服务,该服务器连接到分布式Memcache服务器(Couchbase)。池会创建一组与Couchbase的连接。

PS: My application exposes a set of REST Web Services, that connect to distributed memcache server (Couchbase). The pool creates a set of connections to Couchbase.

任何建议都会有所帮助!

Any suggestion would help!

编辑

我尝试的第一个解决方案是创建一个新类( CouchbaseClientHandler ),该类包含对象类型的连接对象存储在池中( CouchbaseClient )。
我已经在 CouchbaseClientHandler 上实现了 finalize 方法,如果该对象的引用丢失了,请确保该关联对象实际上已返回到池中。意外的异常。当垃圾回收器销毁对象时,将返回该对象。这没有按预期进行。无法预测垃圾收集器何时到达回收对象,并且通常花费的时间比期望的要长。

The first solution I've tried was to create a New Class (CouchbaseClientHandler) containing a connection Object from the type of Objects stored in the pool (CouchbaseClient). I've implemented the finalize method on CouchbaseClientHandler, ensuring that the associated was actually returned to the pool, if the reference to this object was getting lost by an unexpected exception. The object would be returned when the garbage collector destroys the object. This didn't worked as expected. Is impossible to predict when the garbage collector will arrive a reclaim objects, and it was normally taken higher time than desired.

我现在实际工作的解决方案是有点不同,但更安全。
因为我的Pool打算由Web服务使用,并且由于每个Web服务都在单独且唯一的线程中运行(我在Tomcat上使用Jersey,所以我决定使用一个静态hashmap变量将唯一的线程ID映射到在执行Web服务时从池借来的 CouchbaseClient 对象列表。由于我的Web服务是以这样一种方式设计的,无论发生什么情况,处理程序方法都会在返回之前处理最终输出,因此,我可以确保始终运行将那些借用对象返回到池
的方法(连接),但尚未有效返回到池中。

The solution I've actually working right now is a bit different, but wide safer. Since my Pool is intended to be used by Web Services, and since every web service is running in a separate and unique thread (I'm using Jersey on Tomcat), I'm decided to use a static hashmap variable that maps unique Thread Id's to a list of CouchbaseClient objects created borrowed from the pool in the execution of the webservice. Since my web services are designed in such a way that, not matter what happens, a handler method will process the final output before returning, I can be sure to always run a method that returns to the pool those borrowed objects (connections), that have not been effectively returned to the pool.

尽管这对我来说效果很好,但我真的很想知道是否有更好的方法可以通过超时或未归还借来的对象来声明或清除。

Although this worked pretty well for me, I would really like to know if there is a better way of claiming or wiping by timeout borrowed objects that have not been returned.

推荐答案

假定破坏/创建新方法是可以的,并且您正在使用公共池2.0+版本,则可以使用废弃的对象跟踪和删除,以确保在借用且从未归还对象时不会永久泄漏容量。有关配置设置,请参阅AdoconedConfig的javadoc,以及将AbandonedConfig实例作为参数的GenericObjectPool构造函数。如果启用了废弃对象跟踪和删除功能,则当池容量不足时,该池将销毁已借用但返回时间不超过removeAbandonedTimeout的实例。

Assuming that the destroy / create new approach is OK and you are using version 2.0+ of commons pool, you can use abandoned object tracking and removal to make sure capacity is not permanently leaked when objects are borrowed and never returned. See the javadoc for AbandonedConfig for the configuration settings and the GenericObjectPool constructor that takes an AbandonedConfig instance as an argument. With abandoned object tracking and removal enabled, the pool will destroy instances that have been borrowed but not returned for longer than the removeAbandonedTimeout when the pool is low on capacity.

由于某些原因,您必须使用版本1.x池,您可以获取源代码或直接使用DBCP 1.x附带的AbandonedObjectPool。

If for some reason you have to use a version 1.x pool, you can grab the source or use directly the AbandonedObjectPool that ships with DBCP 1.x.

这篇关于如何使用Apache Commons Pool通过超时将对象返回给Pool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆