Class DefaultTopicResolver

java.lang.Object
org.springframework.pulsar.core.DefaultTopicResolver
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, TopicResolver

public class DefaultTopicResolver extends Object implements TopicResolver, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanClassLoaderAware
Topic resolver that accepts custom type to topic mappings and uses the mappings during topic resolution.

Message type to topic mappings can be configured with addCustomTopicMapping(Class, String).

  • Constructor Details

    • DefaultTopicResolver

      public DefaultTopicResolver(ExpressionResolver expressionResolver)
      Constructs a new DefaultTopicResolver with the given expression resolver.
      Parameters:
      expressionResolver - the expression resolver to use for resolving topic
    • DefaultTopicResolver

      public DefaultTopicResolver()
      Constructs a new DefaultTopicResolver.
  • Method Details

    • usePulsarMessageAnnotations

      public void usePulsarMessageAnnotations(boolean usePulsarMessageAnnotations)
      Sets whether to inspect message classes for the @PulsarMessage annotation during topic resolution.
      Parameters:
      usePulsarMessageAnnotations - whether to inspect messages for the annotation
    • addCustomTopicMapping

      @Nullable public String addCustomTopicMapping(Class<?> messageType, String topic)
      Adds a custom mapping from message type to topic.
      Parameters:
      messageType - the message type
      topic - the topic to use for messages of type messageType
      Returns:
      the previously mapped topic or null if there was no mapping for messageType.
    • removeCustomMapping

      @Nullable public String removeCustomMapping(Class<?> messageType)
      Removes the custom mapping from message type to topic.
      Parameters:
      messageType - the message type
      Returns:
      the previously mapped topic or null if there was no mapping for messageType.
    • getCustomTopicMappings

      @Deprecated(since="1.2.5", forRemoval=true) public Map<Class<?>,String> getCustomTopicMappings()
      Deprecated, for removal: This API element is subject to removal in a future version.
      deprecated in favor of getCustomTopicMapping(Class)
      Gets the currently registered custom mappings from message type class name to topic.
      Returns:
      unmodifiable map of custom mappings
    • getCustomTopicMapping

      public Optional<String> getCustomTopicMapping(Class<?> messageType)
      Gets the currently registered custom mapping for the specified message type.
      Parameters:
      messageType - the message type
      Returns:
      optional custom topic registered for the message type
    • resolveTopic

      public Resolved<String> resolveTopic(@Nullable String userSpecifiedTopic, Supplier<String> defaultTopicSupplier)
      Description copied from interface: TopicResolver
      Resolve the topic name to use.
      Specified by:
      resolveTopic in interface TopicResolver
      Parameters:
      userSpecifiedTopic - the topic specified by the user
      defaultTopicSupplier - supplies the default topic to use (use a supplier that returns null to signal no default)
      Returns:
      the topic to use or empty if no topic could be resolved
    • resolveTopic

      public <T> Resolved<String> resolveTopic(@Nullable String userSpecifiedTopic, @Nullable T message, Supplier<String> defaultTopicSupplier)
      Description copied from interface: TopicResolver
      Resolve the topic name to use for the given message.
      Specified by:
      resolveTopic in interface TopicResolver
      Type Parameters:
      T - the message type
      Parameters:
      userSpecifiedTopic - the topic specified by the user
      message - the message instance being produced or consumed
      defaultTopicSupplier - supplies the default topic to use (use a supplier that returns null to signal no default)
      Returns:
      the topic to use or empty if no topic could be resolved
    • resolveTopic

      public Resolved<String> resolveTopic(@Nullable String userSpecifiedTopic, @Nullable Class<?> messageType, Supplier<String> defaultTopicSupplier)
      Description copied from interface: TopicResolver
      Resolve the topic name to use for the given message type.
      Specified by:
      resolveTopic in interface TopicResolver
      Parameters:
      userSpecifiedTopic - the topic specified by the user
      messageType - the message type being produced or consumed
      defaultTopicSupplier - supplies the default topic to use (use a supplier that returns null to signal no default)
      Returns:
      the topic to use or empty if no topic could be resolved
    • doResolveTopic

      protected Resolved<String> doResolveTopic(@Nullable String userSpecifiedTopic, @Nullable Class<?> messageType, Supplier<String> defaultTopicSupplier)
    • setBeanFactory

      public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
      Throws:
      org.springframework.beans.BeansException
    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Specified by:
      setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware