利用Servlet中Listener解決用戶的動態上線離線問題

首先我們來看一下我自己項目中的一個Listener的實際應用(實現判斷用戶在不同機器登錄時的情況(會被踢下前一個上線的人喲)),然後我們再來詳細講解其實現:

/**
* 用戶登錄及退出監聽器
* @author cWX16989
*
*/
public class UserSessionListener implements HttpSessionListener,
HttpSessionAttributeListener
{

public void sessionCreated(HttpSessionEvent event)
{

}

/**
* 當session銷燬時,消除用戶登陸,若查詢任務隊列中存在此用戶的待短消息查詢任務,則刪除此任務。
*/
public void sessionDestroyed(HttpSessionEvent event)
{
HttpSession session = event.getSession();

//清除程序自身維護的sessionMAP
Monitor.getInstance().getSessionMap().remove(session.getId());

UserInfoDTO userInfoDTO = (UserInfoDTO) session
.getAttribute(AttributeNameConstant.SessionAttribute.USER_INFO);

if (userInfoDTO != null)
{
//int userID = userInfoDTO.getUserID();

//若用戶登陸MAP裏存在,則清除
UserLoginStatus.getInstance().removeUserLoginInfo(userInfoDTO);
}
}

/**
* 當往session添加用戶信息屬性時,判斷用戶賬號是否已經登陸,若已登陸,則將先前登陸的用戶踢下線。
*/
public void attributeAdded(HttpSessionBindingEvent event)
{
String attributeName = event.getName();

// 如果添加的屬性名是userInfo,代表往session中添加用戶登陸信息,則先判斷該用戶是否已登錄
if (AttributeNameConstant.SessionAttribute.USER_INFO
.equals(attributeName))
{
UserInfoDTO userInfoDTO = (UserInfoDTO) event.getValue();

if (null != userInfoDTO)
{
//在userMap裏替換新的用戶信息
UserInfoDTO lastLoginUser = UserLoginStatus.getInstance()
.getUserMap().put(userInfoDTO.getUserID(), userInfoDTO);

//如果userMap裏含有用戶信息實例,則說明該用戶在其它地方已登錄,強行將其先登錄的會話踢出
if (lastLoginUser != null)
{
String sessionID = lastLoginUser.getSessionID();

HttpSession lastLoginUserSession = Monitor.getInstance()
.getSessionMap().get(sessionID);

// 若以往的用戶session存在,則清空存儲於session的登陸信息
if (lastLoginUserSession != null)
{
lastLoginUserSession
.removeAttribute(AttributeNameConstant.SessionAttribute.USER_INFO);

//添加消息 此賬號已在別處登陸!
lastLoginUserSession
.setAttribute(
AttributeNameConstant.SessionAttribute.LOGIN_ERROR_MESSAGE,
"此賬號已在別處登陸!" + "IP:"
+ userInfoDTO.getLoginIP());

//清除程序自身維護的sessionMAP
Monitor.getInstance().getSessionMap().remove(sessionID);
}
}
}
}
}
}


Listener是一個很好的東西,
能夠監聽到session,application的create,destroy,可以監聽到session,application

屬性綁定的變化,考慮了一下,可以應用在"在線人數統計","數據緩存"等各個方面,

下面是參照別人的講解:


Listener 是Servlet的監聽器,它可以監聽客戶端的請求、服務端的操作等。通過監聽器,可以自動激發一些操作,比如監聽在線的用戶的數量。當增加一個 HttpSession時,就激發sessionCreated(HttpSessionEvent se)方法,這樣就可以給在線人數加1。常用的監聽接口有以下幾個:

ServletContextAttributeListener監聽對ServletContext屬性的操作,比如增加、刪除、修改屬性。

ServletContextListener 監聽ServletContext。當創建ServletContext時,激發contextInitialized (ServletContextEvent sce)方法;當銷燬ServletContext時,激發contextDestroyed(ServletContextEvent sce)方法。

HttpSessionListener 監聽HttpSession的操作。當創建一個Session時,激發session Created(HttpSessionEvent se)方法;當銷燬一個Session時,激發sessionDestroyed (HttpSessionEvent se)方法。

HttpSessionAttributeListener 監聽HttpSession中的屬性的操作。當在Session增加一個屬性時,激發attributeAdded (HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被重新設置時,激發attributeReplaced(HttpSessionBindingEvent se) 方法。

下面我們開發一個具體的例子,這個監聽器能夠統計在線的人數。在ServletContext初始化和銷燬時,在服務器控制檯打印對應的信息。當ServletContext裏的屬性增加、改變、刪除時,在服務器控制檯打印對應的信息。

要獲得以上的功能,監聽器必須實現以下3個接口:

HttpSessionListener

ServletContextListener

ServletContextAttributeListener


1 // ==================== Program Discription =====================
2 // 程序名稱:示例14-9 : EncodingFilter .java
3 // 程序目的:學習使用監聽器
4 // ==============================================================
5 import javax.servlet.http.*;
6 import javax.servlet.*;
7
8 public class OnLineCountListener implements HttpSessionListener,
ServletContextListener,ServletContextAttributeListener
9 {
10 private int count;
11 private ServletContext context = null;
12
13 public OnLineCountListener()
14 {
15 count=0;
16 //setContext();
17 }
18 //創建一個session時激發
19 public void sessionCreated(HttpSessionEvent se)
20 {
21 count++;
22 setContext(se);
23
24 }
25 //當一個session失效時激發
26 public void sessionDestroyed(HttpSessionEvent se)
27 {
28 count--;
29 setContext(se);
30 }
31 //設置context的屬性,它將激發attributeReplaced或attributeAdded方法
32 public void setContext(HttpSessionEvent se)
33 {
34 se.getSession().getServletContext().
setAttribute("onLine",new Integer(count));
35 }
36 //增加一個新的屬性時激發
37 public void attributeAdded(ServletContextAttributeEvent event) {
38
39 log("attributeAdded('" + event.getName() + "', '" +
40 event.getValue() + "')");
41
42 }
43
44 //刪除一個新的屬性時激發
45 public void attributeRemoved(ServletContextAttributeEvent event) {
46
47 log("attributeRemoved('" + event.getName() + "', '" +
48 event.getValue() + "')");
49
50 }
51
52 //屬性被替代時激發
53 public void attributeReplaced(ServletContextAttributeEvent event) {
54
55 log("attributeReplaced('" + event.getName() + "', '" +
56 event.getValue() + "')");
57 }
58 //context刪除時激發
59 public void contextDestroyed(ServletContextEvent event) {
60
61 log("contextDestroyed()");
62 this.context = null;
63
64 }
65
66 //context初始化時激發
67 public void contextInitialized(ServletContextEvent event) {
68
69 this.context = event.getServletContext();
70 log("contextInitialized()");
71
72 }
73 private void log(String message) {
74
75 System.out.println("ContextListener: " + message);
76 }
77 }


【程序註解】
在OnLineCountListener 裏,用count代表當前在線的人數,OnLineCountListener將在Web服務器啓動時自動執行。當 OnLineCountListener構造好後,把count設置爲0。每增加一個Session,OnLineCountListener會自動調用 sessionCreated(HttpSessionEvent se)方法;每銷燬一個Session,OnLineCountListener會自動調用sessionDestroyed (HttpSessionEvent se)方法。當調用sessionCreated(HttpSessionEvent se)方法時,說明又有一個客戶在請求,此時使在線的人數(count)加1,並且把count寫到ServletContext中。 ServletContext的信息是所有客戶端共享的,這樣,每個客戶端都可以讀取到當前在線的人數。


從作用域範圍來說,Servlet的作用域有ServletContext,HttpSession,ServletRequest.

Context範圍:

ServletContextListener:
對一個應用進行全局監聽.隨應用啓動而啓動,隨應用消失而消失主要有兩個方法:

contextDestroyed(ServletContextEvent event)

在應用關閉的時候調用

contextInitialized(ServletContextEvent event)

在應用啓動的時候調用

這個監聽器主要用於一些隨着應用啓動而要完成的工作,也就是很多人說的我想在容器
啓動的時候幹..........
一般來說對"全局變量"初始化,如

public void contextInitialized(ServletContextEvent event){
ServletContex sc = event.getServletContext();
sc.setAttribute(name,value);
}

以後你就可以在任何servlet中getServletContext().getAttribute(name);
我最喜歡用它來做守護性工作,就是在contextInitialized(ServletContextEvent event)
方法中實現一個Timer,然後就讓應用在每次啓動的時候讓這個Timer工作:
程序代碼:
public void contextInitialized(ServletContextEvent event){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
//do any things
}
},0,時間間隔);
}


