Apache Mina SFTP 伺服器事件
ApacheMinaSftpEventListener 在 5.2 版本中新增,它監聽特定的 Apache Mina SFTP 伺服器事件,並將其作為 ApplicationEvent 釋出,任何 ApplicationListener bean、@EventListener bean 方法或 事件入站通道介面卡 都可以接收這些事件。
目前,支援的事件有:
-
SessionOpenedEvent- 客戶端會話已開啟 -
DirectoryCreatedEvent- 目錄已建立 -
FileWrittenEvent- 檔案已寫入 -
PathMovedEvent- 檔案或目錄已重新命名 -
PathRemovedEvent- 檔案或目錄已刪除 -
SessionClosedEvent- 客戶端已斷開連線
每個事件都是 ApacheMinaSftpEvent 的子類;您可以配置一個監聽器來接收所有事件型別。每個事件的 source 屬性是一個 ServerSession,您可以從中獲取客戶端地址等資訊;抽象事件上提供了一個方便的 getSession() 方法。
要使用監聽器(必須是 Spring bean)配置伺服器,只需將其新增到 SftpSubsystemFactory 中
server = SshServer.setUpDefaultServer();
...
SftpSubsystemFactory sftpFactory = new SftpSubsystemFactory();
sftpFactory.addSftpEventListener(apacheMinaSftpEventListenerBean);
...
要使用 Spring Integration 事件介面卡消費這些事件
@Bean
public ApplicationEventListeningMessageProducer eventsAdapter() {
ApplicationEventListeningMessageProducer producer =
new ApplicationEventListeningMessageProducer();
producer.setEventTypes(ApacheMinaSftpEvent.class);
producer.setOutputChannel(eventChannel());
return producer;
}