加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

高效管理DWR中的ScriptSession,ScriptSessionLintener

发布时间:2020-12-16 00:50:58 所属栏目:百科 来源:网络整理
导读:当我们在使用DWR的反向AJax是,每次页面的刷新都会产生一个ScriptSession(SS),但是我们确无从对过期的SS进行即使的销毁,虽然可以通过在每个页面访问时,自动执行某个方法,来销毁那些当前用户的非有效SS,但是这样也使得我们在代码管理上带来非常麻烦的问题. DWR

当我们在使用DWR的反向AJax是,每次页面的刷新都会产生一个ScriptSession(SS),但是我们确无从对过期的SS进行即使的销毁,虽然可以通过在每个页面访问时,自动执行某个方法,来销毁那些当前用户的非有效SS,但是这样也使得我们在代码管理上带来非常麻烦的问题.

DWR3的诞生终于给我们提供了ScritpSessionLintener(SSL)接口

本文,主要讲解如何使用ScriptSession接口.

DWR支持在Web.XML当中,配置扩展.

Xml代码
  1. <init-param>
  2. param-name>*</param-value>

但是,经过几次的实验和摸索,SSL在当中配置后,管理器会进行有效构造,但是SS在创建和销毁时,并不会执行继承了ScriptSessionListner类的相关方法.(或许是我自己项目的问题.)

经过自己的研究发现,在ScriptSessionManager类中,包含了AddScriptSessionListener方()法.这样给使用SSL带来了方便

我们只需要在Web.XML文件中配置一个自定义的ScrptSessionManager

