Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessageListenerContainer is started even if autoStartup is false #2297

Closed
tbarabanov opened this issue Jun 6, 2022 · 5 comments · Fixed by #2301
Closed

MessageListenerContainer is started even if autoStartup is false #2297

tbarabanov opened this issue Jun 6, 2022 · 5 comments · Fixed by #2301

Comments

@tbarabanov
Copy link

tbarabanov commented Jun 6, 2022

In what version(s) of Spring for Apache Kafka are you seeing this issue?
2.8,2

Describe the bug
I have a class like this:
@Autowired
private KafkaListenerEndpointRegistry registry;

@KafkaListener(
id = "#{__listener.id}",
groupId = "#{__listener.groupId}",
topics = "#{__listener.topics}",
properties = {"#{__listener.properties}"},
autoStartup = "false") // autostart is disabled, use listener.start()

Instances of the class are created this way:
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public KafkaMessageListener<?, ?> kafkaListener(String groupId,
String[] topics,
String[] properties) {
return new KafkaMessageListener<>(groupId, topics, properties);
when I create an instance of the KafkaMessageListener it starts up despite the fact that autoStartup is "false". I also tried to set

ConcurrentKafkaListenerContainerFactory.setAutoStartup(false) but without success.

I found that KafkaListenerEndpointRegistry starts my listener in this method:
private void startIfNecessary(MessageListenerContainer listenerContainer) {
if (this.contextRefreshed || listenerContainer.isAutoStartup()) {
listenerContainer.start();
}
}

because KafkaListenerEndpointRegistrar.startImmediately is true when I call appCtx.getBean(...)

I expected that when I create an instance of KafkaMessageListener it must not be started.

@tbarabanov tbarabanov changed the title MessageListenerContainer is started even if autoStartUp if false MessageListenerContainer is started even if autoStartup is false Jun 7, 2022
@garyrussell
Copy link
Contributor

Please learn how to use GitHub markdown to format code properly.

autoStartup only applies during context initialization; it is not relevant when the context has been refreshed (fully initialized), including when creating new prototype bean instances after the initialization is complete.

We could consider adding an option to the registry.

@garyrussell garyrussell added type: enhancement ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. and removed type: bug status: waiting-for-triage labels Jun 7, 2022
@garyrussell garyrussell added this to the Backlog milestone Jun 7, 2022
@tbarabanov
Copy link
Author

Thx for the advice ). Is there any need in an option? I mean what if just get rid of the contextRefreshed check in KafkaListenerEndpointRegistry.startIfNecessary?

@garyrussell
Copy link
Contributor

It has to be an option, the default behavior is correct.

@garyrussell
Copy link
Contributor

I presume you are using context.getBean() to get new instance(s) of your listener; why not defer that until you actually want the container to start?

@tbarabanov
Copy link
Author

Yep, can do it this way. But it puts restrictions on how the code is organised, create/start/stop/ is just a bit more natural.

@garyrussell garyrussell added backport 2.8.x (obsolete) backport 2.9.x (obsolete) and removed ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. labels Jun 7, 2022
@garyrussell garyrussell modified the milestones: Backlog, 3.0.0.M5 Jun 7, 2022
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Jun 7, 2022
Resolves spring-projects#2297

Add an option to apply `autoStartup` semantics to late registrations.

**cherry-pick to 2.9.x, 2.8.x**
artembilan pushed a commit that referenced this issue Jun 7, 2022
Resolves #2297

Add an option to apply `autoStartup` semantics to late registrations.

**cherry-pick to 2.9.x, 2.8.x**
artembilan pushed a commit that referenced this issue Jun 7, 2022
Resolves #2297

Add an option to apply `autoStartup` semantics to late registrations.

**cherry-pick to 2.9.x, 2.8.x**
artembilan pushed a commit that referenced this issue Jun 7, 2022
Resolves #2297

Add an option to apply `autoStartup` semantics to late registrations.

**cherry-pick to 2.9.x, 2.8.x**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants