From 2c72a1e42cd4a2947a5cde2433bf2e2ce01c334b Mon Sep 17 00:00:00 2001 From: Kapil Khandelwal Date: Wed, 13 Apr 2022 05:56:26 +0530 Subject: [PATCH] BAEL-5369: Checking Connection to MongoDB (#12057) --- .../com/baeldung/mongo/ConnectionCheck.java | 46 ++++++++++++++++++ .../mongo/ConnectionCheckLiveTest.java | 47 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java create mode 100644 persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java diff --git a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java new file mode 100644 index 0000000000..240e54ab99 --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java @@ -0,0 +1,46 @@ +package com.baeldung; + +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.ServerAddress; + +public class ConnectionCheck { + + public static void checkingConnection() { + + MongoClientOptions.Builder builder = MongoClientOptions.builder(); + + builder.connectionsPerHost(100); + builder.maxWaitTime(60000); + builder.connectTimeout(1500); + builder.socketTimeout(60000); + builder.socketKeepAlive(true); + + ServerAddress ServerAddress = new ServerAddress("localhost", 27017); + MongoClient mongoClient = new MongoClient(ServerAddress, builder.build()); + + try { + System.out.println("MongoDB Server is Up:- " + mongoClient.getAddress()); + DB db = mongoClient.getDB("baeldung"); + System.out.println(mongoClient.getConnectPoint()); + System.out.println(db.getStats()); + } catch (Exception e) { + System.out.println("MongoDB Server is Down"); + mongoClient.close(); + } + + } + + public static void main(String[] args) { + + // + // Connection check + // + + checkingConnection(); + + } + +} + diff --git a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java new file mode 100644 index 0000000000..69f356997c --- /dev/null +++ b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java @@ -0,0 +1,47 @@ +package com.baeldung.mongo; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.ServerAddress; + +public class ConnectionCheckLiveTest { + + private static MongoClient mongoClient; + private static MongoClientOptions.Builder builder; + private static ServerAddress ServerAddress; + + @BeforeClass + public static void setup() throws IOException { + if (mongoClient == null) { + + builder = MongoClientOptions.builder(); + builder.connectionsPerHost(100); + builder.maxWaitTime(60000); + builder.connectTimeout(1500); + builder.socketTimeout(60000); + builder.socketKeepAlive(true); + + ServerAddress = new ServerAddress("localhost", 27017); + mongoClient = new MongoClient(ServerAddress, builder.build()); + + } + } + + @Test + public void givenMongoClient_whenConnectionCheck_thenCheckingForConnectionPoint() { + + String connectionPoint = mongoClient.getConnectPoint(); + assertNotNull(connectionPoint); + assertFalse(connectionPoint.isEmpty()); + + } +} +