Spring FTP Integration : How to verify FTP session?

442 views Asked by At

I am using following code to create ftpSessionfactory -

@Bean
@Lazy(false)
public SessionFactory<FTPFile> ftpSessionFactory() {
    DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
    sf.setHost(server);
    sf.setPort(port);
    sf.setUsername(username);
    sf.setPassword(password);
    return new CachingSessionFactory<FTPFile>(sf);
}

and following method to check if FTP session is good -

private boolean isFTPSessionOK() {
    try {
        SessionFactory<FTPFile> ftpSessionFactory = ftpSessionFactory();
        boolean open = ftpSessionFactory.getSession().isOpen();
        System.out.println("Session is good ? "+ open);
        return open;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

Is there any other way, to verify FTP Session instead of writing a dumb method?

Thanks

1

There are 1 answers

2
Artem Bilan On BEST ANSWER

Well, yes, you don't need that method if you use CachingSessionFactory already.

Each time to perform ftpSessionFactory.getSession() the pool abstraction ensures to return to you a valid, open instance.