Apache Mina FTP 服务器事件

ApacheMinaFtplet 在 5.2 版本中添加,它监听特定的 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;
}