-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
GH-2870: EmbeddedKafka: register BeanDefinition #2872
Conversation
Fixes spring-projects#2870 The `EmbeddedKafkaContextCustomizer` uses `beanFactory.initializeBean()` which is too early according to the `ApplicationContext` lifecycle since it is not refreshed yet for `ContextCustomizer` * Rework the logic in the `EmbeddedKafkaContextCustomizer` to register a `BeanDefinition` for an `EmbeddedKafkaBroker` to include it into standard `ApplicationContext` lifecycle * Also mark `@EmbeddedKafka` with a `@DisabledInAotMode` to disallow this kind of tests in native images
OK. So, there is a problem with this fix.
The in test configuration this cannot be resolved:
Just because we have not initialized environment yet. |
…EmbeddedKafkaContextCustomizer` since we need to have broker started and system props initialized before the rest of `ApplicationContext`
IMO, there is no rush to fix this, certainly post RC as I expect other lifecycle issues. The code that we have now is making sure that the broker starts (very) early and, from that perspective, it is a good thing.
Having it working doesn't necessarily means it's right. To make it right, the bits that mutate the environment should be separated from the bits that actually start the container. Then, to make sure things are initialized in the right order, a dependency should probably be defined between the embedded broker and whatever is using it first. At the moment, the broker became a bean like any other bean and the container is free to start it whenever it feels like. I can understand why it's frustrating to have to spend more time to make it work again with the proper semantics but having it done initially wouldn't have lead to this issue at all. |
I would rather change the tests to get the broker address from the broker bean itself rather than using that We should not be calling |
There is more into that, Gary. I guess if we would not expose this In the end this customizer is there really to expose a bean for target Spring configuration convenience in the test, but the Kafka server must be started and its options exposed to the environment before other beans got into the action.
So, why is that so bad to have a single convenient component which can be reused in different places? The end result is very beneficial for users of our framework. |
Fixes #2870
The
EmbeddedKafkaContextCustomizer
usesbeanFactory.initializeBean()
which is too early according to theApplicationContext
lifecycle since it is not refreshed yet forContextCustomizer
EmbeddedKafkaContextCustomizer
to register aBeanDefinition
for anEmbeddedKafkaBroker
to include it into standardApplicationContext
lifecycle@EmbeddedKafka
with a@DisabledInAotMode
to disallow this kind of tests in native images