fix: throw exception
parent
0bfa4ff1fa
commit
9a9e1cf9f0
|
@ -1,5 +1,6 @@
|
||||||
package org.csource.fastdfs.pool;
|
package org.csource.fastdfs.pool;
|
||||||
|
|
||||||
|
import org.csource.common.MyException;
|
||||||
import org.csource.fastdfs.ClientGlobal;
|
import org.csource.fastdfs.ClientGlobal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -12,11 +13,16 @@ public class ConnectionFactory {
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static Connection create(InetSocketAddress socketAddress) throws IOException {
|
public static Connection create(InetSocketAddress socketAddress) throws MyException {
|
||||||
Socket sock = new Socket();
|
try {
|
||||||
sock.setReuseAddress(true);
|
Socket sock = new Socket();
|
||||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
sock.setReuseAddress(true);
|
||||||
sock.connect(socketAddress, ClientGlobal.g_connect_timeout);
|
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||||
return new Connection(sock, socketAddress);
|
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.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
@ -45,7 +46,7 @@ public class ConnectionManager {
|
||||||
this.inetSocketAddress = socketAddress;
|
this.inetSocketAddress = socketAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() throws MyException, IOException {
|
public Connection getConnection() throws MyException {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
|
@ -76,9 +77,6 @@ public class ConnectionManager {
|
||||||
}
|
}
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("get connection ERROR , emsg:" + e.getMessage());
|
|
||||||
throw e;
|
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
@ -100,7 +98,7 @@ public class ConnectionManager {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void closeConnection(Connection connection) throws IOException {
|
public void closeConnection(Connection connection) {
|
||||||
try {
|
try {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
totalCount.decrementAndGet();
|
totalCount.decrementAndGet();
|
||||||
|
@ -109,7 +107,6 @@ public class ConnectionManager {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("close socket error , msg:" + e.getMessage());
|
System.err.println("close socket error , msg:" + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,17 +13,20 @@ public class ConnectionPool {
|
||||||
*/
|
*/
|
||||||
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();
|
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) {
|
if (socketAddress == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String key = getKey(socketAddress);
|
String key = getKey(socketAddress);
|
||||||
ConnectionManager connectionManager;
|
ConnectionManager connectionManager;
|
||||||
synchronized (ConnectionPool.class) {
|
connectionManager = CP.get(key);
|
||||||
connectionManager = CP.get(key);
|
if (connectionManager == null) {
|
||||||
if (connectionManager == null) {
|
synchronized (ConnectionPool.class) {
|
||||||
connectionManager = new ConnectionManager(socketAddress);
|
connectionManager = CP.get(key);
|
||||||
CP.put(key, connectionManager);
|
if (connectionManager == null) {
|
||||||
|
connectionManager = new ConnectionManager(socketAddress);
|
||||||
|
CP.put(key, connectionManager);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return connectionManager.getConnection();
|
return connectionManager.getConnection();
|
||||||
|
@ -60,7 +63,7 @@ public class ConnectionPool {
|
||||||
if (socketAddress == null) {
|
if (socketAddress == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return String.format("%s:%s", socketAddress.getHostName(), socketAddress.getPort());
|
return String.format("%s:%s", socketAddress.getHostName().trim(), socketAddress.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue