我正在尝试使用Spring Integeration连接Http和SFTP网关...并想读取文件列表,即运行LS命令。
这是我的代码:
// Spring集成配置..
@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);
}
//网关
@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,即主要逻辑。
@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;
}
每当我到达终点(“ / api / sftp / ping”)时,它就会进入以下循环:
performSftpReadOperation() Waitign .....
performSftpReadOperation() Waitign .....
performSftpReadOperation() Waitign .....
performSftpReadOperation() Waitign .....
performSftpReadOperation() Waitign .....
请指导我如何解决此问题。 httpGetIntegrationFlow()可能存在一些问题。 谢谢
答案 0 :(得分:0)
您的问题@Gateway
没有任何参数,同时您在SftpOutboundGateway
中对payload
表达式执行LS命令,这意味着“给我一个要列出的远程目录”。
因此,您需要考虑为网关方法指定一个特定的参数,并将其值作为远程目录以列出其中的文件。