首页 文章

spring sftp出站网关

提问于
浏览
1

我需要从所有目录中获取所有文件,我想使用 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
    <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" />
    

相关问题