Apache Mina SFTP 伺服器事件
版本 5.2 新增的 ApacheMinaSftpEventListener
,會監聽特定的 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;
}