Getting exception while build in junit test execution - spring-boot

Task :test
Exception in thread "Thread-24" java.lang.IllegalStateException: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'traceApplicationContextRefreshedListener': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:174)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onClose(ReactiveWebServerApplicationContext.java:93)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1023)
at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:937)
Caused by: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'traceApplicationContextRefreshedListener': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:208)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1012)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:338)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333)
at org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.contextRefreshed(ReactorSleuth.java:145)
at org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.lambda$null$2(ReactorSleuth.java:124)
at reactor.core.publisher.MonoLift.subscribe(MonoLift.java:42)
at reactor.core.publisher.Mono.block(Mono.java:1174)
at reactor.ipc.netty.tcp.BlockingNettyContext.shutdown(BlockingNettyContext.java:152)
at org.springframework.boot.web.embedded.netty.NettyWebServer.stop(NettyWebServer.java:123)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:170)
... 3 more

There could be a couple of reasons for that possibly which I can recollect looking at this issue:
Setting the right path to the JAVA_HOME environment variable, updating your PATH variable and restarting your eclipse / STS IDE whichever you're using.
The other could be multiple Tomcat instances are open at the same time when you're trying to run your JUnit test cases. You could close all of those instances and open just one to get this sorted.
Kindly cross-check these and let me know it helps!

Related

Unable to create retry-able datasource for spring boot jdbc

I would like to add the retry feature for database connection certain number of times until the app acquires it. For the I have used the spring-retry on the DataSource but it is not working. It is throwing the following error
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcTemplate' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource
I have seen the debug logs but those are not helpful. Here is the sample source code . kindly help
Note: the dependencies mentioned in build.gradle is required for my app. I have only extracted the retry part.
Your use-case is delaying the start of Spring Boot until your database is up. Spring actually ships with a component that does that. The DatabaseStartupValidator is that component and has existed since about Spring 1.x.
You can add it as a bean and it will wait for further bootstrapping until the database is up.
#Bean
public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
var dsv = new DatabaseStartupValidator();
dsv.setDataSource(dataSource);
return dsv;
}
For a more detailed explanation see this blog post of mine.

Spring Boot application cannot join Coherence cluster on server: Join request was aborted

I have developed a small REST API application with Spring Boot after another sample application with classic Spring XML configuration. It imports context from a library with a Coherence cache set up.
When I developed it locally, running the Spring Boot app with Intellij Idea runner, there were no issues, the application worked fine and the endpoint worked connecting to that cache.
When I deployed the application to the Unix server, I am getting the following error during the application startup:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coreSearchSync' defined in class path resource [core-search-core-context.xml]: Cannot resolve reference to bean 'corePersistenceManager' while setting bean property 'corePersistenceManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'corePersistenceManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' coreSearchResultCache' defined in class path resource [core-search-coherence-context.xml]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.tangosol.net.NamedCache]: Factory method 'getCache' threw exception; nested exception is (Wrapped: Failed to start Service "Management" (ServiceState=SERVICE_STOPPED)) java.lang.RuntimeException: Join request was aborted
The application on the server is using the application.properties but they are the same as in the application.yaml locally.
I am not sure where to continue the investigation...
The problem was solved by passing JVM arguments to the script to start the application:
-Dtangosol.coherence.clusterport=<port> -Dtangosol.coherence.site=<site> -Dtangosol.coherence.clusteraddress=<cluster_ip_ddress> -Dtangosol.coherence.cluster=<cluster_name>
It appears that these properties are not applied from the application properties because starting the coherence cluster works on the system level and/or starts before the properties are initialized. Or those properties are not visible to the coherence.

How to avoid spring application failure due to BeanCreationException caused by third party package

I am using a third-party package in my spring boot app. This one to be specific. This package has a method that creates a bean when the application starts. The bean is only created successfully if this package is able to talk to nats service. In most cases, this all works fine. The remote server is available, the bean is created, my spring boot apps boots up correctly.
But there are some edge cases when remote nats servers might not be up. This causes my spring boot app to fail to start as the above function throws BeanCreationException
How can I avoid this i.e avoid my spring app boot failure due to this exception when the nats server is not up?
Note: It is fine for my spring app/business logic to be up and running if this bean (connections to nats service) is not available.
Specific Exception :
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.\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)\n\tat org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)\n\tat org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)\n\tat
The bean natsConnection is created by auto-configuration and annotated with #ConditionalOnMissingBean. Such beans are instantiated after your own beans.
You can create your own bean of this type, io.nats.client.Connection, and register it as natsConnection. This bean should have no own logic. When called for the first time, it should create a worker instance via NatsAutoConfiguration.natsConnection(...) and then delegate all calls to it.
Because of #ConditionalOnMissingBean the default bean from the NATS library will not be instantiated and only your bean will be created.

