我目前使用Spring Kafka API
我们目前有要求为Kafka主题动态添加后缀。 在尝试修改并将其设置回Annotation“@KafkaListener”时,如下所示 -
@KafkaListener(topics = modifiedTopics)
它给了我一个错误,说 - 注释属性KafkaListener.topics的值必须是数组初始值设定项
或者如果我尝试下面的代码 - (用花括号)
@KafkaListener(topics = {modifiedTopics})
它给了我错误说 - 注释属性KafkaListener.topics的值必须是一个常量表达式
有没有人遇到与annotataions类似的问题? 如果是,请提供帮助!
I am currently using Spring Kafka API's
We currently have a requirement to dynamically add a suffix to Kafka topics. While trying to modify and set it back to Annotation "@KafkaListener" as follows -
@KafkaListener(topics = modifiedTopics )
It gives me an error saying - The value for annotation attribute KafkaListener.topics must be an array initializer
or if I try the code one below - (with flower braces)
@KafkaListener(topics = {modifiedTopics} )
It gives me error saying - The value for annotation attribute KafkaListener.topics must be a constant expression
Has anyone faced similar issues with annotataions? If yes, kindly help!
最满意答案
解决方案是:将lambda添加到注释中的一种方法如下:在KafkaReceiver类的方法中 -
@Autowired TopicUtil topicUtil; @KafkaListener(topics = "#{topicUtil.suffixTopics()}") //In the TopicUtil - add the follwoing method public String[] suffixTopics() { return Arrays.asList(pTopics.split(",")).stream().map(p -> p + envSuffix).toArray(String[]::new); }Solution is: One of the way to add lambda into annotation is as follows: In the KafkaReceiver class's method -
@Autowired TopicUtil topicUtil; @KafkaListener(topics = "#{topicUtil.suffixTopics()}") //In the TopicUtil - add the follwoing method public String[] suffixTopics() { return Arrays.asList(pTopics.split(",")).stream().map(p -> p + envSuffix).toArray(String[]::new); }更多推荐
发布评论