Muhammad Saimon
Muhammad Saimon

Reputation: 281

How to check SFTP connection successful or not in Spring (with a try catch)? Which Library should I use to check SFTP connection in Spring?

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

Answers (2)

Javier Aviles
Javier Aviles

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

Towfiqul Islam
Towfiqul Islam

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

Related Questions