2

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?

Muhammad Saimon
  • 233
  • 2
  • 10
  • 1
    Hi Saimon, I would like to suggest you to use `JSCH` library. You can manually check SFTP authentication and path as well with this. Better of you add more tags like : **JAVA**, **spring-integration** Thanks. – Towfiqul Islam Jun 15 '20 at 00:51

2 Answers2

1

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

Towfiqul Islam
  • 456
  • 6
  • 14
0

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();
        }
    }
}
Javier Aviles
  • 7,952
  • 2
  • 22
  • 28