diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java b/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java index 3b79131..5cb1ffc 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java @@ -1,5 +1,6 @@ package org.csource.fastdfs.pool; +import org.csource.common.MyException; import org.csource.fastdfs.ClientGlobal; import java.io.IOException; @@ -12,11 +13,16 @@ public class ConnectionFactory { * @return * @throws IOException */ - public static Connection create(InetSocketAddress socketAddress) throws IOException { - Socket sock = new Socket(); - sock.setReuseAddress(true); - sock.setSoTimeout(ClientGlobal.g_network_timeout); - sock.connect(socketAddress, ClientGlobal.g_connect_timeout); - return new Connection(sock, socketAddress); + public static Connection create(InetSocketAddress socketAddress) throws MyException { + try { + Socket sock = new Socket(); + sock.setReuseAddress(true); + sock.setSoTimeout(ClientGlobal.g_network_timeout); + sock.connect(socketAddress, ClientGlobal.g_connect_timeout); + return new Connection(sock, socketAddress); + } catch (Exception e) { + System.err.println("get connection error , emsg:" + e.getMessage()); + throw new MyException("get connection error , emsg:" + e.getMessage()); + } } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java index 26fd37b..a481edb 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java @@ -5,6 +5,7 @@ import org.csource.fastdfs.ClientGlobal; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.LinkedList; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -45,7 +46,7 @@ public class ConnectionManager { this.inetSocketAddress = socketAddress; } - public Connection getConnection() throws MyException, IOException { + public Connection getConnection() throws MyException { lock.lock(); try { Connection connection = null; @@ -76,9 +77,6 @@ public class ConnectionManager { } return connection; } - } catch (IOException e) { - System.err.println("get connection ERROR , emsg:" + e.getMessage()); - throw e; } finally { lock.unlock(); } @@ -100,7 +98,7 @@ public class ConnectionManager { } - public void closeConnection(Connection connection) throws IOException { + public void closeConnection(Connection connection) { try { if (connection != null) { totalCount.decrementAndGet(); @@ -109,7 +107,6 @@ public class ConnectionManager { } catch (IOException e) { System.err.println("close socket error , msg:" + e.getMessage()); e.printStackTrace(); - throw e; } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java index 300e64d..0679fbc 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java @@ -13,17 +13,20 @@ public class ConnectionPool { */ private final static ConcurrentHashMap CP = new ConcurrentHashMap(); - public static Connection getConnection(InetSocketAddress socketAddress) throws MyException, IOException { + public static Connection getConnection(InetSocketAddress socketAddress) throws MyException { if (socketAddress == null) { return null; } String key = getKey(socketAddress); ConnectionManager connectionManager; - synchronized (ConnectionPool.class) { - connectionManager = CP.get(key); - if (connectionManager == null) { - connectionManager = new ConnectionManager(socketAddress); - CP.put(key, connectionManager); + connectionManager = CP.get(key); + if (connectionManager == null) { + synchronized (ConnectionPool.class) { + connectionManager = CP.get(key); + if (connectionManager == null) { + connectionManager = new ConnectionManager(socketAddress); + CP.put(key, connectionManager); + } } } return connectionManager.getConnection(); @@ -60,7 +63,7 @@ public class ConnectionPool { if (socketAddress == null) { return null; } - return String.format("%s:%s", socketAddress.getHostName(), socketAddress.getPort()); + return String.format("%s:%s", socketAddress.getHostName().trim(), socketAddress.getPort()); } @Override