[英]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.