1

I am learning java and Spring boot 2

So my problem is:

2018-07-09 11:40:24.816 INFO 20196 --- [localhost:27017] org.mongodb.driver.cluster: Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.6.4.jar:na] at com.mongodb.connection.InternalStreamConnection.open (InternalStreamConnection.java:126) ~[mongo-java-driver-3.6.4.jar:na] at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run (DefaultServerMonitor.java:114) ~[mongo-java-driver-3.6.4.jar:na] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171] Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171] at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171] at com.mongodb.connection.SocketStreamHelper.initialize (SocketStreamHelper.java:59) ~[mongo-java-driver-3.6.4.jar:na] at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongo-java-driver-3.6.4.jar:na] ... 3 common frames omitted 2018-07-09 11:40:25.164 INFO 20196 --- [ main] org.mongodb.driver.cluster: Cluster description not yet available. Waiting for 30000 ms before timing out 

So when Spring Boot 2 is Maven build app it stuck because of absent MongoDB server and trying to connect 3 time with timeout 30000ms, then stuck...

So I want to init this connection, after finishing of Maven build, from runtime with external config parameter, and then with @shedules monitoring check if MongoDB alive, and reconnect if it lost.

What I found for SQL is excluding autoconfiguration from topic but I dont know which file is configure MongoDB from this link. And may be I must configure it like a @PostConstruct @bean conf files? (please suggest me example if it present and possibility to do this opportunity)

Thank you in advance!

for now I take an example of db routers:

@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.class ,org.springframework.boot.autoconfigure.data.mongo .MongoDataAutoConfiguration.class}) public class Application { 

exclude 2 files from autoconfigure, wrote some class for setup my properties, but I still do not know how to delay an effort of connection!

4
  • Rather than over-engineer a solution why not just add a profile that enables/disables the mongo autoconfiguration. Have it active when mongo is present? Commented Jul 21, 2018 at 21:45
  • Formatted the Java stack section and a couple typos. Commented Jul 22, 2018 at 1:43
  • may be you are right and i try to research in your way. Commented Jul 22, 2018 at 10:09
  • I tried this kind of way, but there is another problem mongorepository want to create bean independently of app had connection or not. And then crush all app cause of nested exception of connection exception to mongo db Commented Jul 23, 2018 at 11:40

2 Answers 2

2

Skip the junit test while building the project.

Use -Dmaven.test.skip=true to skip the tests

or

in eclipse->Project->Run As->run configuration->Maven build-> check Skip Tests

Sign up to request clarification or add additional context in comments.

3 Comments

I know why my build is stuck , I wrote this below exception. My purpose to ignore it anr build my app and after in runtime check with utility class if my mongo db is alive.
i think you are running Junit which tries to connect Mongo DB while creating build. Skip the tests while build. Updated answer
This fixed the issue for me. Should be assigned as the correct answer. Thank you!
0

since you are using spring boot it should take care of the required versions or did you added the entries for the driver in the pom yourself.

  1. try maven clean ,and force update of snapshots.
  2. mongo-java-driver-3.6.4.jar either it is not compatible or not picked by the ide you are using. check the table mongo-db-compatibilty

1 Comment

I know why my build is stuck , I wrote this below exception. My purpose to ignore it anr build my app and after in runtime check with utility class if my mongo db is alive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.