83 lines
2.7 KiB
Java
Raw Normal View History

2019-12-27 21:41:10 +08:00
package org.csource.fastdfs.pool;
2019-12-29 03:28:26 +08:00
import org.csource.common.MyException;
2019-12-27 21:41:10 +08:00
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ConnectionPool {
/**
* key is ip:port, value is ConnectionManager
*/
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();
2019-12-29 09:18:23 +08:00
public static Connection getConnection(InetSocketAddress socketAddress) throws MyException, IOException {
2019-12-27 21:41:10 +08:00
if (socketAddress == null) {
return null;
}
String key = getKey(socketAddress);
2019-12-29 03:28:26 +08:00
ConnectionManager connectionManager;
synchronized (ConnectionPool.class) {
connectionManager = CP.get(key);
if (connectionManager == null) {
2019-12-29 20:53:37 +08:00
connectionManager = new ConnectionManager(socketAddress);
2019-12-29 03:28:26 +08:00
CP.put(key, connectionManager);
}
2019-12-27 21:41:10 +08:00
}
return connectionManager.getConnection();
}
2019-12-29 03:28:26 +08:00
public static void releaseConnection(Connection connection) throws IOException {
if (connection == null) {
2019-12-27 21:41:10 +08:00
return;
}
2019-12-29 03:28:26 +08:00
String key = getKey(connection.getInetSocketAddress());
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.releaseConnection(connection);
2019-12-27 21:41:10 +08:00
} else {
2019-12-29 03:28:26 +08:00
try {
2019-12-29 20:53:37 +08:00
connection.closeDirectly();
2019-12-29 03:28:26 +08:00
} catch (IOException e) {
System.err.println("close socket error, msg:" + e.getMessage());
e.printStackTrace();
}
2019-12-27 21:41:10 +08:00
}
2019-12-29 03:28:26 +08:00
2019-12-27 21:41:10 +08:00
}
2019-12-29 03:28:26 +08:00
public static void closeConnection(Connection connection) throws IOException {
if (connection == null) {
2019-12-27 21:41:10 +08:00
return;
}
2019-12-29 03:28:26 +08:00
String key = getKey(connection.getInetSocketAddress());
ConnectionManager connectionManager = CP.get(key);
if (connectionManager != null) {
connectionManager.closeConnection(connection);
2019-12-27 21:41:10 +08:00
} else {
2019-12-29 20:53:37 +08:00
connection.closeDirectly();
2019-12-27 21:41:10 +08:00
}
}
private static String getKey(InetSocketAddress socketAddress) {
if (socketAddress == null) {
return null;
}
return String.format("%s:%s", socketAddress.getHostName(), socketAddress.getPort());
}
2019-12-29 03:28:26 +08:00
2019-12-27 21:41:10 +08:00
@Override
public String toString() {
if (!CP.isEmpty()) {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, ConnectionManager> managerEntry : CP.entrySet()) {
2019-12-29 20:53:37 +08:00
builder.append("key:[" + managerEntry.getKey() + " ]-------- entry:" + managerEntry.getValue() + "\n");
2019-12-27 21:41:10 +08:00
}
return builder.toString();
}
return null;
}
}