Search code examples
spring-pulsar

Is it possible for the topics names set in the @PulsarListener annotation to come from a Spring app properties value?


I am wondering if it is possible to do something like this

 @Value("${pulsar.topic.name}")
  private String topicName;

  @PulsarListener(subscriptionName = "subscription-name",
      subscriptionType = SubscriptionType.Shared,
      schemaType = SchemaType.STRING,
      topics = topicName)
  )

When I attempt to I get errors about. Attribute value must be constant

Is there a reason this isn't allowed?

My reasoning for doing it is I would like to specify the name of the topic per environment.


Solution

  • Expressions are supported in the majority of the @PulsarListener attributes. In your example you just need to move the expression directly to the topics attribute. For example, you can do something like this:

    @PulsarListener(topics = "${pulsar.topic.name}") 
    

    or

    @PulsarListener(topics = "#{someBean.getTopic())") 
    

    More info can be found on topic resolution can be found here.