簡體   English   中英

春季集成:帶有SFTP網關的Http

[英]Spring Integration: Http with SFTP Gateway

我正在嘗試使用Spring Integeration連接Http和SFTP網關...,並想讀取文件列表,即運行LS命令。

這是我的代碼:

// 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);
}

//網關

@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()等待中.....

performSftpReadOperation()等待中.....

performSftpReadOperation()等待中.....

performSftpReadOperation()等待中.....

performSftpReadOperation()等待中.....

請指導我如何解決此問題。 httpGetIntegrationFlow()可能存在一些問題。 謝謝

您的問題是您的@Gateway沒有任何參數,同時您在SftpOutboundGateway針對payload表達式執行了LS命令,這意味着“給我一個要列出的遠程目錄”。

因此,您需要考慮為網關方法指定一個特定的參數,並將其值作為遠程目錄以列出其中的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM