generated from root/miduo_server
132 lines
5.2 KiB
Java
132 lines
5.2 KiB
Java
package com.ljsd.util;
|
|
|
|
import com.ljsd.controller.GetUserController;
|
|
import com.mongodb.*;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Properties;
|
|
|
|
/*
|
|
* mongodb数据库链接池
|
|
*/
|
|
public class MyMongoDBPool {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MyMongoDBPool.class);
|
|
private MongoClient mongoClient = null;
|
|
private String dbName;
|
|
private DB db;
|
|
|
|
public MyMongoDBPool() {
|
|
MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
|
|
Properties properties = BaseGlobal.getInstance().properties;
|
|
buide.connectionsPerHost(Integer.parseInt(properties.getProperty("mongodb_maximumNumberOfConnections")));// 与目标数据库可以建立的最大链接数
|
|
buide.connectTimeout(Integer.parseInt(properties.getProperty("mongodb_connectTimeout")));// 与数据库建立链接的超时时间
|
|
buide.maxWaitTime(Integer.parseInt(properties.getProperty("mongodb_maxWaitTime")));// 一个线程成功获取到一个可用数据库之前的最大等待时间
|
|
buide.threadsAllowedToBlockForConnectionMultiplier(Integer.parseInt(properties.getProperty("mongodb_threadsAllowedToBlockForConnectionMultiplier")));
|
|
buide.maxConnectionIdleTime(Integer.parseInt(properties.getProperty("mongodb_maxConnectionIdleTime")));
|
|
buide.maxConnectionLifeTime(Integer.parseInt(properties.getProperty("mongodb_maxConnectionLifeTime")));
|
|
buide.socketTimeout(Integer.parseInt(properties.getProperty("mongodb_socketTimeout")));
|
|
buide.socketKeepAlive(Boolean.parseBoolean(properties.getProperty("mongodb_socketKeepAlive")));
|
|
MongoClientURI mongodb_host = new MongoClientURI(properties.getProperty("mongodb_url"),buide);
|
|
mongoClient = new MongoClient(mongodb_host);
|
|
dbName = properties.getProperty("mongodb_name");
|
|
db = mongoClient.getDB(dbName);
|
|
LOGGER.info("the dbName={},mongoUrl={}",dbName,properties.getProperty("mongodb_url"));
|
|
}
|
|
|
|
public DBCollection getCollection(String collectionName) {
|
|
return db.getCollection(collectionName);
|
|
}
|
|
|
|
|
|
public List<DBObject> findAll(String collectionName) throws Exception {
|
|
DBCollection coll = db.getCollection(collectionName);
|
|
DBCursor cursor = coll.find();
|
|
List<DBObject> docs = new ArrayList<>();
|
|
|
|
while (cursor.hasNext()) {
|
|
DBObject doc = cursor.next();
|
|
docs.add(doc);
|
|
}
|
|
return docs;
|
|
}
|
|
|
|
public DBObject findOne(String collectionName, String id) throws Exception {
|
|
return findOne2(collectionName, id);
|
|
}
|
|
|
|
public List<DBObject> find(String collectionName, DBObject object) throws Exception {
|
|
DBCollection coll = db.getCollection(collectionName);
|
|
DBCursor cursor = coll.find(object);
|
|
List<DBObject> docs = new ArrayList<>();
|
|
while (cursor.hasNext()) {
|
|
DBObject doc = cursor.next();
|
|
docs.add(doc);
|
|
}
|
|
LOGGER.info("collectionName{},个数,{}",collectionName,docs.size());
|
|
return docs;
|
|
}
|
|
|
|
public String findList(String collectionName) throws Exception {
|
|
DBCollection coll = db.getCollection(collectionName);
|
|
DBCursor cursor = coll.find();
|
|
List<DBObject> docs = new ArrayList<>();
|
|
|
|
while (cursor.hasNext()) {
|
|
DBObject doc = cursor.next();
|
|
docs.add(doc);
|
|
}
|
|
return docs.toString();
|
|
}
|
|
|
|
|
|
public DBObject findOne(String collectionName, Integer id) throws Exception {
|
|
return findOne2(collectionName, id);
|
|
}
|
|
|
|
private DBObject findOne2(String collectionName, Object id) throws Exception {
|
|
LOGGER.info("the collection={},id={}",collectionName,id);
|
|
DBCollection coll = db.getCollection(collectionName);
|
|
if(coll == null){
|
|
LOGGER.info("the collection={},id={} is null",collectionName,id);
|
|
return null;
|
|
}
|
|
return coll.findOne(id.toString());
|
|
}
|
|
|
|
public void updateValue(String collectionName, BasicDBObject searchQuery, BasicDBObject dbObject) {
|
|
DBCollection coll = db.getCollection(collectionName);
|
|
coll.update(searchQuery, dbObject);
|
|
}
|
|
|
|
public void save(String collectionName, DBObject doc) {
|
|
DBCollection coll = db.getCollection(collectionName);
|
|
coll.save(doc);
|
|
}
|
|
|
|
|
|
public synchronized int inc(String collectionName, String id, String key) {
|
|
DBCollection collection = db.getCollection(collectionName);
|
|
BasicDBObject searchQuery = new BasicDBObject();
|
|
searchQuery.put("_id", id);
|
|
// 使用collection的find方法查找document
|
|
DBCursor cursor = collection.find(searchQuery);
|
|
// 循环输出结果
|
|
if (cursor.hasNext()) {
|
|
BasicDBObject update = new BasicDBObject().append("$inc", new BasicDBObject().append(key, 1));
|
|
DBObject query = cursor.next();
|
|
DBObject result = collection.findAndModify(query, update);
|
|
return Integer.parseInt(result.get(key).toString()) + 1;
|
|
} else {
|
|
searchQuery.put(key, 1);
|
|
collection.insert(searchQuery);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
public int inc(String key) {
|
|
return inc("inc_c", "inc_k", key);
|
|
}
|
|
} |