Class AbstractMappingMessageRouter

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.core.Ordered, ExpressionCapable, Orderable, IntegrationPattern, MessageRouter, NamedComponent, IntegrationManagement, MappingMessageRouterManagement, TrackableComponent, org.springframework.messaging.MessageHandler, reactor.core.CoreSubscriber<org.springframework.messaging.Message<?>>
Direct Known Subclasses:
ErrorMessageExceptionTypeRouter, ExpressionEvaluatingRouter, HeaderValueRouter, MethodInvokingRouter, PayloadTypeRouter

public abstract class AbstractMappingMessageRouter
extends AbstractMessageRouter
implements MappingMessageRouterManagement
Base class for all Message Routers that support mapping from arbitrary String values to Message Channel names.
Since:
2.1
  • Constructor Details

  • Method Details

    • setChannelMappings

      @ManagedAttribute public void setChannelMappings​(java.util.Map<java.lang.String,​java.lang.String> channelMappings)
      Provide mappings from channel keys to channel names. Channel names will be resolved by the DestinationResolver.
      Specified by:
      setChannelMappings in interface MappingMessageRouterManagement
      Parameters:
      channelMappings - The channel mappings.
    • setPrefix

      public void setPrefix​(java.lang.String prefix)
      Specify a prefix to be added to each channel name prior to resolution.
      Parameters:
      prefix - The prefix.
    • setSuffix

      public void setSuffix​(java.lang.String suffix)
      Specify a suffix to be added to each channel name prior to resolution.
      Parameters:
      suffix - The suffix.
    • setResolutionRequired

      public void setResolutionRequired​(boolean resolutionRequired)
      Specify whether this router should ignore any failure to resolve a channel name to an actual MessageChannel instance when delegating to the ChannelResolver strategy.
      Parameters:
      resolutionRequired - true if resolution is required.
    • setChannelKeyFallback

      public void setChannelKeyFallback​(boolean channelKeyFallback)
      When true (default), if a resolved channel key does not exist in the channel map, the key itself is used as the channel name, which we will attempt to resolve to a channel. Set to false to disable this feature. This could be useful to prevent malicious actors from generating a message that could cause the message to be routed to an unexpected channel, such as one upstream of the router, which would cause a stack overflow.
      Parameters:
      channelKeyFallback - false to disable the fall back.
      Since:
      5.2
    • setDynamicChannelLimit

      public void setDynamicChannelLimit​(int dynamicChannelLimit)
      Set a limit for how many dynamic channels are retained (for reporting purposes). When the limit is exceeded, the oldest channel is discarded.

      NOTE: this does not affect routing, just the reporting which dynamically resolved channels have been routed to. Default 100.

      Parameters:
      dynamicChannelLimit - the limit.
      See Also:
      getDynamicChannelNames()
    • getChannelMappings

      @ManagedAttribute public java.util.Map<java.lang.String,​java.lang.String> getChannelMappings()
      Returns an unmodifiable version of the channel mappings. This is intended for use by subclasses only.
      Specified by:
      getChannelMappings in interface MappingMessageRouterManagement
      Returns:
      The channel mappings.
    • setChannelMapping

      @ManagedOperation public void setChannelMapping​(java.lang.String key, java.lang.String channelName)
      Add a channel mapping from the provided key to channel name.
      Specified by:
      setChannelMapping in interface MappingMessageRouterManagement
      Parameters:
      key - The key.
      channelName - The channel name.
    • removeChannelMapping

      @ManagedOperation public void removeChannelMapping​(java.lang.String key)
      Remove a channel mapping for the given key if present.
      Specified by:
      removeChannelMapping in interface MappingMessageRouterManagement
      Parameters:
      key - The key.
    • getDynamicChannelNames

      @ManagedAttribute public java.util.Collection<java.lang.String> getDynamicChannelNames()
      Description copied from interface: MappingMessageRouterManagement
      Provide a collection of channel names to which we have routed messages where the channel was not explicitly mapped.

      Implementations may choose to return only the most recent channel names.

      Specified by:
      getDynamicChannelNames in interface MappingMessageRouterManagement
      Returns:
      a collection of channel names to which we have routed messages where the channel was not explicitly mapped.
    • getChannelKeys

      protected abstract java.util.List<java.lang.Object> getChannelKeys​(org.springframework.messaging.Message<?> message)
      Subclasses must implement this method to return the channel keys. A "key" might be present in this router's "channelMappings", or it could be the channel's name or even the Message Channel instance itself.
      Parameters:
      message - The message.
      Returns:
      The channel keys.
    • determineTargetChannels

      protected java.util.Collection<org.springframework.messaging.MessageChannel> determineTargetChannels​(org.springframework.messaging.Message<?> message)
      Description copied from class: AbstractMessageRouter
      Subclasses must implement this method to return a Collection of zero or more MessageChannels to which the given Message should be routed.
      Specified by:
      determineTargetChannels in class AbstractMessageRouter
      Parameters:
      message - The message.
      Returns:
      The collection of message channels.
    • replaceChannelMappings

      @ManagedOperation public void replaceChannelMappings​(java.util.Properties channelMappings)
      Convenience method allowing conversion of a list of mappings in a control-bus message.

      This is intended to be called via a control-bus; keys and values that are not Strings will be ignored.

      Mappings must be delimited with newlines, for example:

      "@'myRouter.handler'.replaceChannelMappings('foo=qux \n baz=bar')".

      Specified by:
      replaceChannelMappings in interface MappingMessageRouterManagement
      Parameters:
      channelMappings - The channel mappings.
      Since:
      4.0