由于工作需要,最近在找一些解決方案,發(fā)現(xiàn)Listener是一個(gè)很好的東西,
能夠監(jiān)聽(tīng)到session,application的create,destroy,可以監(jiān)聽(tīng)到session,application
屬性綁定的變化,考慮了一下,可以應(yīng)用在"在線人數(shù)統(tǒng)計(jì)","數(shù)據(jù)緩存"等各個(gè)方面,
下面是整理的一些資料.
Listener是Servlet的監(jiān)聽(tīng)器,它可以監(jiān)聽(tīng)客戶端的請(qǐng)求、服務(wù)端的操作等。通過(guò)監(jiān)聽(tīng)器,可以自動(dòng)激發(fā)一些操作,比如監(jiān)聽(tīng)在線的 用戶的數(shù)量。當(dāng)增加一個(gè)HttpSession時(shí),就激發(fā)sessionCreated(HttpSessionEvent se)方法,這樣就可以給在線人數(shù)加1。常用的監(jiān)聽(tīng)接口有以下幾個(gè):
ServletContextAttributeListener監(jiān)聽(tīng)對(duì)ServletContext屬性的操作,比如增加、刪除、修改屬性。
ServletContextListener監(jiān)聽(tīng)ServletContext。當(dāng)創(chuàng)建ServletContext時(shí),激發(fā) contextInitialized(ServletContextEvent sce)方法;當(dāng)銷毀ServletContext時(shí),激發(fā)contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener監(jiān)聽(tīng)HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session Created(HttpSessionEvent se)方法;當(dāng)銷毀一個(gè)Session時(shí),激發(fā)sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener監(jiān)聽(tīng)HttpSession中的屬性的操作。當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā) attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。
下面我們開(kāi)發(fā)一個(gè)具體的例子,這個(gè)監(jiān)聽(tīng)器能夠統(tǒng)計(jì)在線的人數(shù)。在ServletContext初始化和銷毀時(shí),在服務(wù)器控制臺(tái)打印對(duì)應(yīng)的信息。當(dāng)ServletContext里的屬性增加、改變、刪除時(shí),在服務(wù)器控制臺(tái)打印對(duì)應(yīng)的信息。
要獲得以上的功能,監(jiān)聽(tīng)器必須實(shí)現(xiàn)以下3個(gè)接口:
HttpSessionListener
ServletContextListener
ServletContextAttributeListener
我們看具體的代碼,見(jiàn)示例14-9。
【程序源代碼】
1
// ==================== Program Discription =====================
2
// 程序名稱:示例14-9 : EncodingFilter .java
3
// 程序目的:學(xué)習(xí)使用監(jiān)聽(tīng)器
4
// ==============================================================
5
import javax.servlet.http.*;
6
import javax.servlet.*;
7
8
public class OnLineCountListener implements HttpSessionListener, ServletContextListener,
9
ServletContextAttributeListener
10

{
11
private int count;
12
13
private ServletContext context = null;
14
15
public OnLineCountListener()
16
{
17
count = 0;
18
// setContext();
19
}
20
21
// 創(chuàng)建一個(gè)session時(shí)激發(fā)
22
public void sessionCreated(HttpSessionEvent se)
23
{
24
count++;
25
setContext(se);
26
27
}
28
29
// 當(dāng)一個(gè)session失效時(shí)激發(fā)
30
public void sessionDestroyed(HttpSessionEvent se)
31
{
32
count--;
33
setContext(se);
34
}
35
36
// 設(shè)置context的屬性,它將激發(fā)attributeReplaced或attributeAdded方法
37
public void setContext(HttpSessionEvent se)
38
{
39
se.getSession().getServletContext().setAttribute("onLine", new Integer(count));
40
}
41
42
// 增加一個(gè)新的屬性時(shí)激發(fā)
43
public void attributeAdded(ServletContextAttributeEvent event)
44
{
45
46
log("attributeAdded('" + event.getName() + "', '" + event.getValue() + "')");
47
48
}
49
50
// 刪除一個(gè)新的屬性時(shí)激發(fā)
51
public void attributeRemoved(ServletContextAttributeEvent event)
52
{
53
54
log("attributeRemoved('" + event.getName() + "', '" + event.getValue() + "')");
55
56
}
57
58
// 屬性被替代時(shí)激發(fā)
59
public void attributeReplaced(ServletContextAttributeEvent event)
60
{
61
62
log("attributeReplaced('" + event.getName() + "', '" + event.getValue() + "')");
63
}
64
65
// context刪除時(shí)激發(fā)
66
public void contextDestroyed(ServletContextEvent event)
67
{
68
69
log("contextDestroyed()");
70
this.context = null;
71
72
}
73
74
// context初始化時(shí)激發(fā)
75
public void contextInitialized(ServletContextEvent event)
76
{
77
78
this.context = event.getServletContext();
79
log("contextInitialized()");
80
81
}
82
83
private void log(String message)
84
{
85
86
System.out.println("ContextListener: " + message);
87
}
88
}
89
【程序注解】
在OnLineCountListener里,用count代表當(dāng)前在線的人數(shù),OnLineCountListener將在 Web服務(wù)器啟動(dòng)時(shí)自動(dòng)執(zhí)行。當(dāng)OnLineCountListener構(gòu)造好后,把count設(shè)置為0。每增加一個(gè)Session, OnLineCountListener會(huì)自動(dòng)調(diào)用sessionCreated(HttpSessionEvent se)方法;每銷毀一個(gè)Session,OnLineCountListener會(huì)自動(dòng)調(diào)用sessionDestroyed (HttpSessionEvent se)方法。當(dāng)調(diào)用sessionCreated(HttpSessionEvent se)方法時(shí),說(shuō)明又有一個(gè)客戶在請(qǐng)求,此時(shí)使在線的人數(shù)(count)加1,并且把count寫(xiě)到ServletContext中。 ServletContext的信息是所有客戶端共享的,這樣,每個(gè)客戶端都可以讀取到當(dāng)前在線的人數(shù)。
從作用域范圍來(lái)說(shuō),Servlet的作用域有ServletContext,HttpSession,ServletRequest.
Context范圍:
ServletContextListener:
對(duì)一個(gè)應(yīng)用進(jìn)行全局監(jiān)聽(tīng).隨應(yīng)用啟動(dòng)而啟動(dòng),隨應(yīng)用消失而消失主要有兩個(gè)方法:
contextDestroyed(ServletContextEvent event)
在應(yīng)用關(guān)閉的時(shí)候調(diào)用
contextInitialized(ServletContextEvent event)
在應(yīng)用啟動(dòng)的時(shí)候調(diào)用
這個(gè)監(jiān)聽(tīng)器主要用于一些隨著應(yīng)用啟動(dòng)而要完成的工作,也就是很多人說(shuō)的我想在容器
啟動(dòng)的時(shí)候干..........
一般來(lái)說(shuō)對(duì)"全局變量"初始化,如
public void contextInitialized(ServletContextEvent event){
ServletContex sc = event.getServletContext();
sc.setAttribute(name,value);
}
以后你就可以在任何servlet中g(shù)etServletContext().getAttribute(name);
我最喜歡用它來(lái)做守護(hù)性工作,就是在contextInitialized(ServletContextEvent event)
方法中實(shí)現(xiàn)一個(gè)Timer,然后就讓?xiě)?yīng)用在每次啟動(dòng)的時(shí)候讓這個(gè)Timer工作:
程序代碼:
public void contextInitialized(ServletContextEvent event){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
//do any things
}
},0,時(shí)間間隔);
}
有人說(shuō)Timer只能規(guī)定從現(xiàn)在開(kāi)始的多長(zhǎng)時(shí)間后,每隔多久做一次事或在什么時(shí)間做
一次事,那我想在每月1號(hào)或每天12點(diǎn)做一項(xiàng)工作如何做呢?
你只要設(shè)一個(gè)間隔,然后每次判斷一下當(dāng)時(shí)是不是那個(gè)時(shí)間段就行了啊,比如每月一號(hào)做,那你
時(shí)間間隔設(shè)為天,即24小時(shí)一個(gè)循環(huán),然后在run方法中判斷當(dāng)時(shí)日期new Date().getDate()==1
就行了啊.如果是每天的12點(diǎn),那你時(shí)間間隔設(shè)為小時(shí),然后在run中判斷new Date().getHour()
==12,再做某事就行了.
ServletContextAttributeListener:
這個(gè)監(jiān)聽(tīng)器主要監(jiān)聽(tīng)ServletContex對(duì)象在setAttribute()和removeAttribute()的事件,注意
也就是一個(gè)"全局變量"在被Add(第一次set),replace(對(duì)已有的變量重新賦值)和remove的時(shí)候.
分別調(diào)用下面三個(gè)方法:
public void attributeAdded(ServletContextAttributeEvent scab)這個(gè)方法不僅可以知道
哪些全局變量被加進(jìn)來(lái),而且可獲取容器在啟動(dòng)時(shí)自動(dòng)設(shè)置了哪些context變量:
程序代碼:
public void attributeAdded(ServletContextAttributeEvent scab){
System.out.println(scab.getName());
}
public void attributeRemoved(ServletContextAttributeEvent scab)
public void attributeReplaced(ServletContextAttributeEvent scab)
Session范圍:
HttpSessionListener:
這個(gè)監(jiān)聽(tīng)器主要監(jiān)聽(tīng)一個(gè)Session對(duì)象被生成和銷毀時(shí)發(fā)生的事件.對(duì)應(yīng)有兩個(gè)方法:
程序代碼:
public void sessionCreated(HttpSessionEvent se)
public void sessionDestroyed(HttpSessionEvent se)
一般來(lái)說(shuō),一個(gè)session對(duì)象被create時(shí),可以說(shuō)明有一個(gè)新客端進(jìn)入.可以用來(lái)粗略統(tǒng)計(jì)在線人
數(shù),注意這不是精確的,因?yàn)檫@個(gè)客戶端可能立即就關(guān)閉了,但sessionDestroyed方法卻會(huì)按一定
的策略很久以后才會(huì)發(fā)生.
HttpSessionAttributeListener:
和ServletContextAttributeListener一樣,它監(jiān)聽(tīng)一個(gè)session對(duì)象的Attribut被Add(一個(gè)特定
名稱的Attribute每一次被設(shè)置),replace(已有名稱的Attribute的值被重設(shè))和remove時(shí)的事件.
對(duì)就的有三個(gè)方法.
程序代碼:
public void attributeAdded(HttpSessionBindingEvent se)
public void attributeRemoved(HttpSessionBindingEvent se)
public void attributeReplaced(HttpSessionBindingEvent se)
上面的幾個(gè)監(jiān)聽(tīng)器的方法,都是在監(jiān)聽(tīng)?wèi)?yīng)用邏輯中servlet邏輯中發(fā)生了什么事,一般的來(lái)說(shuō).
我們只要完成邏輯功能,比如session.setAttribute("aaa","111");我只要把一個(gè)名為aaa的變量
放在session中以便以后我能獲取它,我并不關(guān)心當(dāng)session.setAttribute("aaa","111");發(fā)生時(shí)
我還要干什么.(當(dāng)然有些時(shí)候要利用的),但對(duì)于下面這個(gè)監(jiān)聽(tīng)器,你應(yīng)該好好發(fā)解一下:
HttpSessionBindingListener:
上面的監(jiān)聽(tīng)器都是作為一個(gè)獨(dú)立的Listener在容器中控制事件的.而HttpSessionBindingListener
對(duì)在一對(duì)象中監(jiān)聽(tīng)該對(duì)象的狀態(tài),實(shí)現(xiàn)了該接口的對(duì)象如果被作為value被add到一個(gè)session中或從
session中remove,它就會(huì)知道自己已經(jīng)作為一個(gè)session對(duì)象或已經(jīng)從session刪除,這對(duì)于一些非
純JAVA對(duì)象,生命周期長(zhǎng)于session的對(duì)象,以及其它需要釋放資源或改變狀態(tài)的對(duì)象非常重要.
比如:
session.setAttribute("abcd","1111");
以后session.removeAttribute("abcd");因?yàn)閍bcd是一個(gè)字符中,你從session中remove后,它就會(huì)
自動(dòng)被垃圾回收器回收,而如果是一個(gè)connection:(只是舉例,你千萬(wàn)不要加connection往session
中加入)
程序代碼:
session.setAttribute("abcd",conn);
以后session.removeAttribute("abcd");這時(shí)這個(gè)conn被從session中remove了,你已經(jīng)無(wú)法獲取它
的句柄,所以你根本沒(méi)法關(guān)閉它.而在沒(méi)有remove之前你根本不知道什么時(shí)候要被remove,你又無(wú)法
close(),那么這個(gè)connection對(duì)象就死了.另外還有一些對(duì)象可以在被加入一個(gè)session時(shí)要鎖定
還要被remove時(shí)要解鎖,應(yīng)因你在程序中無(wú)法判斷什么時(shí)候被remove(),add還好操作,我可以先加鎖
再add,但remove就后你就找不到它的句柄了,根本沒(méi)法解鎖,所以這些操作只能在對(duì)象自身中實(shí)現(xiàn).
也就是在對(duì)象被add時(shí)或remove時(shí)通知對(duì)象自己回調(diào)相應(yīng)的方法:
程序代碼:
MyConn extends Connection implements HttpSessionBindingListener{
public void valueBound(HttpSessionBindingEvent se){
this.initXXX();
}
public void valueUnbound(HttpSessionBindingEvent se){
this.close();
}
}
session.setAttribute("aaa",new MyConn());
這時(shí)如果調(diào)用session.removeAttribute("aaa"),則觸發(fā)valueUnbound方法,就會(huì)自動(dòng)關(guān)閉自己.
而其它的需要改變狀態(tài)的對(duì)象了是一樣.
下面代碼是曉光兄弟的:
1
package com.kingbole.listener;
2
3
import javax.servlet.http.HttpSessionAttributeListener;
4
import javax.servlet.http.HttpSessionBindingEvent;
5
import javax.servlet.http.HttpSessionEvent;
6
import javax.servlet.http.HttpSessionListener;
7
import org.apache.log4j.Logger;
8
import com.kingbole.context.IContext;
9
import com.kingbole.mn.util.LoginUtil;
10
import com.kingbole.shop.struts.model.User;
11
12
/** *//**
13
*
14
* @author songxg
15
*
16
*/
17
public class KingboleSessionListener implements HttpSessionListener, HttpSessionAttributeListener
18

