使用MongoDB連接配接池MongoOptions來進行連接配接 以及相關方法的調用
//獲得驅動位址(這裡的驅動 寫入了配置檔案中)
String serverAddressStr = Configure.getInstance().getProperty("SERVER_ADDRESSES");
log.debug("serverAddressStr:" + serverAddressStr);
如果需要連接配接的MongoDB為多個,則用逗号分隔開,加入集合中以便後續使用
String[] serverAddressArray = serverAddressStr.split(",");
for (String address : serverAddressArray) {
log.debug("address:" + address);
addresslist.add(new ServerAddress(address));
}
//聲明MongoOptions對象
MongoOptions options = new MongoOptions();
//autoConnectRetry方法用于在連接配接失敗後是否重新連接配接,可寫成配置項
String autoConnectRetry = Configure.getInstance().getProperty("AUTO_CONNECT_RETRY");
if (StringUtils.isNotEmpty(autoConnectRetry)) {
options.autoConnectRetry = Boolean.valueOf(autoConnectRetry);
}
//設定連接配接池的大小也可寫成配置項 友善以後調整 使用的是autoConnectRetry方法
String connectionsPerHost = Configure.getInstance().getProperty("CONNECTIONS_PER_HOST");
if (StringUtils.isNotEmpty(connectionsPerHost)) {
options.connectionsPerHost = Integer.valueOf(connectionsPerHost);
}
//線程隊列
String threadsAllowedToBlockForConnectionMultiplier = Configure.getInstance().getProperty("THREAD_ALLOWED");
log.debug("[THREAD_ALLOWED]:" + threadsAllowedToBlockForConnectionMultiplier);
if (StringUtils.isNotEmpty(threadsAllowedToBlockForConnectionMultiplier)) {
options.threadsAllowedToBlockForConnectionMultiplier = Integer.valueOf(threadsAllowedToBlockForConnectionMultiplier);
}
//最大阻塞時間
String connectTimeout = Configure.getInstance().getProperty("CONNECT_TIME_OUT");
log.debug("[CONNECT_TIME_OUT]:" + connectTimeout);
if (StringUtils.isNotEmpty(connectTimeout)) {
options.connectTimeout = Integer.valueOf(connectTimeout);
}
// 被阻塞線程從連接配接池擷取連接配接的最長等待時間(ms)
// options.maxWaitTime = 12000;
String maxWaitTime = Configure.getInstance().getProperty("MAX_WAIT_TIME");
log.debug("[MAX_WAIT_TIME]:" + maxWaitTime);
if (StringUtils.isNotEmpty(maxWaitTime)) {
options.maxWaitTime = Integer.valueOf(maxWaitTime);
}
// 是否答應驅動從次要節點讀取資料,預設為false
String slaveOk = Configure.getInstance().getProperty("SlAVE_OK");
log.debug("[SlAVE_OK]:" + slaveOk);
if (StringUtils.isNotEmpty(slaveOk)) {
options.slaveOk = Boolean.valueOf(slaveOk);
}
從某個中按照字段查找相應資料 并放入集合中
public DBObject findOne(String collectionName,String keystr,String value){
DB db = this.getDB();
DBCollection collection = db.getCollection(collectionName);
DBObject dbObject;
try {
dbObject = collection.findOne(new BasicDBObject(keystr, value));
log.debug("dbObject1:" + dbObject);
if (dbObject == null) {
db = this.switchCluster().getDB();
collection = db.getCollection(collectionName);
dbObject = collection.findOne(new BasicDBObject(keystr, value));
log.debug("dbObject1-2:" + dbObject);
}
} catch (MongoException e) {
db = this.switchCluster().getDB();
collection = db.getCollection(collectionName);
dbObject = collection.findOne(new BasicDBObject(keystr, value));
log.debug("dbObject2:" + dbObject);
}
return dbObject;
}
前台通過DBCollection 根據名稱擷取相應的value 然後加入list中~
public List<DBObject> getValue(List<String> columnNames) {
db = MongoDB.getInstance().getDB();
DBCollection collection = db.getCollection("labels");
List<DBObject> list = new ArrayList<DBObject>();
BasicDBList dbList = new BasicDBList();
dbList.addAll(columnNames);
DBObject inObj = new BasicDBObject("$in", dbList);
DBCursor cursor = collection.find(new BasicDBObject("column_name", inObj));
DBObject dbObj = null;
while (cursor.hasNext()) {
dbObj = cursor.next();
list.add(dbObj);
}
return list;