Skip to content

Commit b96eaac

Browse files
committed
adding a busy waiting for pinging if the mongoDB is ready before finishing the starting of the SUT in the external controller of reservations-api
1 parent 1acde34 commit b96eaac

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

jdk_11_gradle/em/embedded/rest/reservations-api/src/main/java/em/embedded/reservationsapi/EmbeddedEvoMasterController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public String startSut() {
113113
return "http://localhost:" + getSutPort();
114114
}
115115

116+
/**
117+
* Checks if the mongo database is ready to receive commands using a ping command
118+
* @return
119+
*/
116120
private boolean isMongoClientReady() {
117121
try {
118122
MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME);

jdk_11_gradle/em/external/rest/reservations-api/src/main/java/em/external/reservationsapi/ExternalEvoMasterController.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ public void preStart() {
153153

154154
@Override
155155
public void postStart() {
156+
try {
157+
Thread.sleep(3_000);
158+
} catch (InterruptedException e) {
159+
// do nothing
160+
}
161+
162+
while (!isMongoClientReady()) {
163+
try {
164+
Thread.sleep(1_000);
165+
} catch (InterruptedException e) {
166+
// do nothing
167+
}
168+
}
169+
}
170+
171+
/**
172+
* Checks if the mongo database is ready to receive commands using a ping command
173+
* @return
174+
*/
175+
private boolean isMongoClientReady() {
176+
try {
177+
MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME);
178+
Document pingResult = db.runCommand(new Document("ping", 1));
179+
return pingResult.getDouble("ok") == 1.0;
180+
} catch (Exception ex) {
181+
// Connection error
182+
return false;
183+
}
156184
}
157185

158186
@Override
@@ -270,4 +298,6 @@ public List<DbSpecification> getDbSpecifications() {
270298
public Object getMongoConnection() {
271299
return mongoClient;
272300
}
301+
302+
273303
}

0 commit comments

Comments
 (0)