1
votes

I need to get all files from all directories and I want to use sftp:outbound-gateway with command = "mget" (of course with filters).

The sample code

<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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration
        http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">

    <bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
                <property name="host" value="host" />
                <property name="user" value="username" />
                <property name="password" value="pass" />
            </bean>

            <int:channel id="output">
                <int:queue />
            </int:channel>

            <int:channel id="inboundMGetRecursive" />
            <int:poller default="true" fixed-delay="5000">
            </int:poller>
            <int-sftp:outbound-gateway session-factory="sftpSessionFactory"
                                      request-channel="inboundMGetRecursive"
                                      command="mget"
                                      expression="payload"
                                      command-options="-R"
                                      local-directory-expression="#remoteDirectory"
                                      reply-channel="output"
                                      remote-directory="${remote-directory}"/>


        </beans>

works only when a code in java creates a message and send it.

    public final class Main {
    private static final Logger LOGGER = Logger.getLogger(Main.class);

    private Main() { }

    /**
     * Load the Spring Integration Application Context
     *
     * @param args - command line arguments
     */
    public static void main(final String... args) {


        final AbstractApplicationContext context =
                new ClassPathXmlApplicationContext("/main/resources/META-INF/spring/integration/spring-integration-context.xml");

        context.registerShutdownHook();

        final DirectChannel requestChannel = (DirectChannel) context.getBean("inboundMGetRecursive");
        final PollableChannel replyChannel = (PollableChannel) context.getBean("output");


        String dir = "/remote_directory/test/sample";
        requestChannel.send(new GenericMessage<Object>(dir + "'*'"));
        Message<?> result = replyChannel.receive(1000);

        List<File> localFiles = (List<File>) result.getPayload();

        for (File file : localFiles) {
            System.out.println(file.getName());
        }

        System.exit(0);

    }
}

I want the code to be called on start up with out a java code, like the sftp:inbound-channel-adapter works. Can it be done? if yes, is there a sample code or an example?

1

1 Answers

0
votes
<int:inbound-channel-adapter channel="inboundMGetRecursive"
       expression="'/remote_directory/test/sample'">
   <int:poller fixed-delay="60000" />
</int:inbound-channel-adapter>

<int:sftp-outbound-gateway ...

Will invoke the gateway once a minute.

If you want to invoke it once, and once only, use a FireOnceTrigger as discussed in this answer

<int:poller trigger="myTrigger" />