Add Apache MINA SftpEventListener

- republish certain events as `ApplicationEvent`s.

* * Add ApacheMinaFtplet to provide the same functionality with FTP
* Fix typo

* * Polishing javadocs and event toString() methods
This commit is contained in:
Gary Russell
2019-07-30 10:31:23 -04:00
committed by Artem Bilan
parent b1dfb7bfa3
commit cd0f56bc87
31 changed files with 1477 additions and 15 deletions

View File

@@ -55,6 +55,51 @@ In the preceding example, all application context events that match one of the t
If a downstream component throws an exception, a `MessagingException` that contains the failed message and exception is sent to the channel named 'eventErrorChannel'.
If no `error-channel` is specified and the downstream channels are synchronous, the exception is propagated to the caller.
Using Java to configure the same adapter:
====
[source, java]
----
@Bean
public ApplicationEventListeningMessageProducer eventsAdapter(
MessageChannel eventChannel, MessageChannel eventErrorChannel) {
ApplicationEventListeningMessageProducer producer =
new ApplicationEventListeningMessageProducer();
producer.setEventTypes(example.FooEvent.class, example.BarEvent.class, java.util.Date.class);
producer.setOutputChannel(eventChannel);
producer.setErrorChannel(eventErrorChannel);
return producer;
}
----
====
With the Java DSL:
====
[source, java]
----
@Bean
public ApplicationEventListeningMessageProducer eventsAdapter() {
ApplicationEventListeningMessageProducer producer =
new ApplicationEventListeningMessageProducer();
producer.setEventTypes(example.FooEvent.class, example.BarEvent.class, java.util.Date.class);
return producer;
}
@Bean
public IntegrationFlow eventFlow(ApplicationEventListeningMessageProducer eventsAdapter,
MessageChannel eventErrorChannel) {
return IntegrationFlows.from(eventsAdapter, e -> e.errorChannel(eventErrorChannel))
.handle(...)
...
.get();
}
----
====
[[appevent-outbound]]
=== Sending Spring Application Events
@@ -72,7 +117,7 @@ For convenience, namespace support is provided to configure an `ApplicationEven
----
====
If you use a `PollableChannel` (such as a `Queue`), you can also provide `poller` as a child element of the `outbound-channel-adapter` element.
If you use a `PollableChannel` (such as a `QueueChannel`), you can also provide a `poller` child element of the `outbound-channel-adapter` element.
You can also optionally provide a `task-executor` reference for that poller.
The following example demonstrates both:
@@ -96,3 +141,40 @@ If the payload of the message is an `ApplicationEvent`, it is passed as-is.
Otherwise, the message itself is wrapped in a `MessagingEvent` instance.
Starting with version 4.2, you can configure the `ApplicationEventPublishingMessageHandler` (`<int-event:outbound-channel-adapter>`) with the `publish-payload` boolean attribute to publish to the application context `payload` as is, instead of wrapping it to a `MessagingEvent` instance.
To configure the adapter using Java configuration:
====
[source, java]
----
@Bean
@ServiceActivator(inputChannel = "eventChannel")
public ApplicationEventPublishingMessageHandler eventHandler() {
ApplicationEventPublishingMessageHandler handler =
new ApplicationEventPublishingMessageHandler();
handler.setPublishPayload(true);
return handler;
}
----
====
With the Java DSL:
====
[source, java]
----
@Bean
public ApplicationEventPublishingMessageHandler eventHandler() {
ApplicationEventPublishingMessageHandler handler =
new ApplicationEventPublishingMessageHandler();
handler.setPublishPayload(true);
return handler;
}
@Bean
// MessageChannel is "eventsFlow.input"
public IntegrationFlow eventsOutFlow(ApplicationEventPublishingMessageHandler eventHandler) {
return f -> f.handle(eventHandler);
}
----
====

View File

@@ -1508,3 +1508,54 @@ When using XML configuration, the `<int-ftp:outbound-gateway/>` provides a `sess
NOTE: The `session-callback` is mutually exclusive with the `command` and `expression` attributes.
When configuring with Java, different constructors are available in the https://docs.spring.io/spring-integration/api/org/springframework/integration/ftp/gateway/FtpOutboundGateway.html[`FtpOutboundGateway`] class.
[[ftp-server-events]]
=== Apache Mina FTP Server Events
The `ApacheMinaFtplet`, added in version 5.2, listens for certain Apache Mina FTP server events and publishes them as `ApplicationEvent` s which can be received by any `ApplicationListener` bean, `@EventListener` bean method, or <<./event.adoc#appevent-inbound, Event Inbound Channel Adapter>>.
Currently supported events are:
* `SessionOpenedEvent` - a client session was opened
* `DirectoryCreatedEvent` - a directory was created
* `FileWrittenEvent` - a file was written to
* `PathMovedEvent` - a file or directory was renamed
* `PathRemovedEvent` - a file or directory was removed
* `SessionClosedEvent` - the client has disconnected
Each of these is a subclass of `ApacheMinaFtpEvent`; you can configure a single listener to receive all of the event types.
The `source` property of each event is a `FtpSession`, from which you can obtain information such as the client address; a convenient `getSession()` method is provided on the abstract event.
Events other than session open/close have another property `FtpRequest` which has properties such as the command and arguments.
To configure the server with the listener (which must be a Spring bean), add it to the server factory:
====
[source, java]
----
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();
----
====
To consume these events using a Spring Integration event adapter:
====
[source, java]
----
@Bean
public ApplicationEventListeningMessageProducer eventsAdapter() {
ApplicationEventListeningMessageProducer producer =
new ApplicationEventListeningMessageProducer();
producer.setEventTypes(ApacheMinaFtpEvent.class);
producer.setOutputChannel(eventChannel());
return producer;
}
----
====

