Apache Mina FTP 伺服器事件

在版本 5.2 中新增的 ApacheMinaFtplet 會監聽特定的 Apache Mina FTP 伺服器事件,並將它們作為 ApplicationEvent 釋出。這些事件可以由任何 ApplicationListener bean、@EventListener bean 方法或 事件入站通道介面卡 接收。

當前支援的事件有:

  • SessionOpenedEvent - 客戶端會話已開啟

  • DirectoryCreatedEvent - 已建立目錄

  • FileWrittenEvent - 已寫入檔案

  • PathMovedEvent - 檔案或目錄已重新命名

  • PathRemovedEvent - 檔案或目錄已移除

  • SessionClosedEvent - 客戶端已斷開連線

這些事件都是 ApacheMinaFtpEvent 的子類;您可以配置一個監聽器來接收所有事件型別。每個事件的 source 屬性都是一個 FtpSession,您可以從中獲取諸如客戶端地址之類的資訊;抽象事件上提供了便捷的 getSession() 方法。

除了會話開啟/關閉事件之外,其他事件還有一個 FtpRequest 屬性,該屬性包含命令和引數等資訊。

要使用監聽器(必須是 Spring bean)配置伺服器,請將其新增到伺服器工廠中

FtpServerFactory serverFactory = new FtpServerFactory();
...
ListenerFactory factory = new ListenerFactory();
...
serverFactory.addListener("default", factory.createListener());
serverFactory.setFtplets(new HashMap<>(Collections.singletonMap("springFtplet", apacheMinaFtpletBean)));
server = serverFactory.createServer();
server.start();

要使用 Spring Integration 事件介面卡消費這些事件

@Bean
public ApplicationEventListeningMessageProducer eventsAdapter() {
    ApplicationEventListeningMessageProducer producer =
        new ApplicationEventListeningMessageProducer();
    producer.setEventTypes(ApacheMinaFtpEvent.class);
    producer.setOutputChannel(eventChannel());
    return producer;
}