📄 activeusermanager.java
字号:
package com.easyjf.bbs.business.util;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.easyjf.bbs.business.ActiveUser;
import com.easyjf.web.UserConnectManage;
import com.easyjf.bbs.business.config.BBSConfig;
public class ActiveUserManager {
private static final Logger logger = (Logger) Logger.getLogger(UserConnectManage.class.getName());
private static long maxFailureInterval = 0;// 毫秒
private static int maxOnlineUser = 200;// 同时在线的最大数
private static Thread checkThread = null;
public ActiveUserManager() {
// TODO Auto-generated constructor stub
}
private static class CheckTimeOut implements Runnable {
private Thread parentThread;
public CheckTimeOut(Thread parentThread) {
this.parentThread = parentThread;
synchronized (this) {
if (checkThread == null) {
checkThread = new Thread(this);
System.out.println("创建一个新线程!");
checkThread.start();
System.out.println("thread begin");
}
}
}
public void run() {
while (true) {
if (parentThread.isAlive()) {
try {
Thread.sleep(maxFailureInterval);
System.out.println("weak up");
int i = 0;
synchronized (ActiveUserUtil.showAll()) {// 执行删除操作
Iterator it = ActiveUserUtil.showAll().iterator();
Date now = new Date();
while (it.hasNext()) {
Object key = it.next();
ActiveUser user=(ActiveUser)key;
if (now.getTime()
- user.getLoginTime().getTime() > maxFailureInterval)// 删除超时的用户
{
ActiveUserUtil.remove(user);
logger.info("删除了一个超时的连接" + i);
i++;
}
}
System.out.println(ActiveUserUtil.showAll().size());
if (i < 5&& ActiveUserUtil.showAll().size()>100)// 如果删除少于5个,则强行删除1/2在线记录,牺牲性能的情况下保证内存
{
int num = maxOnlineUser / 2;
it = ActiveUserUtil.showAll().iterator();
while (it.hasNext() && i < num) {
logger.info("删除了一个多余的连接" + ((ActiveUser)it.next()).getIp());
ActiveUserUtil.remove((ActiveUser)it.next());
i++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
break;
}
}
logger.info("监视程序运行结束!");
}
}
public static void checkLoginValidate()//只检查认证失败次数
{
if(maxFailureInterval==0){
BBSConfig cif=BBSConfig.getInstance();
maxFailureInterval=Long.parseLong(cif.getDelay_time().trim());
}
if(checkThread==null)new CheckTimeOut(Thread.currentThread());
}
public static long getMaxFailureInterval() {
return maxFailureInterval;
}
public static void setMaxFailureInterval(long maxFailureInterval) {
ActiveUserManager.maxFailureInterval = maxFailureInterval;
}
public static int getMaxOnlineUser() {
return maxOnlineUser;
}
public static void setMaxOnlineUser(int maxOnlineUser) {
ActiveUserManager.maxOnlineUser = maxOnlineUser;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -