add the connection pool

web_backend_develope
lily 9 years ago
parent 984d6628f9
commit 45bfd2eca8

@ -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 {
* ipPeerinCluster(Connected)
* ipPeerinCluster(Disconnected)
* @return
* @throws IOException
* @see [##]
*/
public Map<String, String> showClusterInfo() {
@ -86,6 +90,20 @@ public class ClusterInfo {
}
}
for (Map.Entry<String, String> 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;
}

@ -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<String, Connection> otherConns;
public boolean status = true;// 锟角凤拷锟斤拷锟街达拷锟斤拷锟斤拷锟阶刺<E998B6>
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<String> 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<String> reStrings = new ArrayList<String>();
Session sess = null;
@ -148,9 +155,15 @@ public class GanymedSSH {
List<String> reStrings = new ArrayList<String>();
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);

Loading…
Cancel
Save