两个线程在远程服务器中并发运行

时间:2016-01-10 06:29:12

标签: java multithreading jsch

我想远程连接到服务器,我通过 J sch programe 做了, 现在,我希望在该远程服务器中同时运行两个线程。

一个线程将监视远程服务器的CPU详细信息, 第二个线程将在同一个远程服务器上启动一个应用程序。

这是我的主要类的代码: OpenSessionForRemoteConnection()是使用jsch连接到远程服务器的子类。 GetCpuDetailsInitiateApllications是线程的子类。我的问题是如果我创建GetCpuDetails作为一个普通的子类,然后它得到CpuDetails没有任何问题,但是当我使用线程时,我没有得到任何输入流。我无法确定我哪里出错了!

我不是IT专家;有人可以帮忙吗?或者建议我做另外的选择吗?

MAIN CLASS ----

public static void main(String[] args) {        
    String host="192.168.101.191";
    Session session = null;
    OpenSessionForRemoteConnection server1 = new      OpenSessionForRemoteConnection();

    try{
        System.out.println("calling the method for opening a new session");
        session= server1.openSession(host);
        GetCpuDetails Cpu=new GetCpuDetails (session);
        InitiateApllications initiate=new InitiateApllications(session);


        Thread t1 = new Thread(Cpu);
        Thread t2 = new Thread(initiate);       

            t1.start();
            t2.start();


        server1.closeSession();
    } catch(Exception e){
          System.out.println(e);
    }
    System.out.println("SESSION CLOSED--->");

}

打开Session到远程主机的子类:

  String user="user";
  String password="password"; 
  String port="22";

  Session session;

/**
 * @param args
 */

  /*
 * This method is for opening a session and establishing remote connection to the host
 */
public Session openSession (String host){

    try{

        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();

        session=jsch.getSession(user, host, 22);


        session.setPassword(password);    
        session.setConfig(config);
        session.connect(); // New Session for Host established and connected                

    }catch(Exception e){
          System.out.println(e);
    }
     return session;   
}

/*
 * This method id for closing the session and disconnecting the remote server.
 */
public void closeSession(){     

    session.disconnect();   

}

这个子类实现了runnable;获取cpu详细信息

public class GetCpuDetails实现Runnable {

/**
 * @param args
 */
private Session session;

public GetCpuDetails (Session MainSession){
    //this.command=s;
    this.session=MainSession;
}

@Override
public void run() {
  processCommand(session);      
}

private  void processCommand(Session session) {

    try{
        System.out.println("get host"); 
        if(session.isConnected()){
            System.out.println("SESSION STILL CONNECTED------------->");    
        }       

        try{

            Channel channelForCPUDetails=channelForCPUDetails=session.openChannel("exec");
            ((ChannelExec)channelForCPUDetails).setCommand("iostat");               

            channelForCPUDetails.setInputStream(null);
            ((ChannelExec)channelForCPUDetails).setErrStream(System.err);   

            InputStream in=channelForCPUDetails.getInputStream();
            channelForCPUDetails.connect();


            byte[] tmp=new byte[1024];
            while(true){
                while(in.available()>0){
                     System.out.printf("INSIDE WHILE->"+channelForCPUDetails.getInputStream()); 
                  int i=in.read(tmp, 0, 1024);
                  if(i<0)break; 

                  System.out.print(new String(tmp, 0, i));

                }
                if(channelForCPUDetails.isClosed()){
                  System.out.println("exit-status: "+channelForCPUDetails.getExitStatus());
                  break;
                }
              }

            channelForCPUDetails.disconnect();        


    }catch(Exception e){
          System.out.println(e);
    }   


}


private void suspendThread(){       
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

2 个答案:

答案 0 :(得分:1)

您的问题是您过早关闭会话:

t1.start();
t2.start();
server1.closeSession();

您正在将会话注入到线程中。在关闭主线程中的会话之前,您无法保证线程会在服务器上使用这些会话。我建议你等待线程完成(例如,通过CyclicBarrierCountdownLatch),然后关闭会话。

答案 1 :(得分:0)

正如SMA所说,你在线程完成之前就已经退出了。您需要等待这些线程完成。你可以通过加入&#39;来做到这一点。到开始的线程。将您的代码更改为:

t1.start();
t2.start();

//Wait for the threads to finish
t1.join();
t2.join();

server1.closeSession();