Apache Mina SFTP 服务器事件
ApacheMinaSftpEventListener
在 5.2 版本中添加,它监听特定的 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;
}