Errors while running tests

I am trying to run my tests using mvn clean install, but these errors stop the process. I tried debugging using JUNIT but nothing shown on the console.
these are the logs :
com.datamint.service.impl.EmailServiceImplDevTest:Error creating bean with name'entityManagerFactory'defined in class path resource
[orm.xml]:BeanPostProcessor before instantiation of bean failed;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.security.methodSecurityMetadataSourceAdvisor':Cannot resolve reference to bean'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0':Cannot create inner bean'(inner bean)'of type[org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource]while setting constructor argument with key[0];nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'(inner bean)':Cannot create inner bean'(inner bean)'of type[org.springframework.security.access.expression.method.ExpressionBasedAnnotationAttributeFactory]while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'(inner bean)':Cannot resolve reference to bean'expressionHandler'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'expressionHandler'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'permissionEvaluator'while setting bean property'permissionEvaluator';nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'permissionEvaluator'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'aclService'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'aclService'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'lookupStrategy'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'lookupStrategy'defined in class path resource
[spring-security.xml]:Cannot resolve reference to bean'aclCache'while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'aclCache'defined in class path resource
[spring-security.xml]:Cannot create inner bean'org.springframework.cache.ehcache.EhCacheFactoryBean#a512c9c'of type[org.springframework.cache.ehcache.EhCacheFactoryBean]while setting constructor argument;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.cache.ehcache.EhCacheFactoryBean#a512c9c'defined in class path resource
[spring-security.xml]:Cannot create inner bean'org.springframework.cache.ehcache.EhCacheManagerFactoryBean#68394e81'of type[org.springframework.cache.ehcache.EhCacheManagerFactoryBean]while setting bean property'cacheManager';nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'org.springframework.cache.ehcache.EhCacheManagerFactoryBean#68394e81'defined in class path resource
[spring -security.xml]:Invocation of init method failed;nested exception is net.sf.ehcache.CacheException:Another unnamed CacheManager already exists in the same VM.Please provide unique names for each CacheManager in the config or do one of following:
What can be the problem? thanks
The cause of the problem is that with the default settings, you cannot have multiple processes sharing the same EhCache. Taken from here
CacheManager supports two creation modes: singleton and instance.
Versions of Ehcache before version 2.5 allowed any number of
CacheManagers with the same name (same configuration resource) to
exist in a JVM. Therefore, each time new CacheManager(...) was called,
a new CacheManager was created without regard to existing
CacheManagers. Calling CacheManager.create(...) returned the existing
singleton CacheManager with the configured name (if it existed) or
created the singleton based on the passed-in configuration.
Ehcache 2.5 and higher does not allow multiple CacheManagers with the
same name to exist in the same JVM. CacheManager() constructors
creating non-Singleton CacheManagers can violate this rule, causing a
NullPointerException. If your code may create multiple CacheManagers
of the same name in the same JVM, avoid this error by using the static
CacheManager.create() methods, which always return the named (or
default unnamed) CacheManager if it already exists in that JVM. If the
named (or default unnamed) CacheManager does not exist, the
CacheManager.create() methods create it.
One solution that will fix your problem (but will slow down the execution of the tests), is to add #DirtiesContext to each of your tests that loads the Spring configuration. That will force Spring to recreate all the beans upon running each test class and you'll be able to sidestep the problem.
A solution I prefer in these cases is to run the tests (that don't test caching) using a different profile, where caching is not configured. That way you don't need #DirtiesContext and therefore the execution time of the tests if much better

Separate Hibernate library for multiple web applications

I want to have two web applications that use Hibernate, Spring, and JSF 2.0. One would be the website which is available to the public and the other a content management system available on the company intranet.
I don't want to have duplicate code that accesses the database. I want to create a JAR file that contains all the database manipulation using hibernate.
I can't find any examples on the we of how this is done.
Does anyone have any helpful tips? I've managed to create the hibernate JAR file and put a test main in it and that can retrieve the data from the database. I think my main problem is how do I go about configuring all this in the web application?
thanks
After playing around with things a bit. I'm almost there
I'm getting this error when deploying to glassfish now
Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BrandService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'BrandDao' while setting bean property 'brandDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BrandDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'SessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'DataSource' while setting bean property 'dataSource'; nested .... msg.seeServerLog
I think there is not difference between JAR was used by java console application(your test main) and JAR was used by java web application .

Resources