2017-05-20 2 views
-2

Je reçois la liste des fichiers dans différents répertoire parallèle depuis un serveur SFTP en utilisant JSch avec groupe de threads (différentes sessions) et je suis en face:Obtenir la liste des fichiers dans différents répertoire parallèle depuis un serveur SFTP en utilisant JSch

"FileProcessThread4" prio=10 tid=0x00007fd826c9e000 nid=0x1e0c waiting for monitor entry [0x00007fd841824000] 
    java.lang.Thread.State: BLOCKED (on object monitor) 

Problème à cause de JSch 0.1.54 ou parallèle?

avec commande find obtenir dernières 10 min anciens fichiers,

code iam utilisant:

public static String[] getRemoteFiles(String host, String user, String password, String command, String port) { 
    String line = null; 
    Session session = null; 
    ChannelExec channelExec = null; 
    InputStream in = null; 
    List<String> list = null; 
    BufferedReader reader = null; 
    try { 
     list = new ArrayList<String>(); 
     JSchUtil jSchUtil = new JSchUtil(); 
     session = jSchUtil.connect(host, user, password, port); 
     channelExec = (ChannelExec) session.openChannel("exec"); 
     logger.debug("SSH exec channel opened"); 
     in = channelExec.getInputStream(); 
     channelExec.setCommand(command); 
     channelExec.connect() 
     reader = new BufferedReader(new InputStreamReader(in)); 

     //Here block is happens 
     while ((line = reader.readLine()) != null) { 
      list.add(line); 
     } 

    } catch (Exception exception) { 
     logger.error("Exception-->" + exception.getMessage(), exception); 
    } finally { 
     if (channelExec != null) { 
      try { 
       channelExec.disconnect(); 
      } catch (Exception exception) { 
       logger.error("Exception-->" + exception.getMessage(), exception); 
      } finally { 
       channelExec = null; 
      } 
     } 
     if (session != null) { 
      try { 
       session.disconnect(); 
      } catch (Exception exception) { 
       logger.error("Exception-->" + exception.getMessage(), exception); 
      } finally { 
       session = null; 
      } 
     } 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (Exception exception) { 
       logger.error("Exception-->" + exception.getMessage(), exception); 
      } finally { 
       reader = null; 
      } 
     } 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (Exception exception) { 
       logger.error("Exception-->" + exception.getMessage(), exception); 
      } finally { 
       in = null; 
      } 
     } 
    } 
    return list.toArray(new String[] {}); 
} 

public class JSchUtil { 

private Logger logger = Logger.getLogger(JSchUtil.class); 

private static final String STRICT_HOSTKEY_CHECKIN_KEY = "StrictHostKeyChecking"; 
private static final String STRICT_HOSTKEY_CHECKIN_VALUE = "no"; 

public Session connect(String host, String username, String password, String port) throws Exception { 
    JSch jsch = new JSch(); 
    Session sshSession = jsch.getSession(username, host, Integer.valueOf(port)); 
    sshSession.setUserInfo(new UserInfo() { 
     public String getPassphrase() { 
      return null; 
     } 

     public String getPassword() { 
      return null; 
     } 

     public boolean promptPassphrase(String string) { 
      return false; 
     } 

     public boolean promptPassword(String string) { 
      return false; 
     } 

     public boolean promptYesNo(String string) { 
      return true; 
     } 

     public void showMessage(String string) { 
     } 
    }); 
    sshSession.setPassword(password); 
    Properties config = new Properties(); 
    config.put(STRICT_HOSTKEY_CHECKIN_KEY, STRICT_HOSTKEY_CHECKIN_VALUE); 
    sshSession.setConfig(config); 
    logger.debug("SSH_Connection|Host->" + host + "|Port->" + port + "|Username->" + username + "|Password->" + password); 
    sshSession.connect(); 
    logger.debug("SSH_Connection|Status->Success"); 
    return sshSession; 
} 

}

Merci pour suggestions

+0

Montrez-nous votre code! –

+0

Code ajouté dans la question. – Ajay

+0

Le code n'a rien à voir avec SFTP. –

Répondre

0

J'ai utilisé cette bibliothèque avant et l'ordre dans dont j'ai exécuté les différentes instructions, est la suivante (ceci ne compile pas, montrant simplement les différentes méthodes à invoquer et leur ordre):

session.openChannel("exec"); 
channel.setCommand(...); 
reader = new BufferedReader(new InputStreamReader(channel.getInputStream())); 
channel.connect(); 

// Wait for command to terminate 
while ((exit_code = channel.getExitStatus()) == -1) 
{ 
    // Sleep for, for instance, 100ms 
} 

// Now read the output 
while (reader.ready() && ((line = reader.readLine()) != null)) 
{ 
    ... 
} 

channel.disconnect(); 
+0

Nous pouvons télécharger en parallèle plusieurs fichiers avec différentes sessions de JSch SFTP canal droite? – Ajay

+0

Je ne vois aucune raison pourquoi pas –

+0

iam face à bloquer lors de la lecture de la méthode, alors comment puis-je vérifier channelExec.isClosed(), si supposons que la lecture arrive, il ne sortira pas à droite. – Ajay