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;
}
© . This site is unofficial and not affiliated with VMware.