diff --git a/src/main/java/org/csource/common/MyException.java b/src/main/java/org/csource/common/MyException.java index 67d2220..2c1fe76 100644 --- a/src/main/java/org/csource/common/MyException.java +++ b/src/main/java/org/csource/common/MyException.java @@ -16,6 +16,7 @@ */ public class MyException extends Exception { public MyException(String s, Exception e) { + super(s,e); } public MyException(String message) { diff --git a/src/main/java/org/csource/fastdfs/TrackerGroup.java b/src/main/java/org/csource/fastdfs/TrackerGroup.java index dfcfd74..b93966a 100644 --- a/src/main/java/org/csource/fastdfs/TrackerGroup.java +++ b/src/main/java/org/csource/fastdfs/TrackerGroup.java @@ -87,7 +87,7 @@ public TrackerServer getTrackerServer() throws IOException { } } - return null; + throw new IOException("can not found available server, please check: " + tracker_servers); } public Object clone() { diff --git a/src/main/java/org/csource/fastdfs/TrackerServer.java b/src/main/java/org/csource/fastdfs/TrackerServer.java index a9875ed..8abe087 100644 --- a/src/main/java/org/csource/fastdfs/TrackerServer.java +++ b/src/main/java/org/csource/fastdfs/TrackerServer.java @@ -8,7 +8,6 @@ package org.csource.fastdfs; -import org.csource.common.MyException; import org.csource.fastdfs.pool.Connection; import org.csource.fastdfs.pool.ConnectionPool; import org.csource.fastdfs.pool.ConnectionFactory; @@ -26,11 +25,12 @@ public class TrackerServer { protected InetSocketAddress inetSockAddr; - public TrackerServer(InetSocketAddress inetSockAddr) throws IOException { + public TrackerServer(InetSocketAddress inetSockAddr) throws IOException{ this.inetSockAddr = inetSockAddr; + checkServerEnabled(); } - public Connection getConnection() throws MyException, IOException { + public Connection getConnection() throws IOException { Connection connection; if (ClientGlobal.g_connection_pool_enabled) { connection = ConnectionPool.getConnection(this.inetSockAddr); @@ -48,4 +48,16 @@ public InetSocketAddress getInetSocketAddress() { return this.inetSockAddr; } + /** + * check server enabled + * @throws IOException + */ + public void checkServerEnabled() throws IOException { + Connection connection = getConnection(); + try { + connection.release(); + }catch (Exception e){ + //ignore + } + } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java b/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java index 0a6b663..11659a5 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionFactory.java @@ -15,7 +15,7 @@ public class ConnectionFactory { * @return * @throws IOException */ - public static Connection create(InetSocketAddress socketAddress) throws MyException { + public static Connection create(InetSocketAddress socketAddress) throws IOException { try { Socket sock = new Socket(); sock.setReuseAddress(true); @@ -23,7 +23,7 @@ public static Connection create(InetSocketAddress socketAddress) throws MyExcept sock.connect(socketAddress, ClientGlobal.g_connect_timeout); return new Connection(sock, socketAddress); } catch (Exception e) { - throw new MyException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg:" + e.getMessage()); + throw new IOException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg:" + e.getMessage(), e); } } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java index fa76798..db01182 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java @@ -45,7 +45,7 @@ public ConnectionManager(InetSocketAddress socketAddress) { this.inetSocketAddress = socketAddress; } - public Connection getConnection() throws MyException { + public Connection getConnection() throws IOException { lock.lock(); try { Connection connection = null; @@ -81,10 +81,10 @@ public Connection getConnection() throws MyException { //wait single success continue; } - throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms"); + throw new IOException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms"); } catch (InterruptedException e) { e.printStackTrace(); - throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage()); + throw new IOException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage()); } } return connection; diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java index ec3820d..0889799 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java @@ -13,7 +13,7 @@ public class ConnectionPool { */ private final static ConcurrentHashMap CP = new ConcurrentHashMap(); - public static Connection getConnection(InetSocketAddress socketAddress) throws MyException { + public static Connection getConnection(InetSocketAddress socketAddress) throws IOException { if (socketAddress == null) { return null; }