【问题标题】:Spring Integration: Http with SFTP GatewaySpring 集成:带有 SFTP 网关的 Http
【发布时间】:2018-11-20 17:32:48
【问题描述】:

我正在尝试使用 Spring 集成连接 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”)时,它就会进入一个循环:

执行SftpReadOperation() 等待.....

执行SftpReadOperation() 等待.....

执行SftpReadOperation() 等待.....

执行SftpReadOperation() 等待.....

执行SftpReadOperation() 等待.....

请指导我如何解决此问题。 httpGetIntegrationFlow() 可能存在一些问题。 谢谢

【问题讨论】:

  • 配置看起来不错;我建议你打开org.springframework.integration 的调试日志并观察消息流。您可能还需要为 com.jcraft.jsch 启用调试日志记录。

标签: java spring spring-integration spring-integration-dsl spring-integration-sftp


【解决方案1】:

您的@Gateway 没有任何参数的问题,同时您在SftpOutboundGateway 中针对payload 表达式执行LS 命令,这意味着“给我一个远程目录来列出”。

因此,您需要考虑为网关方法指定一个特定参数,其值为远程目录以列出其中的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2014-06-08
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多