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