fix:some function rename
This commit is contained in:
parent
82105c4483
commit
98f55774cf
@ -12,10 +12,8 @@ import org.csource.common.Base64;
|
|||||||
import org.csource.common.MyException;
|
import org.csource.common.MyException;
|
||||||
import org.csource.common.NameValuePair;
|
import org.csource.common.NameValuePair;
|
||||||
import org.csource.fastdfs.pool.Connection;
|
import org.csource.fastdfs.pool.Connection;
|
||||||
import org.omg.CORBA.CODESET_INCOMPATIBLE;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -678,7 +676,7 @@ public class StorageClient {
|
|||||||
return results;
|
return results;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -686,7 +684,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -841,7 +839,7 @@ public class StorageClient {
|
|||||||
return results;
|
return results;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -849,7 +847,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -920,7 +918,7 @@ public class StorageClient {
|
|||||||
return 0;
|
return 0;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -928,14 +926,14 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doFinally(Connection connection, boolean bNewStorageServer) {
|
private void releaseConnection(Connection connection, boolean bNewStorageServer) {
|
||||||
try {
|
try {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
this.storageServer.releaseConnection(connection);
|
connection.release();
|
||||||
}
|
}
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
@ -1017,7 +1015,7 @@ public class StorageClient {
|
|||||||
return 0;
|
return 0;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1026,7 +1024,7 @@ public class StorageClient {
|
|||||||
|
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1050,7 +1048,7 @@ public class StorageClient {
|
|||||||
return pkgInfo.errno;
|
return pkgInfo.errno;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1058,7 +1056,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1131,7 +1129,7 @@ public class StorageClient {
|
|||||||
return pkgInfo.errno;
|
return pkgInfo.errno;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1139,7 +1137,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1185,7 +1183,7 @@ public class StorageClient {
|
|||||||
return pkgInfo.body;
|
return pkgInfo.body;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1193,7 +1191,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1274,7 +1272,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1282,7 +1280,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1353,7 +1351,7 @@ public class StorageClient {
|
|||||||
return 0;
|
return 0;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1361,7 +1359,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1390,7 +1388,7 @@ public class StorageClient {
|
|||||||
return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset));
|
return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset));
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1398,7 +1396,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1477,7 +1475,7 @@ public class StorageClient {
|
|||||||
return pkgInfo.errno;
|
return pkgInfo.errno;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1485,7 +1483,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1596,7 +1594,7 @@ public class StorageClient {
|
|||||||
create_timestamp, crc32, source_ip_addr);
|
create_timestamp, crc32, source_ip_addr);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
this.storageServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -1604,7 +1602,7 @@ public class StorageClient {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
} finally {
|
} finally {
|
||||||
doFinally(connection, bNewStorageServer);
|
releaseConnection(connection, bNewStorageServer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,6 @@ import org.csource.fastdfs.pool.Connection;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -140,7 +139,7 @@ public class TrackerClient {
|
|||||||
return new StorageServer(ip_addr, port, store_path);
|
return new StorageServer(ip_addr, port, store_path);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
trackerServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -150,7 +149,7 @@ public class TrackerClient {
|
|||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
trackerServer.releaseConnection(connection);
|
connection.release();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -255,7 +254,7 @@ public class TrackerClient {
|
|||||||
return results;
|
return results;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
trackerServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -265,7 +264,7 @@ public class TrackerClient {
|
|||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
trackerServer.releaseConnection(connection);
|
connection.release();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -408,7 +407,7 @@ public class TrackerClient {
|
|||||||
return servers;
|
return servers;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
trackerServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -419,7 +418,7 @@ public class TrackerClient {
|
|||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
trackerServer.releaseConnection(connection);
|
connection.release();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -501,7 +500,7 @@ public class TrackerClient {
|
|||||||
return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize());
|
return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
trackerServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -515,7 +514,7 @@ public class TrackerClient {
|
|||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
trackerServer.releaseConnection(connection);
|
connection.release();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -606,7 +605,7 @@ public class TrackerClient {
|
|||||||
return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize());
|
return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
try {
|
try {
|
||||||
trackerServer.close(connection);
|
connection.close();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -619,7 +618,7 @@ public class TrackerClient {
|
|||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
trackerServer.releaseConnection(connection);
|
connection.release();
|
||||||
} catch (IOException ex1) {
|
} catch (IOException ex1) {
|
||||||
ex1.printStackTrace();
|
ex1.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -679,14 +678,14 @@ public class TrackerClient {
|
|||||||
return pkgInfo.errno == 0;
|
return pkgInfo.errno == 0;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
try {
|
try {
|
||||||
trackerServer.close(connection);
|
connection.close();
|
||||||
} finally {
|
} finally {
|
||||||
connection = null;
|
connection = null;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
trackerServer.releaseConnection(connection);
|
connection.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,34 +47,5 @@ public class TrackerServer {
|
|||||||
public InetSocketAddress getInetSocketAddress() {
|
public InetSocketAddress getInetSocketAddress() {
|
||||||
return this.inetSockAddr;
|
return this.inetSockAddr;
|
||||||
}
|
}
|
||||||
public void close(Connection connection) throws IOException {
|
|
||||||
if (connection == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//if connection enabled get from connection pool
|
|
||||||
if (ClientGlobal.g_connection_pool_enabled) {
|
|
||||||
ConnectionPool.closeConnection(connection);
|
|
||||||
} else {
|
|
||||||
connection.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* releaseConnection connection
|
|
||||||
* @param connection
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public void releaseConnection(Connection connection) throws IOException {
|
|
||||||
if (connection == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ClientGlobal.g_connection_pool_enabled) {
|
|
||||||
ConnectionPool.releaseConnection(connection);
|
|
||||||
} else {
|
|
||||||
connection.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.csource.fastdfs.pool;
|
package org.csource.fastdfs.pool;
|
||||||
|
|
||||||
|
import org.csource.fastdfs.ClientGlobal;
|
||||||
import org.csource.fastdfs.ProtoCommon;
|
import org.csource.fastdfs.ProtoCommon;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -45,11 +46,30 @@ public class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* close direct if not create from pool or not open pool
|
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
//if connection enabled get from connection pool
|
||||||
|
if (ClientGlobal.g_connection_pool_enabled) {
|
||||||
|
ConnectionPool.closeConnection(this);
|
||||||
|
} else {
|
||||||
|
this.closeDirectly();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void release() throws IOException {
|
||||||
|
if (ClientGlobal.g_connection_pool_enabled) {
|
||||||
|
ConnectionPool.releaseConnection(this);
|
||||||
|
} else {
|
||||||
|
this.closeDirectly();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* force close socket,
|
||||||
|
*/
|
||||||
|
public void closeDirectly() throws IOException {
|
||||||
if (this.sock != null) {
|
if (this.sock != null) {
|
||||||
try {
|
try {
|
||||||
ProtoCommon.closeSocket(this.sock);
|
ProtoCommon.closeSocket(this.sock);
|
||||||
|
@ -6,27 +6,6 @@ import java.io.IOException;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
public class ConnectionFactory {
|
public class ConnectionFactory {
|
||||||
public static Connection create(String key) throws IOException {
|
|
||||||
if (key == null) {
|
|
||||||
System.err.printf("ip:port entry conn't be null");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String[] keyPortString = key.split(":");
|
|
||||||
if (keyPortString.length != 2) {
|
|
||||||
System.err.printf("ip:port entry is invalid! key:{}", keyPortString);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String ip = keyPortString[0];
|
|
||||||
Integer port = Integer.parseInt(keyPortString[1]);
|
|
||||||
Socket sock = new Socket();
|
|
||||||
sock.setReuseAddress(true);
|
|
||||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
|
||||||
InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
|
|
||||||
sock.connect(inetSocketAddress, ClientGlobal.g_connect_timeout);
|
|
||||||
return new Connection(sock, inetSocketAddress);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create from InetSocketAddress
|
* create from InetSocketAddress
|
||||||
* @param socketAddress
|
* @param socketAddress
|
||||||
|
@ -2,7 +2,9 @@ package org.csource.fastdfs.pool;
|
|||||||
|
|
||||||
import org.csource.common.MyException;
|
import org.csource.common.MyException;
|
||||||
import org.csource.fastdfs.ClientGlobal;
|
import org.csource.fastdfs.ClientGlobal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
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;
|
||||||
@ -10,10 +12,8 @@ import java.util.concurrent.locks.Condition;
|
|||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class ConnectionManager {
|
public class ConnectionManager {
|
||||||
/**
|
|
||||||
* ip:port is key
|
private InetSocketAddress inetSocketAddress;
|
||||||
*/
|
|
||||||
private String key;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* total create connection pool
|
* total create connection pool
|
||||||
@ -41,20 +41,10 @@ public class ConnectionManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionManager(String key) {
|
public ConnectionManager(InetSocketAddress socketAddress) {
|
||||||
this.key = key;
|
this.inetSocketAddress = socketAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection newConnection() throws IOException {
|
|
||||||
try {
|
|
||||||
Connection connection = ConnectionFactory.create(this.key);
|
|
||||||
return connection;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Connection getConnection() throws MyException, IOException {
|
public Connection getConnection() throws MyException, IOException {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
@ -68,7 +58,7 @@ public class ConnectionManager {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if (ClientGlobal.getG_connection_pool_max_count_per_entry() == 0 || totalCount.get() < ClientGlobal.getG_connection_pool_max_count_per_entry()) {
|
} else if (ClientGlobal.getG_connection_pool_max_count_per_entry() == 0 || totalCount.get() < ClientGlobal.getG_connection_pool_max_count_per_entry()) {
|
||||||
connection = newConnection();
|
connection = ConnectionFactory.create(this.inetSocketAddress);
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
totalCount.incrementAndGet();
|
totalCount.incrementAndGet();
|
||||||
}
|
}
|
||||||
@ -94,22 +84,19 @@ public class ConnectionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void releaseConnection(Connection connection) throws IOException {
|
public void releaseConnection(Connection connection) {
|
||||||
if (connection == null) {
|
if (connection == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ((System.currentTimeMillis() - connection.getLastAccessTime()) < ClientGlobal.g_connection_pool_max_idle_time) {
|
|
||||||
try {
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
try {
|
||||||
|
connection.setLastAccessTime(System.currentTimeMillis());
|
||||||
freeConnections.add(connection);
|
freeConnections.add(connection);
|
||||||
freeCount.incrementAndGet();
|
freeCount.incrementAndGet();
|
||||||
condition.signal();
|
condition.signal();
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
closeConnection(connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,21 +104,22 @@ public class ConnectionManager {
|
|||||||
try {
|
try {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
totalCount.decrementAndGet();
|
totalCount.decrementAndGet();
|
||||||
connection.close();
|
connection.closeDirectly();
|
||||||
}
|
}
|
||||||
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ConnectionManager{" +
|
return "ConnectionManager{" +
|
||||||
"key='" + key + '\'' +
|
"ip:port='" + inetSocketAddress.getHostName() + ":" + inetSocketAddress.getPort() +
|
||||||
", totalCount=" + totalCount +
|
", totalCount=" + totalCount +
|
||||||
", freeCount=" + freeCount +
|
", freeCount=" + freeCount +
|
||||||
", linkedQueueCP=" + freeConnections +
|
", freeConnections =" + freeConnections +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ public class ConnectionPool {
|
|||||||
synchronized (ConnectionPool.class) {
|
synchronized (ConnectionPool.class) {
|
||||||
connectionManager = CP.get(key);
|
connectionManager = CP.get(key);
|
||||||
if (connectionManager == null) {
|
if (connectionManager == null) {
|
||||||
connectionManager = new ConnectionManager(key);
|
connectionManager = new ConnectionManager(socketAddress);
|
||||||
CP.put(key, connectionManager);
|
CP.put(key, connectionManager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ public class ConnectionPool {
|
|||||||
connectionManager.releaseConnection(connection);
|
connectionManager.releaseConnection(connection);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
connection.close();
|
connection.closeDirectly();
|
||||||
} 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();
|
||||||
@ -57,7 +57,7 @@ public class ConnectionPool {
|
|||||||
if (connectionManager != null) {
|
if (connectionManager != null) {
|
||||||
connectionManager.closeConnection(connection);
|
connectionManager.closeConnection(connection);
|
||||||
} else {
|
} else {
|
||||||
connection.close();
|
connection.closeDirectly();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ public class ConnectionPool {
|
|||||||
if (!CP.isEmpty()) {
|
if (!CP.isEmpty()) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (Map.Entry<String, ConnectionManager> managerEntry : CP.entrySet()) {
|
for (Map.Entry<String, ConnectionManager> managerEntry : CP.entrySet()) {
|
||||||
builder.append("key:" + managerEntry.getKey() + " -------- entry:" + managerEntry.getValue() + "\n");
|
builder.append("key:[" + managerEntry.getKey() + " ]-------- entry:" + managerEntry.getValue() + "\n");
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user