Skip to content

Commit 71fbe29

Browse files
artembilangaryrussell
authored andcommitted
Add Docs for Flux Aggregator
* Some test polishing * Fix not properly wrapped code snippet in the `scripting.adoc` Doc polishing
1 parent 2fb7554 commit 71fbe29

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

spring-integration-core/src/test/java/org/springframework/integration/dsl/correlation/CorrelationHandlerTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ public void testSplitterDiscard() {
177177

178178
@Test
179179
public void testFluxAggregator() {
180-
IntegrationFlow testFlow = (flow) ->
181-
flow.split()
180+
IntegrationFlow testFlow =
181+
(flow) -> flow
182+
.split()
182183
.channel(MessageChannels.flux())
183184
.handle(new FluxAggregatorMessageHandler());
184185

@@ -189,7 +190,7 @@ public void testFluxAggregator() {
189190
@SuppressWarnings("unchecked")
190191
Flux<Message<?>> window =
191192
registration.getMessagingTemplate()
192-
.convertSendAndReceive(new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, Flux.class);
193+
.convertSendAndReceive(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, Flux.class);
193194

194195
assertThat(window).isNotNull();
195196

src/reference/asciidoc/aggregator.adoc

+63
Original file line numberDiff line numberDiff line change
@@ -865,3 +865,66 @@ For example, the `JdbcMessageStore` has a `region` property, and the `MongoDbMes
865865
866866
For more information about the `MessageStore` interface and its implementations, see <<./message-store.adoc#message-store,Message Store>>.
867867
=====
868+
869+
[[flux-aggregator]]
870+
==== Flux Aggregator
871+
872+
In version 5.2, the `FluxAggregatorMessageHandler` component has been introduced.
873+
It is based on the Project Reactor `Flux.groupBy()` and `Flux.window()` operators.
874+
The incoming messages are emitted into the `FluxSink` initiated by the `Flux.create()` in the constructor of this component.
875+
If the `outputChannel` is not provided or it is not an instance of `ReactiveStreamsSubscribableChannel`, the subscription to the main `Flux` is done from the `Lifecycle.start()` implementation.
876+
Otherwise it is postponed to the subscription done by the `ReactiveStreamsSubscribableChannel` implementation.
877+
The messages are grouped by the `Flux.groupBy()` using a `CorrelationStrategy` for the group key.
878+
By default, the `IntegrationMessageHeaderAccessor.CORRELATION_ID` header of the message is consulted.
879+
880+
By default every closed window is released as a `Flux` in payload of a message to produce.
881+
This message contains all the headers from the first message in the window.
882+
This `Flux` in the output message payload must be subscribed and processed downstream.
883+
Such a logic can be customized (or superseded) by the `setCombineFunction(Function<Flux<Message<?>>, Mono<Message<?>>>)` configuration option of the `FluxAggregatorMessageHandler`.
884+
For example, if we would like to have a `List` of payloads in the final message, we can configure a `Flux.collectList()` like this:
885+
886+
====
887+
[source,java]
888+
----
889+
fluxAggregatorMessageHandler.setCombineFunction(
890+
(messageFlux) ->
891+
messageFlux
892+
.map(Message::getPayload)
893+
.collectList()
894+
.map(GenericMessage::new));
895+
----
896+
====
897+
898+
There are several options in the `FluxAggregatorMessageHandler` to select an appropriate window strategy:
899+
900+
* `setBoundaryTrigger(Predicate<Message<?>>)` - is propagated to the `Flux.windowUntil()` operator.
901+
See its JavaDocs for more information.
902+
Has a precedence over all other window options.
903+
* `setWindowSize(int)` and `setWindowSizeFunction(Function<Message<?>, Integer>)` - is propagated to the `Flux.window(int)` or `windowTimeout(int, Duration)`.
904+
By default a window size is calculated from the first message in group and its `IntegrationMessageHeaderAccessor.SEQUENCE_SIZE` header.
905+
* `setWindowTimespan(Duration)` - is propagated to the `Flux.window(Duration)` or `windowTimeout(int, Duration)` depending in the window size configuration.
906+
* `setWindowConfigurer(Function<Flux<Message<?>>, Flux<Flux<Message<?>>>>)` - a function to apply a transformation into the grouped fluxes for any custom window operation not covered by the exposed options.
907+
908+
Since this component is a `MessageHandler` implementation it can simply be used as a `@Bean` definition together with a `@ServiceActivator` messaging annotation.
909+
With Java DSL it can be used from the `.handle()` EIP-method.
910+
The sample below demonstrates how we can register an `IntegrationFlow` at runtime and how a `FluxAggregatorMessageHandler` can be correlated with a splitter upstream:
911+
912+
====
913+
[source,java]
914+
----
915+
IntegrationFlow fluxFlow =
916+
(flow) -> flow
917+
.split()
918+
.channel(MessageChannels.flux())
919+
.handle(new FluxAggregatorMessageHandler());
920+
921+
IntegrationFlowContext.IntegrationFlowRegistration registration =
922+
this.integrationFlowContext.registration(fluxFlow)
923+
.register();
924+
925+
@SuppressWarnings("unchecked")
926+
Flux<Message<?>> window =
927+
registration.getMessagingTemplate()
928+
.convertSendAndReceive(new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, Flux.class);
929+
----
930+
====

src/reference/asciidoc/scripting.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ In addition you need to add a script engine implementation, e.g. JRuby, Jython.
3030
Starting with version 5.2, Spring Integration provides a Kotlin Jsr223 support.
3131
You need to add these dependencies into your project to make it working:
3232

33+
====
3334
[source, groovy]
3435
----
3536
runtime 'org.jetbrains.kotlin:kotlin-script-util'

src/reference/asciidoc/whats-new.adoc

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,30 @@ If you are interested in more details, see the Issue Tracker tickets that were r
2020
[[x5.2-new-components]]
2121
=== New Components
2222

23-
[[x5.2-rateLimitAdvice]]
23+
[[x5.2-rate-limit-advice]]
2424
=== Rate Limit Advice Support
2525

2626
The `RateLimiterRequestHandlerAdvice` is now available for limiting requests rate on handlers.
2727
See <<./handler-advice.adoc#rate-limiter-advice,Rate Limiter Advice>> for more information.
2828

29-
[[x5.2-cacheAdvice]]
29+
[[x5.2-cache-advice]]
3030
=== Caching Advice Support
3131

3232
The `CacheRequestHandlerAdvice` is now available for caching request results on handlers.
3333
See <<./handler-advice.adoc#cache-advice,Caching Advice>> for more information.
3434

35-
[[x5.2-kotlinScripts]]
35+
[[x5.2-kotlin-scripts]]
3636
=== Kotlin Scripts Support
3737

3838
The JSR223 scripting module now includes a support for Kotlin scripts.
3939
See <<./scripting.adoc#scripting,Scripting Support>> for more information.
4040

41+
[[x5.2-flux-aggregator]]
42+
=== Flux Aggregator Support
43+
44+
The `FluxAggregatorMessageHandler` is now available for grouping and windowing messages logic based on the Project Reactor `Flux` operators.
45+
See <<./aggregator.adoc#flux-aggregator,Flux Aggregator>> for more information.
46+
4147
[[x5.2-general]]
4248
=== General Changes
4349

0 commit comments

Comments
 (0)