Skip to content

alwaysStartAfterRefresh seems not work #2316

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

Closed
youngkangrui opened this issue Jun 23, 2022 · 2 comments
Closed

alwaysStartAfterRefresh seems not work #2316

youngkangrui opened this issue Jun 23, 2022 · 2 comments

Comments

@youngkangrui
Copy link

contextRereshed in KafkaListenerEndPointRegistry is set to true only if received ContextRefreshEvent, but in Spring SmartLifeCycle is triggered before ContextFreshEvent is sended out , so when smartLIfeCycle wants to start the containers, contextRefreshed will always be false, so my question is adding alwaysStartAfterRefresh this way ( #2301 ) will not start the containers even if context refreshed.

using 2.8.7 ,following code's output is still false, while true is expected

public class TestListener {
    @KafkaListener(id="id1",topics = {"tp"},groupId = "consumerGroupId",autoStartup = "false")
    public void Listen(String info){
        System.out.println(info);
    }
}

ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
KafkaListenerEndpointRegistry registry = (KafkaListenerEndpointRegistry)context.getBean("org.springframework.kafka.config.internalKafkaListenerEndpointRegistry");
MessageListenerContainer id1 = registry.getListenerContainer("id1");
System.out.println(id1.isRunning());
@garyrussell
Copy link
Contributor

garyrussell commented Jun 23, 2022

I already closed this as invalid. #2315

alwaysStartAfterRefresh means always start new registrations after the context has started.

When you register a container before the context is refreshed; the autoStartup property is honored. Refreshing the context does not start any containers that were registered before the refresh.

You have to manually start() such containers.

By default, the autoStartup property is ignored when registering new containers after the refresh and the container is started. alwaysStartAfterRefresh=false means always honor the autoStartup property.

@garyrussell
Copy link
Contributor

garyrussell commented Jun 23, 2022

See the javadocs

/**
* By default, containers registered for endpoints after the context is refreshed
* are immediately started, regardless of their autoStartup property, to comply with
* the {@link SmartLifecycle} contract, where autoStartup is only considered during
* context initialization. Set to false to apply the autoStartup property, even for
* late endpoint binding. If this is called after the context is refreshed, it will
* apply to any endpoints registered after that call.
* @param alwaysStartAfterRefresh false to apply the property.
* @since 2.8.7
*/
public void setAlwaysStartAfterRefresh(boolean alwaysStartAfterRefresh) {
    this.alwaysStartAfterRefresh = alwaysStartAfterRefresh;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants