2

我有一个非常简单的 Spring Boot 应用程序,它提供了几个 restful 端点,这应该驱动一个 sftp 文件被上传到一个 sftp 服务器。我的要求是,如果有多个文件,则应将文件排队。我希望通过 sftp spring 集成工作流的默​​认行为来实现这一点,因为我读到 DirectChannel 会自动对文件进行排队。为了测试行为,我执行以下操作:

  1. 发送一个大文件,通过调用端点阻塞通道一段时间。
  2. 通过调用端点发送一个较小的文件。

预期结果:较小的文件被排队到通道上,并在较大的文件上传完成后处理。实际结果:与 sftp 服务器的新连接打开,较小的文件上传到那里而没有排队,而较大的文件继续传输。

我的应用程序中有两个文件:

DemoApplication.java

@SpringBootApplication
@IntegrationComponentScan
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("localhost");
        factory.setPort(22);
        factory.setUser("tester");
        factory.setPassword("password");
        factory.setAllowUnknownKeys(true);
        return factory;
    }

    @Bean
    @ServiceActivator(inputChannel = "toSftpChannel")
    public MessageHandler handler() {
        SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression("/"));
        return handler;
    }

    @MessagingGateway
    public interface MyGateway {

         @Gateway(requestChannel = "toSftpChannel")
         void sendToSftp(File file);
    }
}

演示控制器.java

@RestController
public class DemoController {

    @Autowired
    MyGateway gateway;

    @RequestMapping("/sendFile")
    public void sendFile() {
        File file = new File("C:/smallFile.txt");
        gateway.sendToSftp(file);
    }

    @RequestMapping("/sendBigFile")
    public void sendBigFile() {
        File file = new File("D:/bigFile.zip");
        gateway.sendToSftp(file);
    }
}

我是 spring 的完全新手,我不确定我的 sftp 频道是否在这里正确创建,我的猜测是每次我进行 sendToSftp 调用时都会创建一个新频道。在这种情况下如何实现队列行为的任何帮助将不胜感激。

4

1 回答 1

2

您在这里没有队列,因为每个 HTTP 请求都在其自己的线程中执行。是的,当 http 线程池用尽时,您可能仍然在那里排队,但这在您的简单用例中看起来并不只有两个请求。

无论如何,您都可以在那里实现队列行为,但您应该将您的声明toSftpChannelQueueChannelbean。

这样下游进程将始终在同一个线程上执行,并且下一条消息恰好在第一条消息之后从队列中拉出。

有关详细信息,请参阅参考手册

更新

由于您使用FtpMessageHandlerwhich 是单向组件,但您仍然需要对 MVC 控制器的方法进行一些回复,唯一的方法是使用不返回的@Gateway方法void,当然我们需要以某种方式发送回复。

为此,我建议使用PublishSubscribeChannel

@Bean
@BridgeTo
public MessageChannel toSftpChannel() {
    return new PublishSubscribeChannel();
}

@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
@Order(0)
public MessageHandler handler() {
    SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression("/"));
    return handler;
}

这样我们就有了两个订阅者toSftpChannel@Order(0)我们确保这是第@ServiceActivator一个订阅者,因为我们需要先执行 SFTP 传输。跟@BridgeTo我们加一秒BridgeHandler一样PublishSubscribeChannel。它的目的只是获取一个replyChannel标头并在那里发送请求消息。由于我们不使用任何线程,因此BridgeHandler将在传输到 SFTP 完成后准确执行。

当然,BridgeHandler你可以有任何其他的@ServiceActivator,或者@Transfromer作为回复而不是请求返回File,而是其他任何东西。例如:

@ServiceActivator(inputChannel = "toSftpChannel")
@Order(1)
public String transferComplete(File payload) {
    return "The SFTP transfer complete for file: " + payload;
}
于 2017-07-28T01:45:56.290 回答