From 45bfd2eca8d9ebb37d56de90c4027254b82dd584 Mon Sep 17 00:00:00 2001 From: lily Date: Tue, 27 Sep 2016 17:54:54 +0800 Subject: [PATCH] add the connection pool --- src/com/platform/glusterfs/ClusterInfo.java | 18 ++++++++ src/com/platform/utils/GanymedSSH.java | 51 +++++++++++++-------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/com/platform/glusterfs/ClusterInfo.java b/src/com/platform/glusterfs/ClusterInfo.java index 793b86ca..da56d396 100644 --- a/src/com/platform/glusterfs/ClusterInfo.java +++ b/src/com/platform/glusterfs/ClusterInfo.java @@ -5,6 +5,7 @@ package com.platform.glusterfs; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -16,6 +17,8 @@ import org.apache.log4j.PropertyConfigurator; import com.platform.utils.Constant; +import ch.ethz.ssh2.Connection; + /** * 获取集群信息 * @author liliy @@ -32,6 +35,7 @@ public class ClusterInfo { * 如果ip在集群中且联通状态为PeerinCluster(Connected) * 如果ip在集群中且但不连通为PeerinCluster(Disconnected) * @return + * @throws IOException * @see [类、类#方法、类#成员] */ public Map showClusterInfo() { @@ -86,6 +90,20 @@ public class ClusterInfo { } } + for (Map.Entry entry:peerIps.entrySet()){ + String key=entry.getKey(); + String value=entry.getValue(); + if(!Constant.ganymedSSH.otherConns.containsKey(key)){ + Connection connection=null; + try { + connection = Constant.ganymedSSH.getOpenedConnection(key, Constant.rootUser, Constant.rootPasswd, Constant.port); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Constant.ganymedSSH.otherConns.put(key,connection); + } + } return peerIps; } diff --git a/src/com/platform/utils/GanymedSSH.java b/src/com/platform/utils/GanymedSSH.java index e790243d..91e05582 100644 --- a/src/com/platform/utils/GanymedSSH.java +++ b/src/com/platform/utils/GanymedSSH.java @@ -23,8 +23,8 @@ import ch.ethz.ssh2.*; public class GanymedSSH { public static Logger log = Logger.getLogger(GanymedSSH.class); - Connection conn; - + Connection conn; + public Map otherConns; public boolean status = true;// 锟角凤拷锟斤拷锟街达拷锟斤拷锟斤拷锟阶刺� public GanymedSSH() { @@ -44,22 +44,22 @@ public class GanymedSSH { } - public Connection getOpenedConnection(String host, String username, String password, int port) + public static Connection getOpenedConnection(String host, String username, String password, int port) throws IOException { - conn = new Connection(host, port); - conn.connect(); // make sure the connection is opened - boolean isAuthenticated = conn.authenticateWithPassword(username, password); + Connection conns = new Connection(host, port); + conns.connect(); // make sure the connection is opened + boolean isAuthenticated = conns.authenticateWithPassword(username, password); if (isAuthenticated == false) throw new IOException("Authentication failed."); - return conn; + return conns; } public void execCmdNoWaitAcquiescent(String cmd) { - String host=Constant.hostIp; - String username=Constant.rootUser; - String password=Constant.rootPasswd; - int port=Constant.port; +// String host=Constant.hostIp; +// String username=Constant.rootUser; +// String password=Constant.rootPasswd; +// int port=Constant.port; Session sess = null; try { @@ -79,9 +79,16 @@ public class GanymedSSH { public void execCmdNoWait(String host, String username, String password, int port, String cmd) { Session sess = null; + Connection new_conn; try { - conn = getOpenedConnection(host, username, password, port); - sess = conn.openSession(); + if(otherConns.containsKey(host) && otherConns.get(host)!=null){ + new_conn=otherConns.get(host); + } + else{ + new_conn = getOpenedConnection(host, username, password, port); + otherConns.put(host, new_conn); + } + sess = new_conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd); } catch (IOException e) { @@ -94,10 +101,10 @@ public class GanymedSSH { } public List execCmdWaitAcquiescent(String cmd) { - String host=Constant.hostIp; - String username=Constant.rootUser; - String password=Constant.rootPasswd; - int port=Constant.port; +// String host=Constant.hostIp; +// String username=Constant.rootUser; +// String password=Constant.rootPasswd; +// int port=Constant.port; List reStrings = new ArrayList(); Session sess = null; @@ -148,9 +155,15 @@ public class GanymedSSH { List reStrings = new ArrayList(); Session sess = null; + Connection new_conn; try { - - sess = conn.openSession(); + if(otherConns.containsKey(host) && otherConns.get(host)!=null){ + new_conn=otherConns.get(host); + } + else{ + new_conn = getOpenedConnection(host, username, password, port); + otherConns.put(host, new_conn); + } // 执锟斤拷cmd sess.execCommand(cmd);