爱编程的欧巴

让我们成长吧~
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

获取在线人员数量

Posted on 2016-03-01 21:25  爱编程的欧巴  阅读(190)  评论(0编辑  收藏  举报

package com.mango.web.listener;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
/**
*
* @author CrazyMango
*
*/
public class MySessionContext {
private static MySessionContext instance;
private HashMap<String,HttpSession> mymap;
private MySessionContext() {
mymap = new HashMap<String, HttpSession>();
}
public static MySessionContext getInstance() {
if (instance == null) {
instance = new MySessionContext();
}
return instance;
}
/*
* 添加session
* */
public synchronized void AddSession(HttpSession session) {
if (session != null) {
mymap.put(session.getId(), session);
}
}
/*根据session删除session
* */
public synchronized void DelSession(HttpSession session) {
if (session != null) {
mymap.remove(session.getId());
}
}
/**
* 根据sessionid的到一个Session
* */
public synchronized HttpSession getSession(String session_id) {
if (session_id == null) return null;
return (HttpSession) mymap.get(session_id);
}
/**
* 得到在线人员的Map集合
* @return HashMap
*/
public HashMap<String, HttpSession> getMymap() {
return mymap;
}

public void setMymap(HashMap<String, HttpSession> mymap) {
this.mymap = mymap;
}
}