是否有任何池解决方案允许将密钥传递给 acquire/take 方法

发布于 2024-10-12 03:02:56 字数 440 浏览 4 评论 0原文

我需要汇集与某些资源的连接。实际资源对于本次讨论并不重要。但值得注意的是,这些连接不是线程安全的,因此需要池化连接等是一项昂贵的操作。

我最初的想法是建立一个大池,它管理连接的超时并杀死它们等。但是问题是每个连接可能包含不同的参数,因此在获取连接时,有必要传递参数并让池程序使用它尝试查找具有相同值的连接。如果它(池)找不到对象,那么它需要创建一个传递参数的实例等。

但是 Apache Commons Pools 和其他类似的库没有提供此类概念的任何规定。是否有包含此类概念的库?我想池下面保存着一个实例列表,当传入请求时,它使用一些匹配器来循环其可用对象并返回第一个匹配的对象。

请注意,无法更改连接对象的参数,因此无法更改问题以及提出问题的原因。

  • 有没有更好的方法来解决这个问题?
  • 是否有一个可以执行上述操作的池库?
  • 我应该自己写吗?

I have a need to pool connections to some resource. The actual resource is not important for this discussion. The thing of note however is that these connections are not threadsafe, thus the need for pooling as connecting etc is an expensive operation.

My original idea was for one big pool which manages the timing out of connections and kills them etc. However the problem is each connection may include a different parameter, thus when taking the connection it is necessary to pass the parameter and let the pooler use this to attempt to find a connection with the same value. If it (the pool) could not find the object it then needs to create an instance passing the parameter of course etc.

However Apache Commons Pools and ohter similar libraries do not have any provision for this sort of concept. Are there any libraries which include this sort of concept. I suppose underneath the Pool holds a list of instances and when a take request comes in it uses some matcher to loop over its available objects and returns the first that is matched.

Note it is not possible to chnage the parameter of the connection object thus the problem and the reason for the question being asked.

  • Is there a better way to solve this problem ?
  • Is there a pooling library that does the above ?
  • Should i just write my own ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡笑忘祈一世凡恋 2024-10-19 03:02:56

为什么不先用钥匙找到泳池呢?然后使用可用的解决方案之一。我认为您不会通过尝试将所有内容集中在一起来简化任何事情,因为不同键控的项目不交叉兼容。

Why not use the key to find the pool first? Then use one of the available solutions. I don't think you would be simplifying anything by trying to pool everything together since differently-keyed items are not cross-compatible.

小草泠泠 2024-10-19 03:02:56

您可以检查 apache Pool 中的 KeyedObjectPool 吗?

Could you check KeyedObjectPool in apache Pool?

小女人ら 2024-10-19 03:02:56

如果对象不是线程安全的,为什么要池化?无论如何,您的池管理器将无法检查状态/从其他线程关闭它们。

只需使用 ThreadLocal 在每个线程中保留一份副本即可。

If the object is not thread-safe, why pooling? Your pool manager won't be able to check the status / close them from other thread anyway.

Just keep one copy in each thread using ThreadLocal.

黑寡妇 2024-10-19 03:02:56

KeyedObjectPool 就是答案,如果您想查看示例,请查看 这个

KeyedObjectPool is the answer and if you want to see an example have a look at this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文