Quando il codice sottostante è terminato, netstat -a|grep sftp
mostra una connessione SFTP aperta. Si presenta anche come una connessione aperta in JProfiler.Perché esiste ancora una connessione SFTP dopo che il canale JSCH è stato chiuso?
channel.isConnected()
nelle stampe infine blocco falsi. Qualche idea sul perché le connessioni non vengano chiuse perché sono in perdita?
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());
}
}
}
Aggiunta openNewTLSftpChannel()
di seguito:
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;
}
si può avere più canali all'interno di una sessione (entrambi allo stesso tempo, così come in sequenza), quindi è una buona cosa che la sessione è non chiuso automaticamente quando un canale è chiuso. –
Questo non è vero - esci solo chiamate disconnessione. Nient'altro. –
sftpChannel.exit() chiama il metodo di disconnessione tramite la classe Channel, che ChannelSftp eredita (riga 494 di Channel.java), session.disconnect viene invocato tramite il proprio metodo di disconnessione (riga 1545 di Session.java). –