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;
}