spring-nats
spring-nats copied to clipboard
Prevent spring boot app crash if not able to connect to NATS server
I have a use case where I don't want my app to crash on startup when binder is not able to connect to nats server.. Since the binder is driven by autoconfiguration, it tries to connect to server and crashes the app if it's not able to connect.. Any ways I can make it not crash the app?
I13:48:32.743 estartedMain o.a.catalina.core.StandardService| Stopping service [Tomcat]
I13:48:32.759 estartedMain itionEvaluationReportLoggingListener|
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
E13:48:32.782 estartedMain o.s.boot.SpringApplication| Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'natsConnection' defined in class path resource [io/nats/spring/boot/autoconfigure/NatsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.nats.client.Connection]: Factory method 'natsConnection' threw exception; nested exception is java.io.IOException: Unable to connect to NATS server.
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1179)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.nats.client.Connection]: Factory method 'natsConnection' threw exception; nested exception is java.io.IOException: Unable to connect to NATS server.
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 25 common frames omitted
Caused by: java.io.IOException: Unable to connect to NATS server.
at io.nats.client.impl.NatsConnection.connect(NatsConnection.java:193)
at io.nats.client.impl.NatsImpl.createConnection(NatsImpl.java:29)
at io.nats.client.Nats.createConnection(Nats.java:218)
at io.nats.client.Nats.connect(Nats.java:147)
at io.nats.spring.boot.autoconfigure.NatsAutoConfiguration.natsConnection(NatsAutoConfiguration.java:92)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 26 common frames omitted
Is this behaviour still happening in the latest version?
Is this behaviour still happening in the latest version?
Yes this is happening in the latest version.
@nitinsh99 This should be the expected behavior the reason being that autoconfiguration make sure nats server is up and spring cloud steam binder can connect and work as expected.
There is a PR #43 ongoing which could fix the issue for the future and will upgrade the lib to spring boot 3