{
19
20
private static final Logger logger = Logger.getRootLogger();
21
22
/** *//**
23
* 在session被創(chuàng)建時(shí)調(diào)用,并為其綁定一個(gè)初始IContext對(duì)象
24
*
25
* @param evt
26
* 事件對(duì)象
27
*/
28
public void sessionCreated(HttpSessionEvent evt)
29
{
30
// TODO add your code here:
31
evt.getSession().setAttribute("IContext", new IContext());
32
logger.info("新建Session");
33
}
34
35
/** *//**
36
* 在Session被銷毀時(shí)調(diào)用,將User的在線狀態(tài)更改為下線
37
*
38
* @param evt
39
* 事件對(duì)象
40
*/
41
public void sessionDestroyed(HttpSessionEvent evt)
42
{
43
// TODO add your code here:
44
IContext icontext = (IContext) evt.getSession().getAttribute("IContext");
45
if (icontext != null)
46
{
47
User user = icontext.getUser();
48
if (user != null)
49
{
50
int userId = user.getId();
51
if (userId > 0)
52
{
53
// OnlineUsersSet.getInstance().removeUser(userId);
54
logger.info("用戶 " + userId + " 的Session被銷毀");
55
}
56
else
57
{
58
logger.info("錯(cuò)誤的用戶Session超時(shí)");
59
}
60
}
61
else
62
{
63
logger.info("無(wú)用戶的Session超時(shí)");
64
}
65
}
66
else
67
{
68
logger.info("Session銷毀時(shí)IContext上下文為空");
69
}
70
}
71
72
public void attributeAdded(HttpSessionBindingEvent arg0)
73
{
74
// TODO Auto-generated method stub
75
String attribute = arg0.getName();
76
if ("edu.yale.its.tp.cas.client.filter.user".equals(attribute))
77
{
78
IContext icontext = (IContext) arg0.getSession().getAttribute("IContext");
79
if (icontext == null)
80
{
81
icontext = new IContext();
82
}
83
User user = User.loadUserByUserName((String) arg0.getValue());
84
85
icontext.setUser(user);
86
arg0.getSession().setAttribute("IContext", icontext);
87
LoginUtil.mnLogin(attribute, arg0.getSession());
88
}
89
}
90
91
public void attributeRemoved(HttpSessionBindingEvent arg0)
92
{
93
// TODO Auto-generated method stub
94
95
}
96
97
public void attributeReplaced(HttpSessionBindingEvent arg0)
98
{
99
// TODO Auto-generated method stub
100
101
}
102
103
}
104
posted on 2007-07-10 09:49
冰封的愛(ài) 閱讀(164)
評(píng)論(0) 編輯 收藏 所屬分類:
J2EE