Java代码
    publicclassCustomScriptSessionManagerextendsorg.directwebremoting.impl.DefaultScriptSessionManager{
  1. publicCustomScriptSessionManager(){
  2. try{
  3. this.addScriptSessionListener(newCustomScriptSessionListener());
  4. }catch(Exceptione){
  5. e.printStackTrace();
  6. }
  7. }

*CustomScriptSessionListener为一个实现了ScriptSessionListener接口的实体类.

通过这种方式,将SS管理程序注射近SSM内,让管理器在SS状态发生变化时,即使通过SSL进行处理

如何使得每个用户的SS都是新鲜的,每个人都有自己的方法,我在自己的管理程序内使用一个

将每个用户的Session内存放当前ScriptSession进行绑定,当用户生成一个新的ScriptSession时,根据用户的Session,并且将旧的ScriptSession进行主动销毁,并更新Session.

这样,我们随着高效的保证每个用户只有一个ScriptSession存在于内存当中,使得我们的程序更加高效.

packagecom.dwr;
  • importjava.util.Collection;
  • importjava.util.Iterator;
  • importjava.util.Vector;
  • importjavax.servlet.http.HttpServletRequest;
  • importjavax.servlet.http.HttpSession;
  • importorg.directwebremoting.ScriptSession;
  • importorg.directwebremoting.ServerContextFactory;
  • importorg.directwebremoting.WebContext;
  • importorg.directwebremoting.WebContextFactory;
  • importorg.directwebremoting.event.ScriptSessionEvent;
  • importorg.directwebremoting.event.ScriptSessionListener;
  • importorg.dom4j.Document;
  • importorg.dom4j.Element;
  • importorg.dom4j.io.SAXReader;
  • /**
  • *在线ScriptSession(SSL)监听管理器.
  • *在SSL中,每当DWR工厂在生成一个新的ScriptSession(SS)时,将被SSL捕获
  • *SSL对捕获的SS进行初始化赋值
  • *其中在SS属性中的SessionLabel.CurrentSesionID赋予当前用户的SessinID
  • *并在改用户的Session属性中的SessionLabel.CurrentScriptSessionID赋予当前的SSL
  • *然后在SessionLabel.CurrentPage中赋予当前SS的操作界面地址
  • *
  • *并且开始激活SSL插件中的sessionCreated方法
  • *当
  • *@author熊浩华-ibmsz
  • *2009-8-18:下午03:11:55-
  • */
  • classCustomScriptSessionListenerimplementsScriptSessionListener{
  • staticfinalHttpSessionLabelSessionLabel=newHttpSessionLabel();
  • privateCollection<IListenerMessage>collection=null;
  • publicCustomScriptSessionListener()throwsException{
  • System.out.println("开始构造SSL");
  • collection=newVector<IListenerMessage>();
  • SAXReaderreader=newSAXReader();
  • Documentdocument=reader.read(this.getClass().getResource("ScriptSessionListener.xml"));
  • ElementrootElement=document.getRootElement();
  • Iteratorit=rootElement.elementIterator("listener");
  • while(it.hasNext()&&it!=null){
  • Elementelement=(Element)it.next();
  • StringclassPath=element.getTextTrim();
  • if(classPath==null||classPath.trim().equals("")){
  • continue;
  • Classcls=Class.forName(classPath);
  • Objectobject=cls.newInstance();
  • if(objectinstanceofIListenerMessage){
  • this.collection.add((IListenerMessage)object);
  • @SuppressWarnings("deprecation")
  • finalvoidsessionCreated(ScriptSessionEventsSessionEvent){
  • //System.out.println("创建新的ScriptSession时执行");
  • ScriptSessionscriptSession=sSessionEvent.getSession();//获取新创建的SS
  • WebContextwebContext=WebContextFactory.get();
  • HttpServletRequesthttpServletRequest=webContext.getHttpServletRequest();
  • HttpSessionhttpSession=httpServletRequest.getSession();//获取构造SS的用户的HttpSession
  • ScriptSessioncurrSession=(ScriptSession)httpSession.getAttribute(SessionLabel.getCurrentScriptSessionID());
  • if(currSession!= currSession.invalidate();
  • for(IListenerMessagemessage:this.collection){
  • message.sessionCreated(sSessionEvent);
  • httpSession.setAttribute("DWR3.CurrPath",scriptSession.getPage());
  • httpSession.setAttribute("DWR3.CurrScriptSession",scriptSession);
  • Collection<ScriptSession>sSCollection=webContext.getScriptSessionsByPage(scriptSession.getPage());
  • for(ScriptSessionsession:sSCollection){
  • if(session.getAttribute(SessionLabel.getCurrentSesionID())== message.sendCreateMessage(scriptSession,session);
  • scriptSession.setAttribute(SessionLabel.getCurrentSesionID(),httpSession.getId());
  • voidsessionDestroyed(ScriptSessionEventsSessionEvent){//销毁一个ScriptSession时执行
  • ScriptSessionscriptSession=sSessionEvent.getSession();
  • /*
  • Objectusername=(Object)scriptSession.getAttribute(SessionLabel.getCurrentScritpUserName());
  • if(username==null){
  • username="";
  • }
  • message.sessionDestroyed(sSessionEvent);
  • Collection<ScriptSession>collection=ServerContextFactory.get().getScriptSessionsByPage(scriptSession.getPage());
  • for(ScriptSessionsession:collection){
  • StringscritpAttrID=(String)session.getAttribute(SessionLabel.getCurrentSesionID());
  • if(scritpAttrID!= message.sendDestroyMessage(scriptSession,85); font-weight:bold">classHttpSessionLabel{
  • privatefinalStringCurrentScriptSessionID="DWR3.CurrScriptSession";
  • finalStringCurrentScritpUserName="DWR.Chat.UserName";
  • finalStringCurrentSesionID="DWR3.CurrSessionID";
  • finalStringCurrentPage="DWR3.CurrPath";
  • *获取当前SessionScript的在线页面
  • *@returncurrentPage
  • publicStringgetCurrentPage(){
  • returnCurrentPage;
  • *获取Session中的当前ScriptSession的ID
  • *@returncurrentScriptSessionID
  • publicStringgetCurrentScriptSessionID(){
  • returnCurrentScriptSessionID;
  • *获取当前用户名称
  • *@returncurrentScritpUserName
  • publicStringgetCurrentScritpUserName(){
  • returnCurrentScritpUserName;
  • *获取ScriptSession中的HttpSessionID
  • *@returncurrentSesionID
  • publicStringgetCurrentSesionID(){
  • returnCurrentSesionID;
  • }
  • 以上代码是从一个通过DWR实现的及时在线聊天系统中抽取出来的ScriptSession监听器.

    红色标注部分为管理SS的程序段

    全案例源代码下载

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读