2013-04-02 6 views
6

以下のコードの実行が終了すると、netstat -a|grep sftpは開いているSFTP接続を示します。また、JProfilerでオープンな接続として表示されます。 ついに版画でJSCHチャンネルを閉じた後にSFTP接続がまだ存在するのはなぜですか?

channel.isConnected()。なぜ私は迷子になって接続が閉じていないすべてのアイデアですか?

public static void clean() { 
    com.jcraft.jsch.ChannelSftp channel = null; 
    try { 
     channel = Helper.openNewTLSftpChannel(); 
     channel.connect(); 
     channel.cd(remoteFileDirectory); 

     List<ChannelSftp.LsEntry> list = channel.ls("*." + fileType); 
     for (ChannelSftp.LsEntry file : list) { 
      String fileName = file.getFilename(); 
      DateTime fileDate = new DateTime(parseDateFromFileName(fileName)); 

      //if this file is older than the cutoff date, delete from the SFTP share 
      if (fileDate.compareTo(cleanupCutoffdate) < 0) { 
       channel.rm(fileName); 
      } 
     } 
    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } finally { 
     if (channel != null) { 
      channel.disconnect(); 
      System.out.println(channel.isConnected()); 
     } 
    } 
} 

以下openNewTLSftpChannel()の追加:

public static ChannelSftp openNewSftpChannel(String privateKeyFileName, String password, String username, String host, int port) 
     throws ConfigurationErrorException { 

    JSch jsch = new JSch(); 
    File sftpPrivateFile = new File(privateKeyFileName); 
    Channel channel; 
    try { 
     if (!sftpPrivateFile.canRead()) { 
      throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath()); 
     } 
     jsch.addIdentity(sftpPrivateFile.getAbsolutePath(), password); 
     Session session = jsch.getSession(username, host, port); 
     java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "no"); 
     session.setConfig(config); 
     session.connect(); 
     channel = session.openChannel("sftp"); 
    } catch (JSchException jschException) { 
     throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath()); 
    } 
    return (ChannelSftp) channel; 
} 

答えて

14

あなたがSFTP用JSCH examplesを見てみる場合は、セッションが終了する方法を説明します:

//setup Session here 
... 
session.connect(); 
... 


Channel channel = session.openChannel("sftp"); 
channel.connect(); 
ChannelSftp sftpChannel = (ChannelSftp) channel; 

...run sftp logic... 

//close sessions here 
sftpChannel.exit(); 
session.disconnect(); 

あなたは接続と切断の2つの部分があることに気付くでしょう。 SessionオブジェクトとChannelオブジェクトを返します。

私のコードでは、私は認証情報を設定するためにセッションオブジェクトを使用し、必要なsftpコマンドを実行するにはChannelオブジェクトを使用します。

あなたのインスタンスでは、openNewSftpChannelメソッドでSessionオブジェクトを作成していますが、決して閉じられないため、セッションはそのまま残ります。

詳細については、例をご覧ください。

+0

一つは(同時にだけでなく、順次両方)のセッションの内部に複数のチャネルを持つことができ、したがって、それはセッションがあることは良いことですが1つのチャンネルが閉じているときに自動閉鎖されません。 –

+0

これは真実ではありません。他に何もない。 –

+0

sftpChannel.exit()は、ChannelSftpが継承するChannelクラス(Channel.javaの494行目)を介してdisconnectメソッドを呼び出し、session.disconnectは独自のdisconnectメソッド(Session.javaの1545行目)を介して呼び出されます。 –

4

Robert Hが正しい場合、チャンネルを終了してセッションを切断する必要があります。私は、チャンネルが閉じていてもセッションが存在すると付け加えたいと思っていました。メソッド内のtryブロック内にセッションを作成するので、セッションを失ったように見えますが、sftpChannelチャネルで 'getSession'を使用して戻すことができます。

あなたは最終的にこれにブロックする変更できます。

} finally { 
    if (channel != null) { 
     Session session = channel.getSession(); 
     channel.disconnect(); 
     session.disconnect(); 
     System.out.println(channel.isConnected()); 
    } 
} 
関連する問題