Search code examples
springkotlinapache-kafkaspring-kafkaspring-el

Is it possible to read a numeric value from application.yml for @RetryableTopic @Backoff in Kotlin?


I want to read configuration values from my properties file and assign them as parameters of @RetryableTopic and @Backoff. In my application.yml I have:

kafka:
  auto:
    start: true
  groupId: mystuff
  stuff:
    topic: mytopic
    concurrency: 8
    auto-create: true
    delay: 2000

My code:

@RetryableTopic(
    attempts = "4",
    backoff = Backoff(delay = "#{'\${kafka.stuff.delay}'}", multiplier = 2.0),
    autoCreateTopics = "#{'\${kafka.stuff.auto-create}'}",
    topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE,
    dltStrategy = DltStrategy.FAIL_ON_ERROR
)
fun myFunction(

This works for string properties, but not for delay = "#{'${my.topic.delay}'}" which expects a long value:

enter image description here

My question is if it is possible to read the delay long value as I do for the string ones and if so how? Thank you for your attention


Solution

  • No, there isn't because the compiler will always see it as a String regardless of what comes from that evaluation. In your case, however, Spring has provided a means for you to do this.

    The Backoff annotation provides a delay parameter that you're using, which takes a long. However, it has another parameter named delayExpression. The value of delayExpression is a String that can be evaluated into a long, e.g. your SpEL expression. All you need to do for your case is to change the parameter you're using from delay to delayExpression, the latter overrides the former.