浏览代码

优化工具类

johnclot69 4 月之前
父节点
当前提交
49039df739
共有 1 个文件被更改,包括 21 次插入16 次删除
  1. 21 16
      incubator-game/src/main/java/com/incubator/game/data/mongo/MongoUtil.java

+ 21 - 16
incubator-game/src/main/java/com/incubator/game/data/mongo/MongoUtil.java

@@ -1,7 +1,9 @@
 package com.incubator.game.data.mongo;
 
+import com.incubator.common.log4j.Log4jUtil;
 import com.incubator.game.GGame;
 import com.incubator.common.util.PropertiesUtil;
+import com.incubator.game.data.jedis.JedisPoolUtil;
 import com.mongodb.*;
 import com.mongodb.client.FindIterable;
 import com.mongodb.client.MongoCollection;
@@ -12,13 +14,17 @@ import com.mongodb.client.result.DeleteResult;
 import org.bson.Document;
 import org.bson.conversions.Bson;
 import org.bson.types.ObjectId;
+import org.slf4j.Logger;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 public class MongoUtil {
 
+	private static final Logger logger = Log4jUtil.getLogger(MongoUtil.class);
+
 	private MongoClient mongoClient = null;
 
 	private static String HOST;
@@ -30,25 +36,24 @@ public class MongoUtil {
 	private static MongoUtil mongoUtil = null;
 
 	private MongoUtil() {
-		if (mongoClient == null) {
-			PropertiesUtil dbmongo = GGame.DB_SETTINGS;
-			HOST = dbmongo.getProperty("mongodb.host");
-			POST = Integer.valueOf(dbmongo.getProperty("mongodb.port"));
-			DBNAME = dbmongo.getProperty("mongodb.dbName");
+        try {
+			PropertiesUtil dbMongo = GGame.DB_SETTINGS;
+			HOST = dbMongo.getProperty("mongodb.host");
+			POST = Integer.valueOf(Objects.requireNonNull(dbMongo.getProperty("mongodb.port")));
+			DBNAME = dbMongo.getProperty("mongodb.dbName");
+
 			MongoClientOptions.Builder build = new MongoClientOptions.Builder();
-			build.maxWaitTime(Integer.parseInt(dbmongo.getProperty("mongodb.maxWaitTime")));
-			build.connectTimeout(Integer.parseInt(dbmongo.getProperty("mongodb.connectTimeout")));
-			build.socketTimeout(Integer.parseInt(dbmongo.getProperty("mongodb.socketTimeout")));
-			build.connectionsPerHost(Integer.parseInt(dbmongo.getProperty("mongodb.connectionsNumbersPerHost")));
+			build.maxWaitTime(Integer.parseInt(Objects.requireNonNull(dbMongo.getProperty("mongodb.maxWaitTime"))));
+			build.connectTimeout(Integer.parseInt(Objects.requireNonNull(dbMongo.getProperty("mongodb.connectTimeout"))));
+			build.socketTimeout(Integer.parseInt(Objects.requireNonNull(dbMongo.getProperty("mongodb.socketTimeout"))));
+			build.connectionsPerHost(Integer.parseInt(Objects.requireNonNull(dbMongo.getProperty("mongodb.connectionsNumbersPerHost"))));
 			build.writeConcern(WriteConcern.ACKNOWLEDGED);
 			MongoClientOptions myOptions = build.build();
-			try {
-				mongoClient = new MongoClient(new ServerAddress(HOST, POST), myOptions);
-			} catch (MongoException e) {
-				e.printStackTrace();
-			}
-		}
-	}
+			mongoClient = new MongoClient(new ServerAddress(HOST, POST), myOptions);
+        } catch (MongoException e) {
+            logger.error("初始化Mongo客户端失败...", e);
+        }
+    }
 
 	public synchronized static MongoUtil getInstance() {
 		if (mongoUtil == null) {