spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.56k stars 40.55k forks source link

@ServiceConnection is not working with @RestartScope in 3.2.0-RC1 #37989

Closed eddumelendez closed 11 months ago

eddumelendez commented 11 months ago

Section Using DevTools with Testcontainers at Development Time in docs shows how @RestartScope works with existing ServiceConnection implementations. This behavior works in 3.1.5 and 3.2.0-M3 but not anymore in 3.2.0-RC1. This result in application trying to start with no containers initialized.

This issue is different than https://github.com/spring-projects/spring-boot/issues/35786

Reproducer: this project contains a consumer and producer folder. Any of them can be used to reproduce it.

Also, the code snippet is missing @ServiceConnection.

mhalbritter commented 11 months ago

Stacktrace is:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v3.2.0-RC1)

2023-10-23T10:52:17.531+02:00  INFO 48330 --- [  restartedMain] c.example.consumer.ConsumerApplication   : Starting ConsumerApplication using Java 17.0.8.1 with PID 48330 (/home/moe/Downloads/issue-projects/sb-37989/consumer/target/classes started by moe in /home/moe/Downloads/issue-projects/sb-37989)
2023-10-23T10:52:17.533+02:00  INFO 48330 --- [  restartedMain] c.example.consumer.ConsumerApplication   : No active profile set, falling back to 1 default profile: "default"
2023-10-23T10:52:17.560+02:00  INFO 48330 --- [  restartedMain] o.s.b.devtools.restart.ChangeableUrls    : The Class-Path manifest attribute in /home/moe/.m2/repository/org/springframework/pulsar/spring-pulsar-cache-provider-caffeine/1.0.0-RC1/spring-pulsar-cache-provider-caffeine-1.0.0-RC1.jar referenced one or more files that do not exist: file:/home/moe/.m2/repository/org/springframework/pulsar/spring-pulsar-cache-provider-caffeine/1.0.0-RC1/spring-pulsar-cache-provider-1.0.0-RC1.jar
2023-10-23T10:52:17.560+02:00  INFO 48330 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-23T10:52:17.902+02:00  INFO 48330 --- [  restartedMain] org.testcontainers.images.PullPolicy     : Image pull policy will be performed by: DefaultPullPolicy()
2023-10-23T10:52:17.903+02:00  INFO 48330 --- [  restartedMain] o.t.utility.ImageNameSubstitutor         : Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
2023-10-23T10:52:17.957+02:00  INFO 48330 --- [  restartedMain] o.s.p.core.DefaultPulsarClientFactory    : Using restartable client
2023-10-23T10:52:18.085+02:00  INFO 48330 --- [  restartedMain] o.t.d.DockerClientProviderStrategy       : Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
2023-10-23T10:52:18.207+02:00  INFO 48330 --- [  restartedMain] o.t.d.DockerClientProviderStrategy       : Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
2023-10-23T10:52:18.208+02:00  WARN 48330 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pulsarClient' defined in class path resource [org/springframework/boot/autoconfigure/pulsar/PulsarConfiguration.class]: Mapped port can only be obtained after the container is started
2023-10-23T10:52:18.213+02:00  INFO 48330 --- [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-10-23T10:52:18.221+02:00 ERROR 48330 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pulsarClient' defined in class path resource [org/springframework/boot/autoconfigure/pulsar/PulsarConfiguration.class]: Mapped port can only be obtained after the container is started
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1775) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:947) ~[spring-context-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:616) ~[spring-context-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:748) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:450) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at com.example.consumer.ConsumerApplication.main(ConsumerApplication.java:11) ~[classes/:na]
    at org.springframework.util.function.ThrowingConsumer.accept(ThrowingConsumer.java:60) ~[spring-core-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.util.function.ThrowingConsumer.accept(ThrowingConsumer.java:49) ~[spring-core-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.boot.SpringApplication$Augmented.lambda$run$1(SpringApplication.java:1495) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication.lambda$withHook$4(SpringApplication.java:1418) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[spring-core-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[spring-core-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1436) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1417) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.SpringApplication$Augmented.run(SpringApplication.java:1495) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at com.example.consumer.ConsumerApplicationTests.main(ConsumerApplicationTests.java:16) ~[test-classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.2.0-RC1.jar:3.2.0-RC1]
Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
    at org.testcontainers.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:512) ~[testcontainers-1.19.1.jar:1.19.1]
    at org.testcontainers.containers.ContainerState.getMappedPort(ContainerState.java:161) ~[testcontainers-1.19.1.jar:na]
    at org.testcontainers.containers.PulsarContainer.getPulsarBrokerUrl(PulsarContainer.java:89) ~[pulsar-1.19.1.jar:na]
    at org.springframework.boot.testcontainers.service.connection.pulsar.PulsarContainerConnectionDetailsFactory$PulsarContainerConnectionDetails.getBrokerUrl(PulsarContainerConnectionDetailsFactory.java:52) ~[spring-boot-testcontainers-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.util.function.SingletonSupplier.get(SingletonSupplier.java:106) ~[spring-core-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.boot.context.properties.PropertyMapper$NullPointerExceptionSafeSupplier.get(PropertyMapper.java:361) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.context.properties.PropertyMapper$Source.to(PropertyMapper.java:292) ~[spring-boot-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.autoconfigure.pulsar.PulsarPropertiesMapper.customizeClientBuilder(PulsarPropertiesMapper.java:55) ~[spring-boot-autoconfigure-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.autoconfigure.pulsar.PulsarConfiguration.lambda$pulsarClientFactory$0(PulsarConfiguration.java:85) ~[spring-boot-autoconfigure-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.autoconfigure.pulsar.PulsarConfiguration.lambda$applyClientBuilderCustomizers$2(PulsarConfiguration.java:94) ~[spring-boot-autoconfigure-3.2.0-RC1.jar:3.2.0-RC1]
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
    at org.springframework.boot.autoconfigure.pulsar.PulsarConfiguration.applyClientBuilderCustomizers(PulsarConfiguration.java:94) ~[spring-boot-autoconfigure-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.boot.autoconfigure.pulsar.PulsarConfiguration.lambda$pulsarClientFactory$1(PulsarConfiguration.java:88) ~[spring-boot-autoconfigure-3.2.0-RC1.jar:3.2.0-RC1]
    at org.springframework.pulsar.core.PulsarClientProxy.createInstance(PulsarClientProxy.java:95) ~[spring-pulsar-1.0.0-RC1.jar:1.0.0-RC1]
    at org.springframework.pulsar.core.PulsarClientProxy.createInstance(PulsarClientProxy.java:47) ~[spring-pulsar-1.0.0-RC1.jar:1.0.0-RC1]
    at org.springframework.pulsar.core.RestartableSingletonFactory.ensureInstanceCreated(RestartableSingletonFactory.java:72) ~[spring-pulsar-1.0.0-RC1.jar:1.0.0-RC1]
    at org.springframework.pulsar.core.RestartableSingletonFactory.afterPropertiesSet(RestartableSingletonFactory.java:61) ~[spring-pulsar-1.0.0-RC1.jar:1.0.0-RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1822) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771) ~[spring-beans-6.1.0-RC1.jar:6.1.0-RC1]
    ... 30 common frames omitted
eddumelendez commented 11 months ago

I can confirm it works. Thanks!