View File

@@ -1387,3 +1387,49 @@ When using XML configuration, the `<int-sftp:outbound-gateway/>` provides a `ses
NOTE: The `session-callback` is mutually exclusive with the `command` and `expression` attributes.
When configuring with Java, the `SftpOutboundGateway` class offers different constructors.
[[sftp-server-events]]
=== Apache Mina SFTP Server Events
The `ApacheMinaSftpEventListener`, added in version 5.2, listens for certain Apache Mina SFTP server events and publishes them as `ApplicationEvent` s which can be received by any `ApplicationListener` bean, `@EventListener` bean method, or <<./event.adoc#appevent-inbound, Event Inbound Channel Adapter>>.
Currently supported events are:
* `SessionOpenedEvent` - a client session was opened
* `DirectoryCreatedEvent` - a directory was created
* `FileWrittenEvent` - a file was written to
* `PathMovedEvent` - a file or directory was renamed
* `PathRemovedEvent` - a file or directory was removed
* `SessionClosedEvent` - the client has disconnected
Each of these is a subclass of `ApacheMinaSftpEvent`; you can configure a single listener to receive all of the event types.
The `source` property of each event is a `ServerSession`, from which you can obtain information such as the client address; a convenient `getSession()` method is provided on the abstract event.
To configure the server with the listener (which must be a Spring bean), simply add it to the `SftpSubsystemFactory`:
====
[source, java]
----
server = SshServer.setUpDefaultServer();
...
SftpSubsystemFactory sftpFactory = new SftpSubsystemFactory();
sftpFactory.addSftpEventListener(apacheMinaSftpEventListenerBean);
...
----
====
To consume these events using a Spring Integration event adapter:
====
[source, java]
----
@Bean
public ApplicationEventListeningMessageProducer eventsAdapter() {
ApplicationEventListeningMessageProducer producer =
new ApplicationEventListeningMessageProducer();
producer.setEventTypes(ApacheMinaSftpEvent.class);
producer.setOutputChannel(eventChannel());
return producer;
}
----
====

View File

@@ -49,6 +49,12 @@ See <<./scripting.adoc#scripting,Scripting Support>> for more information.
The `FluxAggregatorMessageHandler` is now available for grouping and windowing messages logic based on the Project Reactor `Flux` operators.
See <<./aggregator.adoc#flux-aggregator,Flux Aggregator>> for more information.
[[x5.2-sftp-events]]
==== FTP/SFTP Event Publisher
The FTP and SFTP modules now provide an event listener for certain Apache Mina FTP/SFTP server events.
See <<./ftp.adoc#ftp-server-events, Apache Mina FTP Server Events>> and <<./sftp.adoc#sftp-server-events, Apache Mina SFTP Server Events>> for more information.
[[x5.2-general]]
=== General Changes