我試圖在本地文件夾中生成一個文件,然後通過SFTP將它發送到遠程服務器,最後刪除創建的'臨時'文件。幾乎所有的工作都很好。MessageChannel.send方法何時返回?
問題: 當我嘗試刪除文件時,使用File.delete(或Files.delete),它不會這樣做。原因:文件被另一個進程使用。請注意,沒有其他進程(除了通過sftp發送的進程)使用該文件。
處理下面的文件代碼:
List<Order> orders = orderRepository.findAll();
try (FileWriter fileWriter = new FileWriter(tempFile)) {
contentCreator.generateContent(orders, fileWriter);
LOGGER.debug("Handover file written successfully " + tempFile.getAbsolutePath());
} catch (IOException e) {
throw new IllegalStateException("Cannot write in the temp folder. Handover failed.", e);
}
final Message<File> message = MessageBuilder.withPayload(tempFile).build();
if (sftpChannel.send(message)) { //sftpChannel comes autowired by Spring IoC
doLogicInCaseOfSuccess(); //I set some DB records after the file was sent successfully over sftp.
tempFile.delete(0);
}
這裏是配置:
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${host}"/>
<property name="port" value="${port}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<int:channel id="apstChannel"/>
<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter"
session-factory="sftpSessionFactory"
channel="apstChannel"
charset="UTF-8"
remote-directory="${dropFolder}"
/>
我不明白的是該文件是怎麼來仍然在使用,即使MessageChannel.send
方法返回true(因此轉移成功)?
因爲我找不到任何文檔,現在我不相信當文件排隊等待通過sftp發送或實際發送後,該方法將返回true。你知道何時send
方法返回?
此外,如果您知道其他方式如何刪除該文件,那就太棒了。
看起來這不是SFTP過程的問題。如何在發送到SFTP之前保留'tempFile'?也許你忘了關閉文件流? –
我已經添加了寫入文件的代碼。當存在try-catch塊時(使用java 7),流將被關閉。 – Stef
由於Spring Integration基於消息隊列,似乎不可能處理任何同步並從作業中獲取反饋。雖然,我在Spring Integration方面很初學,但我可能在這裏錯過了一些東西。 – Stef