简体   繁体   English

如何从 Spring 的组件中调用配置中的 SFTP 出站网关操作

[英]How to call SFTP Outbound Gateway operations in Configuration from Component in Spring

I have looked here here and am unable to get listFiles to work:我在这里看过,无法让 listFiles 工作:

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("localhost");
        factory.setPort(port);
        factory.setUser("foo");
        factory.setPassword("foo");
        factory.setAllowUnknownKeys(true);
        factory.setTestSession(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @MessagingGateway
    public interface MyGateway {
         @Gateway(requestChannel = "sftpChannel")
         List<File> listFiles();

    }
    @Bean
    @ServiceActivator(inputChannel = "sftpChannel")
    public MessageHandler handler() {
        return new SftpOutboundGateway(ftpSessionFactory(), "ls", "'my_remote_dir/'");
    }

where in my @Component class I have this:在我的@Component class 我有这个:

    @Autowired
    MyGateway gateway;

    public void list(){
        List<File> files = gateway.listFiles();
    }

when I run this, I get an error receive is not supported, because no pollable reply channel has been configured当我运行这个时,我得到一个错误receive is not supported, because no pollable reply channel has been configured

I assume this is an issue with my knowledge/understanding of integration channels.我认为这是我对集成渠道的知识/理解的问题。 Perhaps I am missing a bean, but my main goal here is to do replace my current use of the inboundchannel adapter to request files ad hoc instead of continuously polling the fileserver也许我错过了一个 bean,但我的主要目标是替换我当前使用的入站通道适配器来临时请求文件,而不是连续轮询文件服务器

Yes, the story mentioned in the Spring Integration Gateway with no arguments is definitely related to your problem.是的, 没有arguments的Spring集成网关中提到的故事肯定和你的问题有关。

You are missing the fact that List<File> listFiles() contract comes without arguments, so it is not clear for the framework what to use for sending to that sftpChannel .您错过了List<File> listFiles()合同没有 arguments 的事实,因此框架不清楚使用什么来发送到该sftpChannel Therefore it try to call receive .因此它尝试调用receive But since your sftpChannel is not PollableChannel , you got that error.但是由于您的sftpChannel不是PollableChannel ,因此您遇到了该错误。 Anyway that is a different story and not what you want to get as a reply from sending a message to the sftpChannel as you try to do with that gateway contract.无论如何,这是一个不同的故事,而不是当您尝试使用该网关合同时,您希望从向sftpChannel发送消息得到的回复。

You just need to be more explicit and say what to use as a payload for that no-arg gateway contract.您只需要更明确地说明将什么用作该无参数网关合约的有效负载。

See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-calling-no-argument-methods .在文档中查看更多信息: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-calling-no-argument-methods The @Payload is an answer for you. @Payload是您的答案。 Or you can specify a payloadExpression on that @Gateway annotation or a defaultPayloadExpression on the @MessagingGateway .或者,您可以在@Gateway注释上指定payloadExpression或在@MessagingGateway上指定defaultPayloadExpression

Maybe it is too late, but you also could change your no-arg gateway contract to a one-arg contract having nothing to annotate additionally.也许为时已晚,但您也可以将您的无参数网关合约更改为没有额外注释的单参数合约。

@MessagingGateway
public interface MyGateway {
     @Gateway(requestChannel = "sftpChannel")
     List<File> listFiles(String remoteDir);

}

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
    return new SftpOutboundGateway(ftpSessionFactory(), Command.LS, "payload");
}

In your @Component class then you will have:在您的@Component class 中,您将拥有:

@Autowired
MyGateway gateway;

public void list(){
    List<File> files = gateway.listFiles("'my_remote_dir/'");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用多个sftp出站网关 - How to use multiple sftp outbound-gateway 无法从 Spring 集成出站网关调用 HTTPS 端点 - Cannot call HTTPS endpoint from Spring integration outbound gateway 将文件/文件从远程目录移动到另一个 Spring 集成 SFTP 出站网关 Java 配置/Java DSL - Moving file/files from remote directory to another Spring Integration SFTP Outbound Gateway Java Config/Java DSL Spring SFTP出站网关:如何在Java Config中关闭GET后的会话? - Spring SFTP Outbound Gateway: How to close the session after GET in Java Config? Spring Integeration SFTP出站网关mget -R问题 - Spring Integeration SFTP outbound gateway mget -R issue 设置 Spring Integration SFTP 出站网关时出现 UnsatisfiedDependencyException - UnsatisfiedDependencyException when setting up a Spring Integration SFTP outbound gateway Spring 使用入站适配器消息处理程序中的出站网关的 Sftp 获取文件 - Spring Sftp fetch file using outbound gateway within inbound adapter message handler spring amqp-outbound网关,以产生来自另一个thead的答复(如jms-outbound网关) - spring amqp-outbound gateway to produce reply from a different thead (Like jms-outbound gateway) spring http出站网关定制 - spring http outbound gateway customization 如何在Spring Integration中扩展TCP出站网关以仅接收消息 - How to extend TCP Outbound gateway to just receive messages in Spring Integration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM