Class FluxAggregatorMessageHandler

All Implemented Interfaces:
org.reactivestreams.Subscriber<org.springframework.messaging.Message<?>>, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.context.Lifecycle, org.springframework.core.Ordered, ExpressionCapable, Orderable, MessageProducer, HeaderPropagationAware, IntegrationPattern, NamedComponent, IntegrationManagement, ManageableLifecycle, TrackableComponent, org.springframework.messaging.MessageHandler, reactor.core.CoreSubscriber<org.springframework.messaging.Message<?>>

public class FluxAggregatorMessageHandler
extends AbstractMessageProducingHandler
implements ManageableLifecycle
The AbstractMessageProducingHandler implementation for aggregation logic based on Reactor's Flux.groupBy(java.util.function.Function<? super T, ? extends K>) and Flux.window(int) operators.

The incoming messages are emitted into a FluxSink provided by the Flux.create(java.util.function.Consumer<? super reactor.core.publisher.FluxSink<T>>) initialized in the constructor.

The resulting windows for groups are wrapped into Messages for downstream consumption.

If the AbstractMessageProducingHandler.getOutputChannel() is not a ReactiveStreamsSubscribableChannel instance, a subscription for the whole aggregating Flux is performed in the start() method.

Since:
5.2
  • Constructor Details

    • FluxAggregatorMessageHandler

      public FluxAggregatorMessageHandler()
      Create an instance with a Flux.create(java.util.function.Consumer<? super reactor.core.publisher.FluxSink<T>>) and apply Flux.groupBy(java.util.function.Function<? super T, ? extends K>) and Flux.window(int) transformation into it.
  • Method Details

    • setCorrelationStrategy

      public void setCorrelationStrategy​(CorrelationStrategy correlationStrategy)
      Configure a CorrelationStrategy to determine a group key from the incoming messages. By default a HeaderAttributeCorrelationStrategy is used against a IntegrationMessageHeaderAccessor.CORRELATION_ID header value.
      Parameters:
      correlationStrategy - the CorrelationStrategy to use.
    • setCombineFunction

      public void setCombineFunction​(java.util.function.Function<reactor.core.publisher.Flux<org.springframework.messaging.Message<?>>,​reactor.core.publisher.Mono<org.springframework.messaging.Message<?>>> combineFunction)
      Configure a transformation Function to apply for a Flux window to emit. Requires a Mono result with a Message as value as a combination result of the incoming Flux for window. By default a Flux for window is fully wrapped into a message with headers copied from the first message in window. Such a Flux in the payload has to be subscribed and consumed downstream.
      Parameters:
      combineFunction - the Function to use for result windows transformation.
    • setBoundaryTrigger

      public void setBoundaryTrigger​(java.util.function.Predicate<org.springframework.messaging.Message<?>> boundaryTrigger)
      Configure a Predicate for messages to determine a window boundary in the Flux.windowUntil(java.util.function.Predicate<T>) operator. Has a precedence over any other window configuration options.
      Parameters:
      boundaryTrigger - the Predicate to use for window boundary.
      See Also:
      Flux.windowUntil(Predicate)
    • setWindowSize

      public void setWindowSize​(int windowSize)
      Specify a size for windows to close. Can be combined with the setWindowTimespan(Duration).
      Parameters:
      windowSize - the size for window to use.
      See Also:
      Flux.window(int), Flux.windowTimeout(int, Duration)
    • setWindowSizeFunction

      public void setWindowSizeFunction​(java.util.function.Function<org.springframework.messaging.Message<?>,​java.lang.Integer> windowSizeFunction)
      Specify a Function to determine a size for windows to close against the first message in group. Tne result of the function can be combined with the setWindowTimespan(Duration). By default an IntegrationMessageHeaderAccessor.SEQUENCE_SIZE header is consulted.
      Parameters:
      windowSizeFunction - the Function to use to determine a window size against a first message in the group.
      See Also:
      Flux.window(int), Flux.windowTimeout(int, Duration)
    • setWindowTimespan

      public void setWindowTimespan​(java.time.Duration windowTimespan)
      Configure a Duration for closing windows periodically. Can be combined with the setWindowSize(int) or setWindowSizeFunction(Function).
      Parameters:
      windowTimespan - the Duration to use for windows to close periodically.
      See Also:
      Flux.window(Duration), Flux.windowTimeout(int, Duration)
    • setWindowConfigurer

      public void setWindowConfigurer​(java.util.function.Function<reactor.core.publisher.Flux<org.springframework.messaging.Message<?>>,​reactor.core.publisher.Flux<reactor.core.publisher.Flux<org.springframework.messaging.Message<?>>>> windowConfigurer)
      Configure a Function to apply a transformation into the grouping Flux for any arbitrary Flux.window(int) options not covered by the simple options. Has a precedence over any other window configuration options.
      Parameters:
      windowConfigurer - the Function to apply any custom window transformation.
    • getComponentType

      public java.lang.String getComponentType()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this method to provide component type information.
      Specified by:
      getComponentType in interface NamedComponent
      Overrides:
      getComponentType in class MessageHandlerSupport
    • getIntegrationPatternType

      public IntegrationPatternType getIntegrationPatternType()
      Description copied from interface: IntegrationPattern
      Return a pattern type this component implements.
      Specified by:
      getIntegrationPatternType in interface IntegrationPattern
      Overrides:
      getIntegrationPatternType in class MessageHandlerSupport
      Returns:
      the IntegrationPatternType this component implements.
    • start

      public void start()
      Specified by:
      start in interface org.springframework.context.Lifecycle
      Specified by:
      start in interface ManageableLifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface org.springframework.context.Lifecycle
      Specified by:
      stop in interface ManageableLifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle
      Specified by:
      isRunning in interface ManageableLifecycle
    • handleMessageInternal

      protected void handleMessageInternal​(org.springframework.messaging.Message<?> message)
      Specified by:
      handleMessageInternal in class AbstractMessageHandler
    • shouldCopyRequestHeaders

      protected boolean shouldCopyRequestHeaders()
      Description copied from class: AbstractMessageProducingHandler
      Subclasses may override this. True by default.
      Overrides:
      shouldCopyRequestHeaders in class AbstractMessageProducingHandler
      Returns:
      true if the request headers should be copied.