我正在使用Spring Integration将文件加载到sftp。我有类似的SessionFactory:

<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
        <property name="host" value="${host}"/>
        <property name="port" value="${port}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${password}"/>
        <property name="knownHosts" value="${known.hosts}"/>
</bean>


我需要每隔几个小时检查sftp是否正常。这是在运行状况检查器中使用DefaultSftpSessionFactory的正确方法吗?

public class HealthChecker
{
    @Autowired
    private DefaultSftpSessionFactory sftpSessionFactory;

    public String doHealthCheck()
    {
        try
        {
            SftpSession sftpSession = sftpSessionFactory.getSession();
            if (sftpSession.isOpen())
            {
                sftpSession.close();
                return "OK";
            }
        }
        catch (Exception ex)
        {
            LOG.error("Can't connect to sftp", ex);
        }
        return "ERROR";
    }
}


使用HealthCheck时需要使用一些最佳实践吗?

最佳答案

我认为您的解决方案背后的想法很好,请记住,健康检查服务不应太繁琐。万一您使用任何监视工具(每隔N秒/分钟进行一次轮询),如果运行状况检查服务长期运行,它有时可能会失败。

我还建议使用Spring Actuator,类似这样:

 @Endpoint(id = "sftp")
 @Component
 @RequiredArgsConstructor
 public class SftpEndpoint {
     @ReadOperation
     public Map<String, Object> sftp() {
     }
 }

10-02 00:00
查看更多