fix: throw exception
parent
0bfa4ff1fa
commit
9a9e1cf9f0
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,17 +13,20 @@ public class ConnectionPool {
|
|||
*/
|
||||
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue