我有一个非常简单的 Spring Boot 应用程序,它提供了几个 restful 端点,这应该驱动一个 sftp 文件被上传到一个 sftp 服务器。我的要求是,如果有多个文件,则应将文件排队。我希望通过 sftp spring 集成工作流的默认行为来实现这一点,因为我读到 DirectChannel 会自动对文件进行排队。为了测试行为,我执行以下操作:
- 发送一个大文件,通过调用端点阻塞通道一段时间。
- 通过调用端点发送一个较小的文件。
预期结果:较小的文件被排队到通道上,并在较大的文件上传完成后处理。实际结果:与 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 调用时都会创建一个新频道。在这种情况下如何实现队列行为的任何帮助将不胜感激。