master
jiahuiwen 2018-12-27 10:06:44 +08:00
parent e748a514f8
commit 539bc968d4
9 changed files with 63 additions and 36 deletions

View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0

View File

@ -44,7 +44,7 @@ public class GetServerListController extends HttpServlet {
req.put("channel", channel);
req.put("sub_channel", sub_channel);
req.put("plat", plat);
List<DBObject> serverInfoList = BaseGlobal.mongoDBPool.find(_COLLECTION_NAME, req);
List<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
List<DBObject> serverList = new ArrayList<>();
for (DBObject serverInfo : serverInfoList) {
long time = Long.parseLong(serverInfo.get("open_time").toString());
@ -62,7 +62,7 @@ public class GetServerListController extends HttpServlet {
}
//获取我的服务器列表
Map<String, UserServerInfo> userServerInfoMap = BaseGlobal.redisApp.hgetAll(RedisKey.USER_SERVER_INFO, openId, UserServerInfo.class, -1, false);
Map<String, UserServerInfo> userServerInfoMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.USER_SERVER_INFO, openId, UserServerInfo.class, -1, false);
List<DBObject> myServerList = new ArrayList<>();
for (Map.Entry<String, UserServerInfo> entry : userServerInfoMap.entrySet()) {
DBObject res = new BasicDBObject();

View File

@ -35,20 +35,20 @@ public class GetUserController extends HttpServlet {
dbObject.put("serverId", serverId);
try {
int uid = 0;
List<DBObject> userInfos = BaseGlobal.mongoDBPool.find(_COLLECTION_NAME, dbObject);
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
DBObject res = new BasicDBObject();
if (userInfos.size() == 0) {
uid = BaseGlobal.mongoDBPool.inc("uid");
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid");
dbObject.put("uid", uid);
userInfos.add(dbObject);
BaseGlobal.mongoDBPool.save(_COLLECTION_NAME, dbObject);
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
}
Random random = new Random();
int token = random.nextInt();
uid = (int) userInfos.get(0).get("uid");
res.put("uid", uid);
res.put("token", token);
BaseGlobal.redisApp.set(RedisKey.TOKEN, String.valueOf(uid), token, -1, false);
BaseGlobal.getInstance().redisApp.set(RedisKey.TOKEN, String.valueOf(uid), token, -1, false);
PrintWriter out = response.getWriter();
out.print(res);
out.flush();

View File

@ -51,7 +51,7 @@ public class RegistController extends HttpServlet {
doc.put("plat", plat);
doc.put("state", state);
doc.put("open_time", open_time);
BaseGlobal.mongoDBPool.save("server_info", doc);
BaseGlobal.getInstance().mongoDBPool.save("server_info", doc);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("add suc");

View File

@ -17,14 +17,23 @@ public class WebContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
LOGGER.debug("dddddddddddddddddddddddddddddddd");
InputStream inputStream = null;
try {
InputStream inputStream = getClass().getResourceAsStream(DEFAULT_CONFIG_PATH);
inputStream = getClass().getResourceAsStream(DEFAULT_CONFIG_PATH);
if (inputStream != null) {
BaseGlobal.properties.load(inputStream);
BaseGlobal.getInstance().properties.load(inputStream);
}
BaseGlobal.init();
BaseGlobal.getInstance().init();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -284,7 +284,7 @@ public class RedisApp {
public boolean tryGetDistributedLock(final String type, final String key, final String requestId,
final int expireTime) throws Exception {
String fullKey = getKey(type, key, true);
// String fullKey = getKey(type, key, true);
return tryGetDistributedLock(type, key, requestId, expireTime, true);
}

View File

@ -8,10 +8,7 @@ import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class RedisUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
@ -24,12 +21,13 @@ public class RedisUtil {
protected void init() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(BaseGlobal.properties.getProperty("redis_maxTotal")));
config.setMinIdle(Integer.parseInt(BaseGlobal.properties.getProperty("redis_minIdle")));
config.setMaxWaitMillis(Integer.parseInt(BaseGlobal.properties.getProperty("redis_maxWaitMillis")));
config.setMaxIdle(Integer.parseInt(BaseGlobal.properties.getProperty("redis_maxIdle")));
config.setTestOnBorrow(Boolean.parseBoolean(BaseGlobal.properties.getProperty("redis_testOnBorrow")));
jedisPool = new JedisPool(config, BaseGlobal.properties.getProperty("redis_host"), Integer.parseInt(BaseGlobal.properties.getProperty("redis_port")));
Properties properties = BaseGlobal.getInstance().properties;
config.setMaxTotal(Integer.parseInt(properties.getProperty("redis_maxTotal")));
config.setMinIdle(Integer.parseInt(properties.getProperty("redis_minIdle")));
config.setMaxWaitMillis(Integer.parseInt(properties.getProperty("redis_maxWaitMillis")));
config.setMaxIdle(Integer.parseInt(properties.getProperty("redis_maxIdle")));
config.setTestOnBorrow(Boolean.parseBoolean(properties.getProperty("redis_testOnBorrow")));
jedisPool = new JedisPool(config, properties.getProperty("redis_host"), Integer.parseInt(properties.getProperty("redis_port")));
}
/***************************************普通键值对操作***************************************/

View File

@ -5,11 +5,27 @@ import com.ljsd.redis.RedisApp;
import java.util.Properties;
public class BaseGlobal {
public static Properties properties = new Properties();
public static MyMongoDBPool mongoDBPool;
public static RedisApp redisApp;
public Properties properties = new Properties();
public MyMongoDBPool mongoDBPool;
public RedisApp redisApp;
public static void init() {
private BaseGlobal() {
}
/**
* 获取单例
*
* @return
*/
public static BaseGlobal getInstance() {
return Instance.instance;
}
public static class Instance {
public final static BaseGlobal instance = new BaseGlobal();
}
public void init() {
mongoDBPool = new MyMongoDBPool();
redisApp = new RedisApp();
}

View File

@ -4,6 +4,7 @@ import com.mongodb.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/*
* mongodb数据库链接池
@ -15,18 +16,19 @@ public class MyMongoDBPool {
public MyMongoDBPool() {
MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
buide.connectionsPerHost(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maximumNumberOfConnections")));// 与目标数据库可以建立的最大链接数
buide.connectTimeout(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_connectTimeout")));// 与数据库建立链接的超时时间
buide.maxWaitTime(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maxWaitTime")));// 一个线程成功获取到一个可用数据库之前的最大等待时间
buide.threadsAllowedToBlockForConnectionMultiplier(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_threadsAllowedToBlockForConnectionMultiplier")));
buide.maxConnectionIdleTime(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maxConnectionIdleTime")));
buide.maxConnectionLifeTime(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maxConnectionLifeTime")));
buide.socketTimeout(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_socketTimeout")));
buide.socketKeepAlive(Boolean.parseBoolean(BaseGlobal.properties.getProperty("mongodb_socketKeepAlive")));
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")));
MongoClientOptions myOptions = buide.build();
mongoClient = new MongoClient(new ServerAddress(BaseGlobal.properties.getProperty("mongodb_host"), Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_port"))),
mongoClient = new MongoClient(new ServerAddress(properties.getProperty("mongodb_host"), Integer.parseInt(properties.getProperty("mongodb_port"))),
myOptions);
dbName = BaseGlobal.properties.getProperty("mongodb_name");
dbName = properties.getProperty("mongodb_name");
db = mongoClient.getDB(dbName);
}
@ -94,7 +96,7 @@ public class MyMongoDBPool {
BasicDBObject update = new BasicDBObject().append("$inc", new BasicDBObject().append(key, 1));
DBObject query = cursor.next();
DBObject result = collection.findAndModify(query, update);
return Integer.valueOf(result.get(key).toString()) + 1;
return Integer.parseInt(result.get(key).toString()) + 1;
} else {
searchQuery.put(key, 1);
collection.insert(searchQuery);