[英]Spring Integration: Http with SFTP Gateway
I am trying to Connect both Http and SFTP Gateways using Spring Integeration...and wants to read list of files, ie running LS command. 我正在尝试使用Spring Integeration连接Http和SFTP网关...,并想读取文件列表,即运行LS命令。
This is my code: 这是我的代码:
// Spring Integration Configuration.. // Spring Integration Configuration ..
@Bean(name = "sftp.session.factory")
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setPort(port);
factory.setHost(host);
factory.setUser(user);
factory.setPassword(password);
factory.setAllowUnknownKeys(allowUnknownKeys);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean(name = "remote.file.template")
public RemoteFileTemplate<LsEntry> remoteFileTemplate() {
RemoteFileTemplate<LsEntry> remoteFileTemplate = new RemoteFileTemplate<LsEntry>(sftpSessionFactory());
remoteFileTemplate.setRemoteDirectoryExpression(new LiteralExpression(remoteDirectory));
return remoteFileTemplate;
}
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
return Pollers.fixedRate(500).get();
}
/* SFTP READ OPERATION CONFIGURATIONS */
@Bean(name = "http.get.integration.flow")
@DependsOn("http.get.error.channel")
public IntegrationFlow httpGetIntegrationFlow() {
return IntegrationFlows
.from(httpGetGate())
.channel(httpGetRequestChannel())
.handle("sftpService", "performSftpReadOperation")
.get();
}
@Bean
public MessagingGatewaySupport httpGetGate() {
RequestMapping requestMapping = new RequestMapping();
requestMapping.setMethods(HttpMethod.GET);
requestMapping.setPathPatterns("/api/sftp/ping");
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway();
gateway.setRequestMapping(requestMapping);
gateway.setRequestChannel(httpGetRequestChannel());
gateway.setReplyChannel(httpGetResponseChannel());
gateway.setReplyTimeout(20000);
return gateway;
}
@Bean(name = "http.get.error.channel")
public IntegrationFlow httpGetErrorChannel() {
return IntegrationFlows.from("rejected").transform("'Error while processing request; got' + payload").get();
}
@Bean
@ServiceActivator(inputChannel = "sftp.read.request.channel")
public MessageHandler sftpReadHandler(){
return new SftpOutboundGateway(remoteFileTemplate(), Command.LS.getCommand(), "payload");
}
@Bean(name = "http.get.request.channel")
public MessageChannel httpGetRequestChannel(){
return new DirectChannel(); //new QueueChannel(25);
}
@Bean(name = "http.get.response.channel")
public MessageChannel httpGetResponseChannel(){
return new DirectChannel(); //new QueueChannel(25);
}
@Bean(name = "sftp.read.request.channel")
public MessageChannel sftpReadRequestChannel(){
return new DirectChannel(); //new QueueChannel(25);
}
@Bean(name = "sftp.read.response.channel")
public MessageChannel sftpReadResponseChannel(){
return new DirectChannel(); //new QueueChannel(25);
}
// Gateway //网关
@MessagingGateway(name="sftpGateway")
public interface SftpMessagingGateway {
@Gateway(requestChannel = "sftp.read.request.channel", replyChannel = "sftp.read.response.channel")
@Description("Handles Sftp Outbound READ Request")
Future<Message> readListOfFiles();
}
// ServiceActivator, ie main logic. // ServiceActivator,即主要逻辑。
@Autowired
private SftpMessagingGateway sftpGateway;
@ServiceActivator(inputChannel = "http.get.request.channel", outputChannel="http.get.response.channel")
public ResponseEntity<String> performSftpReadOperation(Message<?> message) throws ExecutionException, InterruptedException {
System.out.println("performSftpReadOperation()");
ResponseEntity<String> responseEntity;
Future<Message> result = sftpGateway.readListOfFiles();
while(!result.isDone()){
Thread.sleep(300);
System.out.println("Waitign.....");
}
if(Objects.nonNull(result)){
List<SftpFileInfo> listOfFiles = (List<SftpFileInfo>) result.get().getPayload();
System.out.println("Sftp File Info: "+listOfFiles);
responseEntity = new ResponseEntity<String>("Sftp Server is UP and Running", HttpStatus.OK);
}
else {
responseEntity = new ResponseEntity<String>("Error while acessing Sftp Server. Please try again later!!!", HttpStatus.SERVICE_UNAVAILABLE);
}
return responseEntity;
}
Whenever I hit the end-point ("/api/sftp/ping") it went to a loop of: 每当我到达终点(“ / api / sftp / ping”)时,都会进入以下循环:
performSftpReadOperation() Waitign..... performSftpReadOperation()等待中.....
performSftpReadOperation() Waitign..... performSftpReadOperation()等待中.....
performSftpReadOperation() Waitign..... performSftpReadOperation()等待中.....
performSftpReadOperation() Waitign..... performSftpReadOperation()等待中.....
performSftpReadOperation() Waitign..... performSftpReadOperation()等待中.....
Kindly guide me how to fix this issue. 请指导我如何解决此问题。 There might be some issue with httpGetIntegrationFlow().
httpGetIntegrationFlow()可能存在一些问题。 Thanks
谢谢
Your problem that your @Gateway
is without any parameters, meanwhile you do LS command in the SftpOutboundGateway
against payload
expression, which means "give me a remote directory to list" . 您的问题是您的
@Gateway
没有任何参数,同时您在SftpOutboundGateway
针对payload
表达式执行了LS命令,这意味着“给我一个要列出的远程目录”。
So, you need to consider to specify a particular argument for the gateway method with the value as a remote directory to list files in it. 因此,您需要考虑为网关方法指定一个特定的参数,并将其值作为远程目录以列出其中的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.