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