spring sftp出站网关

时间:2015-12-16 12:38:26

标签: spring spring-integration sftp

我需要从所有目录中获取所有文件,并且我希望将sftp:outbound-gatewaycommand = "mget"一起使用(当然还有过滤器)。

示例代码

<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>

仅在java中的代码创建消息并发送消息时才有效。

    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);

    }
}

我希望在启动时调用代码,不要使用java代码 sftp:inbound-channel-adapter有效。 可以吗?如果是,是否有示例代码或示例?

1 个答案:

答案 0 :(得分:0)

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

<int:sftp-outbound-gateway ...

每分钟调用一次网关。

如果您想要调用一次,只需调用一次,请使用this answer

中讨论的FireOnceTrigger
<int:poller trigger="myTrigger" />