Reputation: 281
I need to check the health of the SFTP connection. How to check SFTP connection successful or not in Spring boot(with a try-catch)? Which library should I use to check the SFTP connection in Spring?
Upvotes: 2
Views: 2041
Reputation: 10675
Add a custom SFTP Health Indicator:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.integration.sftp.session.SftpSession;
public class SftpHealthIndicator implements HealthIndicator {
private final Session sftpSession;
public SftpHealthIndicator(Session sftpSession) {
this.sftpSession = sftpSession;
}
@Override
public Health health() {
try {
if (sftpSession.isOpen()) {
return Health.up().build();
} else {
return Health.down().build();
}
} catch (Exception e) {
return Health.down().withDetail("Error", ex.getMessage()).build();
}
}
}
Upvotes: 0
Reputation: 496
Please check this link. Hope you will get your answer. Though I am copying some code snippet for your help.
.....
jsch = new JSch();
session = jsch.getSession(user, hostIP, port);
session.setPassword(Password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(getTimeInSecond() * 1000);
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(LocationPath);
...
Thanks
Upvotes: 1