我有一個非常簡單的Spring Boot應用程序,它提供了幾個寧靜的端點,這應該是驅動一個sftp文件上傳到sftp服務器。我的要求是,如果有多個文件,文件應該排隊。我期望通過sftp spring集成工作流程的默認行爲來實現這一點,因爲我讀到了DirectChannel自動排隊文件。要測試行爲,我執行以下操作:Spring集成中的REST端點使消息傳遞通道多線程
- 發送一個大文件,通過調用端點阻塞通道一段時間。
- 通過調用端點來發送較小的文件。
預期結果:較小的文件排隊到一個通道上並在較大的文件上載完成後處理。 實際結果:打開了一個到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);
}
}
DemoController.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);
}
}
我一個完整的新手去春來,我不知道完全是我的sftp頻道在這裏得到了正確的創建,我的猜測是每當我做一次sendToSftp調用時都會創建一個新頻道。值得讚賞的是,在這種情況下如何實現隊列行爲的任何幫助。
嗨,所以我有一個建立在這個問題。我怎樣才能讓sendToSftp返回一些轉移狀態?我嘗試將void更改爲String或Object,將響應通道或錯誤通道添加到消息傳遞網關,但沒有任何結果,有些幫助將不勝感激 – user2334207
請參閱我的答案中的UPDATE。 –
如何捕獲異常?我試着聯繫一個沒有權限的非現有服務器或文件夾,然後獲得一個大量的堆棧跟蹤,它似乎在頂部顯示MessagingException。我在網關的sendToSftp上添加了MessagingException,並且在DemoController中包含了try catch,但是如果發生異常,sendToSftp根本不會返回 – user2334207