`
etfired
  • 浏览: 36685 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

高效管理DWR中的ScriptSession,ScriptSessionLintener

    博客分类:
  • DWR
阅读更多

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

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

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

 

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

<init-param>
   <param-name>*</param-name>
   <param-value>*</param-value>
</init-param>

 

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

 

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

 

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

 

public class CustomScriptSessionManager extends org.directwebremoting.impl.DefaultScriptSessionManager  {

 public CustomScriptSessionManager(){
  try {
   this.addScriptSessionListener(new CustomScriptSessionListener());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 

 

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

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

 

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

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

 

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

 

package com.dwr;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.dom4j.Document;
import org.dom4j.Element;
import org.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-
 */
public class CustomScriptSessionListener  implements ScriptSessionListener  {

 public static final HttpSessionLabel SessionLabel = new HttpSessionLabel();
 
 private Collection<IListenerMessage> collection = null;
 
 public CustomScriptSessionListener() throws Exception{
  System.out.println("开始构造SSL");
  collection = new Vector<IListenerMessage>();
  SAXReader reader = new SAXReader();
  Document document = reader.read(this.getClass().getResource("ScriptSessionListener.xml"));
  Element rootElement = document.getRootElement();
  Iterator it = rootElement.elementIterator("listener");
  while(it.hasNext() && it != null){
   Element element = (Element)it.next();
   String classPath = element.getTextTrim();
   if(classPath == null || classPath.trim().equals("")){
    continue;
   }
   Class cls = Class.forName(classPath);
   Object object = cls.newInstance();
   if(object instanceof IListenerMessage){
    this.collection.add((IListenerMessage)object);
   }
  }
 }
 
 
 
 @SuppressWarnings("deprecation")
 public final void sessionCreated(ScriptSessionEvent sSessionEvent) {
//  System.out.println("创建新的ScriptSession时执行");
  ScriptSession scriptSession = sSessionEvent.getSession(); //获取新创建的SS
  WebContext webContext = WebContextFactory.get();
  HttpServletRequest httpServletRequest = webContext.getHttpServletRequest();
  HttpSession httpSession = httpServletRequest.getSession(); //获取构造SS的用户的HttpSession
  ScriptSession currSession = (ScriptSession)httpSession.getAttribute(SessionLabel.getCurrentScriptSessionID());
  if(currSession != null){
   currSession.invalidate();
  }
  for(IListenerMessage message : this.collection){
   message.sessionCreated(sSessionEvent);
  }
  httpSession.setAttribute("DWR3.CurrPath", scriptSession.getPage());
  httpSession.setAttribute("DWR3.CurrScriptSession", scriptSession);
  Collection<ScriptSession> sSCollection =  webContext.getScriptSessionsByPage(scriptSession.getPage());
  for(ScriptSession session : sSCollection){
   if(session.getAttribute(SessionLabel.getCurrentSesionID()) == null){
    continue;
   }
   for(IListenerMessage message : this.collection){
    message.sendCreateMessage(scriptSession,session);
   }
  }
  scriptSession.setAttribute(SessionLabel.getCurrentSesionID(), httpSession.getId());
 }

 @SuppressWarnings("deprecation")
 public final void sessionDestroyed(ScriptSessionEvent sSessionEvent) {//销毁一个ScriptSession时执行
  ScriptSession scriptSession = sSessionEvent.getSession();
  /*
  Object username = (Object)scriptSession.getAttribute(SessionLabel.getCurrentScritpUserName());
  if(username == null){
   username = "";
  }
  */
  for(IListenerMessage message : this.collection){
   message.sessionDestroyed(sSessionEvent);
  }
  Collection<ScriptSession> collection = ServerContextFactory.get().getScriptSessionsByPage(scriptSession.getPage());
  for(ScriptSession session : collection){
   String scritpAttrID = (String)session.getAttribute(SessionLabel.getCurrentSesionID());
   if( scritpAttrID != null){
    for(IListenerMessage message : this.collection){
     message.sendDestroyMessage(scriptSession,session);
    }
   }
  }
 }

 public static class HttpSessionLabel{
  
  private final String CurrentScriptSessionID = "DWR3.CurrScriptSession";
  
  private final String CurrentScritpUserName = "DWR.Chat.UserName";
  
  private final String CurrentSesionID = "DWR3.CurrSessionID";

  private final String CurrentPage = "DWR3.CurrPath";
  
  /**
   * 获取当前SessionScript的在线页面
   * @return currentPage
   */
  public String getCurrentPage() {
   return CurrentPage;
  }

  /**
   * 获取Session中的当前ScriptSession的ID
   * @return currentScriptSessionID
   */
  public String getCurrentScriptSessionID() {
   return CurrentScriptSessionID;
  }

  /**
   * 获取当前用户名称
   * @return currentScritpUserName
   */
  public String getCurrentScritpUserName() {
   return CurrentScritpUserName;
  }

  /**
   * 获取ScriptSession中的HttpSessionID
   * @return currentSesionID
   */
  public String getCurrentSesionID() {
   return CurrentSesionID;
  }
  
 }
}

  

以上代码是从一个通过DWR实现的及时在线聊天系统中抽取出来的ScriptSession监听器.

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

 

全案例源代码下载

4
0
分享到:
评论
5 楼 oyzm521 2013-07-10  
fyteach-ark-html

/WEB-INF/fyteach-ark-html.tld

fyteach-html

/WEB-INF/fyteach-html.tld

你好,这几个文件能上传一下?这几个文件是为了解决

服务器关闭,dwr弹出错误提示的问题?


很想知道一下,谢谢!
4 楼 oyzm521 2013-06-04  
fyteach-ark-html

/WEB-INF/fyteach-ark-html.tld

fyteach-html

/WEB-INF/fyteach-html.tld

你好,这几个文件能上传一下?这几个文件是为了解决

服务器关闭,dwr弹出错误提示的问题?
3 楼 tianhandigeng 2012-03-08  
GroupScriptSessionFilter类没有,楼主再上传一下吧
2 楼 black_test 2010-09-05  
我比较弱,问一下,怎么在Web.XML文件中配置一个自定义的ScrptSessionManager类?
1 楼 lujiawu12 2009-09-10  
兄弟,非常感谢

相关推荐

    dwr中文文档dwr中文文档dwr中文文档

    dwr中文文档dwr中文文档dwr中文文档dwr中文文档dwr中文文档dwr中文文档dwr中文文档

    DWR中文文档DWR

    DWR中文文档DWR中文文档DWR中文文档DWR中文文档DWR中文文档

    DWR中文帮助文档(最好的DWR中文帮助文档)

    最好的DWR中文帮助文档,最好的DWR中文帮助文档

    DWR中文文档(pdf)

    dwr ajax dwr框架 dwr中文手册

    DWR中文文档.pdfDWR中文文档.pdf

    DWR中文文档.pdfDWR中文文档.pdfDWR中文文档.pdfDWR中文文档.pdf

    DWR中处理List

    DWR中处理List例子,很好,经典,欢迎大家下载

    DWR中文文档 ,非常详细

    DWR中文文档 ,非常详细 自学好帮手DWR中文文档 ,非常详细 自学好帮手

    DWR中文文档v0.9PDF

    DWR中文文档v0.9

    DWR中文文档

    DWR中文文档,DWR中文文档,DWR中文文档,DWR中文文档,DWR中文文档

    DWR中文教程(外带DWR包)

    DWR中文教程(外带DWR包) 很好的入门级教程,想学习DWR的同学可以下载看看的

    DWR中文文档v0.9

    本书可以当作一本 DWR 完整的教程,也可以当作一本详细介绍 DWR 的“词典”,我 的目的只是通过本书,希望您能够了解一些 DWR 的基本知识、常用的用户界面组件、远程 方法调用等。并能够搭建 DWR 开发环境,实现 DWR...

    Ajax DWR中文文档

    Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档Ajax DWR中文文档

    DWR中文官方文档

    DWR的中文官方文档,以及V0.9的文档一共两份 在网站不容易找到合适的说明.这个包里是整合网站的dwr所有帮助信息下载下来的,因为时间缘故没有及时做成电子文档,希望有时间了在做一份. 如果还是看着不方便建议到...

    dwr dwrdwr

    dwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwr

    DWR中文文档.rar

    第5章.DWR中的JavaScript简介,含4小节;第6章.engine.js的功能,含3小节;第7章.util.js的功能,含13小节;第8章.DWR进阶,含5小节;第9章.范例精讲——购物车,含8小节;第10章.附录,含常见问题(4节)和JavaScript高级...

    dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;

    dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;dwr包;

    DWR中文文档.pdf

    DWR中文文档 包含了环境配置, 主要js讲解, 还有不错的示例, 很适合学习DWR

    DWR中文文档和dwr.jar包

    DWR中文文档和dwr.jar包 DWR中文文档v0.9Ajax向我们袭来的时候,很多写代码的程序员看到了Ajax的发展前景,但并不是每一个程序员都能将页面与代码完美整合在一起,DOM、CSS、javascript让人眼花缭乱,不知从何下手。...

    DWR2.0中文文档

    DWR2.0中文文档,讲的很好,值得一看

    dwr 框架dwr 框架dwr 框架dwr 框架dwr 框架dwr 框架

    dwr 框架dwr 框架dwr 框架dwr 框架dwr 框架dwr 框架

Global site tag (gtag.js) - Google Analytics