Search code examples
javaspringspring-integrationsftp

SFTP Integration How do I make my poller work to get the files periodacally


I want to periodacally call the downloadFiles() method, that downloads files from subdirectories of a remote server and puts them locally in a folder with the same name.

I thought this poller makes sure of that, but I believe I don't fully understand the concept of poller.

<int:poller fixed-rate="1000"/>

This is my full code:

SftpOutboundGateway-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
       xsi:schemaLocation="http://www.springframework.org/schema/integration
                           http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
                           http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/integration/sftp 
                           https://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd">

    <import resource="web.xml"/>

    <int-sftp:outbound-gateway id="gateway1" 
                              auto-startup="true"
                              session-factory="sftpSessionFactory"
                              request-channel="receiveChannel"
                              command="mget"
                              local-directory-expression="'C:/temp/cishare/' + headers['register'] "
                              auto-create-local-directory="true"
                              rename-expression=""
                              mode="IGNORE"
                              expression="'/echo_localhost/echo_localhost/' + payload"
                              reply-channel="loggingChannel"
    >
        <int:poller fixed-rate="20"/>
    </int-sftp:outbound-gateway>

    <int:channel id="receiveChannel">
        <int:queue capacity="25"/>
    </int:channel>

    <int:logging-channel-adapter id="loggingChannel" log-full-message="true" logger-name="tapInbound"
                                 level="INFO" />
    
</beans>

SftpTransfer.java

import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

import java.util.ArrayList;

public class SftpTransfer {

    public void downloadFiles(MessageChannel receiveChannel) {
        ArrayList<String> directories = new ArrayList<>();
        directories.add("IN/");
        directories.add("OUT/");

        for (String directory : directories) {
            Message<String> requestMessage = MessageBuilder.withPayload(directory)
                    .setHeader("register", directory)
                    .build();

            receiveChannel.send(requestMessage);
        }
    }
}

App.java

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.MessageChannel;

public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:SftpOutboundGateway-context.xml");

        MessageChannel receiveChannel = (MessageChannel) ctx.getBean("receiveChannel");

        SftpTransfer sftpTransfer = new SftpTransfer();
        sftpTransfer.downloadFiles(receiveChannel);

    }

}

This is a follow up to my other question:

Spring Integration SFTP (xml) Outbound Gateway - copy folderstructure from remote directory to local directory using SPEL Expression and java


Solution

  • The <int-sftp:outbound-gateway> is a variant of the service activator passive endpoint. It does its work only if a message is sent to its request-channel.

    The poller in this endpoint configuration is directly related to that request-channel reference where it makes sense only if such a channel is a QueueChannel.

    What you are talking about is called scheduling. So, you need to configure something which is going to be called periodically and that one will initiate a request for this <int-sftp:outbound-gateway>.

    In Spring Integration for that purpose there is a <int:inbound-channel-adapter> which can be configured with the <poller> and result of this adapter is sent to the output channel.

    You can produce from there as something simple as that list of dirs to poll.

    See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-adapter-namespace-inbound