Send and receive files from FTP in Spring Boot

russellhoff picture russellhoff · Feb 8, 2017 · Viewed 23.4k times · Source

I'm new to Spring Framework and, indeed, I'm learning and using Spring Boot. Recently, in the app I'm developing, I made Quartz Scheduler work, and now I want to make Spring Integration work there: FTP connection to a server to write and read files from.

What I want is really simple (as I've been able to do so in a previous java application). I've got two Quartz Jobs scheduled to fired in different times daily: one of them reads a file from a FTP server and another one writes a file to a FTP server.

I'll detail what I've developed so far.

@SpringBootApplication
@ImportResource("classpath:ws-config.xml")
@EnableIntegration
@EnableScheduling
public class MyApp extends SpringBootServletInitializer {

    @Autowired
    private Configuration configuration;

    //...

    @Bean
    public DefaultFtpsSessionFactory  myFtpsSessionFactory(){
        DefaultFtpsSessionFactory sess = new DefaultFtpsSessionFactory();
        Ftp ftp = configuration.getFtp();
        sess.setHost(ftp.getServer());
        sess.setPort(ftp.getPort());
        sess.setUsername(ftp.getUsername());
        sess.setPassword(ftp.getPassword());
        return sess;
    }

}

The following class I've named it as a FtpGateway, as follows:

@Component
public class FtpGateway {

    @Autowired
    private DefaultFtpsSessionFactory sess;

    public void sendFile(){
        // todo
    }

    public void readFile(){
        // todo
    }

}

I'm reading this documentation to learn to do so. Spring Integration's FTP seems to be event driven, so I don't know how can I execute either of the sendFile() and readFile() from by Jobs when the trigger is fired at an exact time.

The documentation tells me something about using Inbound Channel Adapter (to read files from a FTP?), Outbound Channel Adapter (to write files to a FTP?) and Outbound Gateway (to do what?):

Spring Integration supports sending and receiving files over FTP/FTPS by providing three client side endpoints: Inbound Channel Adapter, Outbound Channel Adapter, and Outbound Gateway. It also provides convenient namespace-based configuration options for defining these client components.

So, I haven't got it clear as how to follow.

Please, could anybody give me a hint?

Thank you!

EDIT:

Thank you @M. Deinum. First, I'll try a simple task: read a file from the FTP, the poller will run every 5 seconds. This is what I've added:

@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(myFtpsSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setPreserveTimestamp(true);
    fileSynchronizer.setRemoteDirectory("/Entrada");
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.csv"));
    return fileSynchronizer;
}


@Bean
@InboundChannelAdapter(channel = "ftpChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> ftpMessageSource() {
    FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(inbound);
    source.setLocalDirectory(new File(configuracion.getDirFicherosDescargados()));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

@Bean
@ServiceActivator(inputChannel = "ftpChannel")
public MessageHandler handler() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Object payload = message.getPayload();
            if(payload instanceof File){
                File f = (File) payload;
                System.out.println(f.getName());
            }else{
                System.out.println(message.getPayload());
            }
        }

    };
}

Then, when the app is running, I put a new csv file intro "Entrada" remote folder, but the handler() method isn't run after 5 seconds... I'm doing something wrong?

Answer

Mr Nobody picture Mr Nobody · Dec 4, 2018

Please add @Scheduled(fixedDelay = 5000) over your poller method.