简体   繁体   中英

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.

This is my code:

// 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.

  @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:

performSftpReadOperation() Waitign.....

performSftpReadOperation() Waitign.....

performSftpReadOperation() Waitign.....

performSftpReadOperation() Waitign.....

performSftpReadOperation() Waitign.....

Kindly guide me how to fix this issue. There might be some issue with 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" .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM