代码如下:
package com.tmp.xSocket;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xsocket.connection.BlockingConnectionPool;
import org.xsocket.connection.IBlockingConnection;
import org.xsocket.connection.MaxConnectionsExceededException;
import com.zhangqing.util.DocbookXSDCheck;
public class ConnectionPool {
protected static Log logger = LogFactory.getLog(ConnectionPool.class);
private static int MAXCON = 10000;
private final static BlockingConnectionPool pool = new BlockingConnectionPool();
static {
pool.setMaxActive(MAXCON);
pool.setMaxIdle(MAXCON/2);
}
private static void sendMessage(String host, int port, String msg) {
IBlockingConnection bc = null;
try {
// retrieve a connection (if no connection is in pool, a new one will be created)
bc = pool.getBlockingConnection(host, port);
logger.info("bc.getId(): " + bc.getId());
bc.write(msg); //发送信息
//接收
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int length = bc.read(byteBuffer);
logger.info("length: " + length);
byteBuffer.flip();
byte[] content = new byte[byteBuffer.limit()];
byteBuffer.get(content); // 从ByteBuffer中读取数据到byte数组中
logger.info(new String(content));
byteBuffer.clear();
bc.flush();
// always close the connection! (the connection will be returned into the connection pool)
bc.close();
} catch (IOException ioe) {
logger.info(ioe.toString());
if (bc != null) {
try {
// if the connection is invalid -> destroy it , it will not return to the pool)
pool.destroy(bc);
} catch (Exception ignore) {
}
}
}
}
private static void displayPoolInfo(){
logger.info("--------------------------------------------------------");
logger.info("getMaxActive:" + pool.getMaxActive());
logger.info("getMaxActivePerServer:" + pool.getMaxActivePerServer());
logger.info("getMaxIdle:" + pool.getMaxIdle());
logger.info("getNumIdle:" + pool.getNumIdle());
logger.info("getNumActive:" + pool.getNumActive());
logger.info("getNumCreated:" + pool.getNumCreated());
logger.info("getNumDestroyed:" + pool.getNumDestroyed());
logger.info("getNumPendingGet:" + pool.getNumPendingGet());
logger.info("getNumTimeoutPooledMaxIdleTime:" + pool.getNumTimeoutPooledMaxIdleTime());
logger.info("getNumTimeoutPooledMaxLifeTime:" + pool.getNumTimeoutPooledMaxLifeTime());
logger.info("getPooledMaxIdleTimeMillis:" + pool.getPooledMaxIdleTimeMillis());
logger.info("getPooledMaxLifeTimeMillis:" + pool.getPooledMaxLifeTimeMillis());
logger.info("isOpen:" + pool.isOpen());
for (String str: pool.getActiveConnectionInfos()){
logger.info(str);
}
logger.info("--------------------------------------------------------");
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 5; i++){
sendMessage("127.0.0.1",8889,"Send messsage to server.");
Thread.sleep(1000);
}
displayPoolInfo();
}
}And the log is :
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C1I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C2I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C3I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C4I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C5I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:63) - --------------------------------------------------------
INFO (ConnectionPool.java:64) - getMaxActive:10000
INFO (ConnectionPool.java:65) - getMaxActivePerServer:2147483647
INFO (ConnectionPool.java:66) - getMaxIdle:5000
INFO (ConnectionPool.java:67) - getNumIdle:0
INFO (ConnectionPool.java:68) - getNumActive:5
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:0
INFO (ConnectionPool.java:71) - getNumPendingGet:0
INFO (ConnectionPool.java:72) - getNumTimeoutPooledMaxIdleTime:0
INFO (ConnectionPool.java:73) - getNumTimeoutPooledMaxLifeTime:0
INFO (ConnectionPool.java:74) - getPooledMaxIdleTimeMillis:2147483647
INFO (ConnectionPool.java:75) - getPooledMaxLifeTimeMillis:2147483647
INFO (ConnectionPool.java:76) - isOpen:true
INFO (ConnectionPool.java:78) - /127.0.0.1:3608 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C1] creationTime=2011.12.06 14:26:37, ageMillis=5141, elapsedLastUsageMillis=5031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3613 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C2] creationTime=2011.12.06 14:26:38, ageMillis=4031, elapsedLastUsageMillis=4031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3614 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C3] creationTime=2011.12.06 14:26:39, ageMillis=3031, elapsedLastUsageMillis=3031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3615 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C4] creationTime=2011.12.06 14:26:40, ageMillis=2031, elapsedLastUsageMillis=2031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3616 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C5] creationTime=2011.12.06 14:26:41, ageMillis=1031, elapsedLastUsageMillis=1031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:81) - --------------------------------------------------------Why 5 connections has been created? In my opinion, only one connection should be created, because :
for (int i = 0; i < 5; i++){
sendMessage("127.0.0.1",8889,"Send messsage to server.");
Thread.sleep(1000);
}同样的address and port, and "bc = pool.getBlockingConnection(host, port);" means "getting a pool connection for the given address. If no free connection is in the pool, a new one will be created. "
但是从第二次循环开始,该地址和端口已经有一个连接了,就不应该建立连接了.
package com.tmp.xSocket;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xsocket.connection.BlockingConnectionPool;
import org.xsocket.connection.IBlockingConnection;
import org.xsocket.connection.MaxConnectionsExceededException;
import com.zhangqing.util.DocbookXSDCheck;
public class ConnectionPool {
protected static Log logger = LogFactory.getLog(ConnectionPool.class);
private static int MAXCON = 10000;
private final static BlockingConnectionPool pool = new BlockingConnectionPool();
static {
pool.setMaxActive(MAXCON);
pool.setMaxIdle(MAXCON/2);
}
private static void sendMessage(String host, int port, String msg) {
IBlockingConnection bc = null;
try {
// retrieve a connection (if no connection is in pool, a new one will be created)
bc = pool.getBlockingConnection(host, port);
logger.info("bc.getId(): " + bc.getId());
bc.write(msg); //发送信息
//接收
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int length = bc.read(byteBuffer);
logger.info("length: " + length);
byteBuffer.flip();
byte[] content = new byte[byteBuffer.limit()];
byteBuffer.get(content); // 从ByteBuffer中读取数据到byte数组中
logger.info(new String(content));
byteBuffer.clear();
bc.flush();
// always close the connection! (the connection will be returned into the connection pool)
bc.close();
} catch (IOException ioe) {
logger.info(ioe.toString());
if (bc != null) {
try {
// if the connection is invalid -> destroy it , it will not return to the pool)
pool.destroy(bc);
} catch (Exception ignore) {
}
}
}
}
private static void displayPoolInfo(){
logger.info("--------------------------------------------------------");
logger.info("getMaxActive:" + pool.getMaxActive());
logger.info("getMaxActivePerServer:" + pool.getMaxActivePerServer());
logger.info("getMaxIdle:" + pool.getMaxIdle());
logger.info("getNumIdle:" + pool.getNumIdle());
logger.info("getNumActive:" + pool.getNumActive());
logger.info("getNumCreated:" + pool.getNumCreated());
logger.info("getNumDestroyed:" + pool.getNumDestroyed());
logger.info("getNumPendingGet:" + pool.getNumPendingGet());
logger.info("getNumTimeoutPooledMaxIdleTime:" + pool.getNumTimeoutPooledMaxIdleTime());
logger.info("getNumTimeoutPooledMaxLifeTime:" + pool.getNumTimeoutPooledMaxLifeTime());
logger.info("getPooledMaxIdleTimeMillis:" + pool.getPooledMaxIdleTimeMillis());
logger.info("getPooledMaxLifeTimeMillis:" + pool.getPooledMaxLifeTimeMillis());
logger.info("isOpen:" + pool.isOpen());
for (String str: pool.getActiveConnectionInfos()){
logger.info(str);
}
logger.info("--------------------------------------------------------");
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 5; i++){
sendMessage("127.0.0.1",8889,"Send messsage to server.");
Thread.sleep(1000);
}
displayPoolInfo();
}
}And the log is :
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C1I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C2I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C3I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C4I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:35) - bc.getId(): 74396444134120d6da52bb96e33C5I0
INFO (ConnectionPool.java:44) - server received data from client sucessful
INFO (ConnectionPool.java:63) - --------------------------------------------------------
INFO (ConnectionPool.java:64) - getMaxActive:10000
INFO (ConnectionPool.java:65) - getMaxActivePerServer:2147483647
INFO (ConnectionPool.java:66) - getMaxIdle:5000
INFO (ConnectionPool.java:67) - getNumIdle:0
INFO (ConnectionPool.java:68) - getNumActive:5
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:0
INFO (ConnectionPool.java:71) - getNumPendingGet:0
INFO (ConnectionPool.java:72) - getNumTimeoutPooledMaxIdleTime:0
INFO (ConnectionPool.java:73) - getNumTimeoutPooledMaxLifeTime:0
INFO (ConnectionPool.java:74) - getPooledMaxIdleTimeMillis:2147483647
INFO (ConnectionPool.java:75) - getPooledMaxLifeTimeMillis:2147483647
INFO (ConnectionPool.java:76) - isOpen:true
INFO (ConnectionPool.java:78) - /127.0.0.1:3608 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C1] creationTime=2011.12.06 14:26:37, ageMillis=5141, elapsedLastUsageMillis=5031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3613 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C2] creationTime=2011.12.06 14:26:38, ageMillis=4031, elapsedLastUsageMillis=4031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3614 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C3] creationTime=2011.12.06 14:26:39, ageMillis=3031, elapsedLastUsageMillis=3031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3615 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C4] creationTime=2011.12.06 14:26:40, ageMillis=2031, elapsedLastUsageMillis=2031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:78) - /127.0.0.1:3616 -> /127.0.0.1:8889 [74396444134120d6da52bb96e33C5] creationTime=2011.12.06 14:26:41, ageMillis=1031, elapsedLastUsageMillis=1031, countUsage=1, isReusable=true
INFO (ConnectionPool.java:81) - --------------------------------------------------------Why 5 connections has been created? In my opinion, only one connection should be created, because :
for (int i = 0; i < 5; i++){
sendMessage("127.0.0.1",8889,"Send messsage to server.");
Thread.sleep(1000);
}同样的address and port, and "bc = pool.getBlockingConnection(host, port);" means "getting a pool connection for the given address. If no free connection is in the pool, a new one will be created. "
但是从第二次循环开始,该地址和端口已经有一个连接了,就不应该建立连接了.
bc.setIdleTimeoutMillis(1000); //sets the idle timeout in millis,在调用close方法后开始算起的那么1秒后,连接不是idle,而是DestroyedINFO (ConnectionPool.java:67) - getNumIdle:0
INFO (ConnectionPool.java:68) - getNumActive:0
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:5bc.close(); 的意思是// always close the connection! (the connection will be returned into the connection pool),同时设置了bc.setIdleTimeoutMillis(1000);按理结果应该是:
INFO (ConnectionPool.java:67) - getNumIdle:5
INFO (ConnectionPool.java:68) - getNumActive:0
INFO (ConnectionPool.java:69) - getNumCreated:5
INFO (ConnectionPool.java:70) - getNumDestroyed:0问题出在哪里??????
xSocket-2.8.14.jar 的包可以在下面下载:http://sourceforge.net/projects/xsocket/files/xsocket%202.x%20-%20multiplexed/2.1.7/