java 在生产中使用 commons-pool 的技巧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/939734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:28:00  来源:igfitidea点击:

Tips for using commons-pool in production

javasocketspool

提问by David Rabinowitz

Based on an answer I got here, I started to give commons-poola serious look. My last experience of using it was around 2003, probably version 1.1 or 1.2. Its main user, DBCP, is considered by many as flawed and to be avoided.

根据我在这里得到的答案,我开始认真看待commons-pool。我最后一次使用它是在 2003 年左右,可能是 1.1 或 1.2 版。它的主要用户DBCP被许多人认为是有缺陷的,应该避免。

Does anyone uses commons pool in production to write pool of your own? What is the best pool type to use? I plan to store clientTCP sockets in it.

有没有人在生产中使用公共池来编写自己的池?最好使用的池类型是什么?我计划在其中存储客户端TCP 套接字。

Is there another generic pool that replaces it?

是否有另一个通用池来替代它?

采纳答案by Confusion

Does anyone uses commons pool in production to write pool of your own?

有没有人在生产中使用公共池来编写自己的池?

Yes, I do and the pool holds TCP connections, like you intend it to. It's wired up via Spring, so assuming you understand Spring configuration:

是的,我这样做了,并且池中包含 TCP 连接,就像您希望的那样。它是通过 Spring 连接的,因此假设您了解 Spring 配置:

<bean class="com.company.ConnectionSupplier">
<constructor-arg>
  <!-- The ConnectionSupplier wraps an object pool -->
  <bean class="org.apache.commons.pool.impl.GenericObjectPool">
    <constructor-arg>
       <!-- The ObjectPool uses a ConnectionFactory to build new connections -->
       <bean class="com.company.ConnectionFactory">
         <constructor-arg value="server" />
         <constructor-arg value="3000" />  
       </bean>  
    </constructor-arg>
    <property name="maxActive" value="20" />
    <property name="testOnBorrow" value="true" />
  </bean>
</constructor-arg>
</bean>  

The ConnectionFactory extends BasePoolableObjectFactory and is a small wrapper around a SocketFactory.

ConnectionFactory 扩展了 BasePoolableObjectFactory 并且是一个围绕 SocketFactory 的小包装器。

@First comment: The ConnectionFactory constructor takes a server and a port. In the overriden makeObject(), it creates sockets that connect to that server and port. It returns 'Connection' objects that wrap the created socket with some convenience methods for communicating through the socket.

@第一条评论:ConnectionFactory 构造函数需要一个服务器和一个端口。在覆盖的 makeObject() 中,它创建连接到该服务器和端口的套接字。它返回“Connection”对象,这些对象使用一些方便的方法包装创建的套接字,以便通过套接字进行通信。

The connection is tested using a sort of 'ping' or 'echo' provided by the protocol used to communicate over the socket. Should that not have been available, validation/testing of the connection is not really possible, except for asking the socket whether it has been closed. In that case, a Connection in the pool would have been invalidated if it threw an exception and every method using Connections should be prepared for that kind of failure and attempt the same operation with another connection.

连接使用由用于通过套接字进行通信的协议提供的某种“ping”或“echo”进行测试。如果不可用,则除了询问套接字是否已关闭之外,实际上不可能对连接进行验证/测试。在这种情况下,如果池中的 Connection 抛出异常,它就会失效,并且每个使用 Connections 的方法都应该为这种失败做好准备,并尝试对另一个连接执行相同的操作。

回答by KARASZI István

You should check that the instantation costs more or the fetching from the pool. Because the only valid situation to use the pool is the first.

您应该检查实例化成本是否更高或从池中获取。因为使用池的唯一有效情况是第一种。

回答by Stefan L

Have you looked into Nettyor Apache MINA? They will both keep track of your TCP connections and should make implementing whatever communications protocol those TCP sockets will use easier as well.

你研究过NettyApache MINA吗?它们都将跟踪您的 TCP 连接,并且应该使实现这些 TCP 套接字将使用的任何通信协议也更容易。

回答by Gandalf

Check out MultiThreadedHttpConnectionManager- it's an Apache Commons HttpClient connection pool manager that will probably fit your need right out of the box.

查看MultiThreadedHttpConnectionManager- 它是一个 Apache Commons HttpClient 连接池管理器,开箱即可满足您的需求。

回答by Eran Medan

First don't use commons-pool 1.3, it has some major issues with multi threaded applications.

首先不要使用 commons-pool 1.3,它在多线程应用程序中存在一些主要问题。

Second, Java 5 concurency package has decent pool implementations (see sample here)

其次,Java 5 并发包具有不错的池实现(请参阅此处的示例)