有人說Timer只能規定從現在開始的多長時間後,每隔多久做一次事或在什麼時間做
一次事,那我想在每月1號或每天12點做一項工作如何做呢?
你只要設一個間隔,然後每次判斷一下當時是不是那個時間段就行了啊,比如每月一號做,那你
時間間隔設爲天,即24小時一個循環,然後在run方法中判斷當時日期new Date().getDate()==1
就行了啊.如果是每天的12點,那你時間間隔設爲小時,然後在run中判斷new Date().getHour()
==12,再做某事就行了.

ServletContextAttributeListener:

這個監聽器主要監聽ServletContex對象在setAttribute()和removeAttribute()的事件,注意
也就是一個"全局變量"在被Add(第一次set),replace(對已有的變量重新賦值)和remove的時候.
分別調用下面三個方法:
public void attributeAdded(ServletContextAttributeEvent scab)這個方法不僅可以知道
哪些全局變量被加進來,而且可獲取容器在啓動時自動設置了哪些context變量:
程序代碼:
public void attributeAdded(ServletContextAttributeEvent scab){
System.out.println(scab.getName());
}
public void attributeRemoved(ServletContextAttributeEvent scab)

public void attributeReplaced(ServletContextAttributeEvent scab)


Session範圍:
HttpSessionListener:
這個監聽器主要監聽一個Session對象被生成和銷燬時發生的事件.對應有兩個方法:
程序代碼:
public void sessionCreated(HttpSessionEvent se)

public void sessionDestroyed(HttpSessionEvent se)


一般來說,一個session對象被create時,可以說明有一個新客端進入.可以用來粗略統計在線人
數,注意這不是精確的,因爲這個客戶端可能立即就關閉了,但sessionDestroyed方法卻會按一定
的策略很久以後纔會發生.

HttpSessionAttributeListener:
和ServletContextAttributeListener一樣,它監聽一個session對象的Attribut被Add(一個特定
名稱的Attribute每一次被設置),replace(已有名稱的Attribute的值被重設)和remove時的事件.
對就的有三個方法.
程序代碼:
public void attributeAdded(HttpSessionBindingEvent se)

public void attributeRemoved(HttpSessionBindingEvent se)

public void attributeReplaced(HttpSessionBindingEvent se)


上面的幾個監聽器的方法,都是在監聽應用邏輯中servlet邏輯中發生了什麼事,一般的來說.
我們只要完成邏輯功能,比如session.setAttribute("aaa","111");我只要把一個名爲aaa的變量
放在session中以便以後我能獲取它,我並不關心當session.setAttribute("aaa","111");發生時
我還要幹什麼.(當然有些時候要利用的),但對於下面這個監聽器,你應該好好發解一下:

HttpSessionBindingListener:
上面的監聽器都是作爲一個獨立的Listener在容器中控制事件的.而HttpSessionBindingListener
對在一對象中監聽該對象的狀態,實現了該接口的對象如果被作爲value被add到一個session中或從
session中remove,它就會知道自己已經作爲一個session對象或已經從session刪除,這對於一些非
純JAVA對象,生命週期長於session的對象,以及其它需要釋放資源或改變狀態的對象非常重要.
比如:
session.setAttribute("abcd","1111");
以後session.removeAttribute("abcd");因爲abcd是一個字符中,你從session中remove後,它就會
自動被垃圾回收器回收,而如果是一個connection:(只是舉例,你千萬不要加connection往session
中加入)
程序代碼:
session.setAttribute("abcd",conn);

以後session.removeAttribute("abcd");這時這個conn被從session中remove了,你已經無法獲取它
的句柄,所以你根本沒法關閉它.而在沒有remove之前你根本不知道什麼時候要被remove,你又無法
close(),那麼這個connection對象就死了.另外還有一些對象可以在被加入一個session時要鎖定
還要被remove時要解鎖,應因你在程序中無法判斷什麼時候被remove(),add還好操作,我可以先加鎖
再add,但remove就後你就找不到它的句柄了,根本沒法解鎖,所以這些操作只能在對象自身中實現.
也就是在對象被add時或remove時通知對象自己回調相應的方法:
程序代碼:
MyConn extends Connection implements HttpSessionBindingListener{
public void valueBound(HttpSessionBindingEvent se){
this.initXXX();
}
public void valueUnbound(HttpSessionBindingEvent se){

this.close();
}
}

session.setAttribute("aaa",new MyConn());
這時如果調用session.removeAttribute("aaa"),則觸發valueUnbound方法,就會自動關閉自己.
而其它的需要改變狀態的對象了是一樣.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章