@KafkaListener 在未激活类级别的配置文件的情况下抛出缺少主题的错误。

huangapple go评论56阅读模式
英文:

@KafkaListener throwing error for missing topic even though class level profile not activated

问题

我们有一个配置类,配置文件为"foo"。它有一个如下所示的Kafka监听器:

@Configuration
@Profile("foo")
public class AListener {

    @KafkaListener(topics = "my.kafka.topic", autoStartup = "${kafka.listeners.auto.startup:true}")
    public void processBatch(List<ConsumerRecord<String, byte[]>> records) {
        //做一些操作
    }
}

当应用程序在没有激活"foo"配置文件的情况下启动时,日志中会出现一个Kafka错误,指出"my.kafka.topic"不存在(这是真的,但这不是重点),这表明无论配置文件如何,监听容器都被创建了。这是否是预期行为?除了Spring之外,是否还有其他东西在管理监听器?

英文:

We have a configuration class with a profile of "foo". It has a kafka listener as below:

@Configuration
@Profile(&quot;foo&quot;)
public class AListener {

     @KafkaListener(topics = &quot;my.kafka.topic&quot;, autoStartup = &quot;${kafka.listeners.auto.startup:true}&quot;)
public void processBatch(List&lt;ConsumerRecord&lt;String, byte[]&gt;&gt; records) {
//do stuff

When the app starts up without foo profile active I see a kafka error in the logs complaining that my.kafka.topic does not exist (which is true but thats neither here nor there) which indicates that the listener container is being created regardless of profile. Is this expected? Is something else besides spring managing the listener?

答案1

得分: 1

以下是您要翻译的部分:

"Something else must be configuring a listener; this works as expected for me..."
"Initially, I thought it might be because it is in a @Configuration class (really meant for bean definitions) instead of @Component, but no, it works as expected for me."
"If you can't figure it out, post an MCRE that exhibits the behavior."
"EDIT"
"This has one active listener and one inactive; works fine..."
"[so75550011other]"
"Only the other listener is registered."

英文:

Something else must be configuring a listener; this works as expected for me...

@SpringBootApplication
public class So75550011Application {

	public static void main(String[] args) {
		SpringApplication.run(So75550011Application.class, args);
	}

}

@Configuration
@Profile(&quot;foo&quot;)
class Listener {

	@KafkaListener(id = &quot;so75550011&quot;, topics = &quot;so75550011&quot;)
	void listen(String in) {
		System.out.println(in);
	}

}

Initially, I thought it might be because it is in a @Configuration class (really meant for bean definitions) instead of @Component, but no, it works as expected for me.

If you can't figure it out, post an MCRE that exhibits the behavior.

EDIT

This has one active listener and one inactive; works fine...

@SpringBootApplication
public class So75550011Application {

	public static void main(String[] args) {
		SpringApplication.run(So75550011Application.class, args).close();
	}

	@Bean
	public NewTopic topic() {
		return TopicBuilder.name(&quot;so75550011other&quot;).partitions(1).replicas(1).build();
	}

	@Bean
	ApplicationRunner runner(KafkaListenerEndpointRegistry registry) {
		return args -&gt; {
			System.out.println(registry.getListenerContainerIds());
			Thread.sleep(5000);
		};
	}

}

@Configuration
@Profile(&quot;foo&quot;)
class Listener {

	@KafkaListener(id = &quot;so75550011&quot;, topics = &quot;so75550011&quot;)
	void listen(String in) {
		System.out.println(in);
	}

}

@Component
class OtherListener {

	@KafkaListener(id = &quot;so75550011other&quot;, topics = &quot;so75550011other&quot;)
	void listen(String in) {
		System.out.println(in);
	}

}
[so75550011other]

Only the other listener is registered.

huangapple
  • 本文由 发表于 2023年2月24日 04:30:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定