`
pengjj2
  • 浏览: 136361 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

dwr3的推送功能使用

 
阅读更多

1、web.xml的配置,红色部分为dwr推技术的主要配置

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  <init-param>
   <param-name>debug</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>logLevel</param-name>
   <param-value>ERROR</param-value>
  </init-param>
  <init-param>
   <param-name>crossDomainSessionSecurity</param-name>
   <param-value>false</param-value>
  </init-param>
  <init-param>
   <param-name>allowScriptTagRemoting</param-name>
   <param-value>true</param-value>
  </init-param>
  <!--
   <init-param> <param-name>crossDomainSessionSecurity</param-name>
   <param-value>false</param-value> </init-param> <init-param>
   <param-name>allowScriptTagRemoting</param-name>
   <param-value>true</param-value> </init-param>
  -->
  <init-param>
   <param-name>classes</param-name>
   <param-value>java.lang.Object</param-value>
  </init-param>
  <!-- 开启反转Ajax 即所谓推技术 -->
  <init-param>
   <param-name>activeReverseAjaxEnabled</param-name>
   <param-value>true</param-value>
  </init-param>
   <init-param>
       <param-name>initApplicationScopeCreatorsAtStartup</param-name>
       <param-value>true</param-value>
     </init-param>
  <init-param>
     <param-name>maxWaitAfterWrite</param-name>
     <param-value>1000</param-value>
  </init-param>
  <!-- 对dwr scriptSession 自定义管理 -->
  <init-param>
   <param-name>org.directwebremoting.extend.ScriptSessionManager</param-name>
   <param-value>com.xx.xx.util.DwrScriptSessionManagerUtil</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
 </servlet-mapping>

 

2、由于使用dwr推技术,在每次刷新的时候都会产生一个新的scriptsession,而dwr本身并没有对旧的scriptsession进行处理,所以需要自己对其进行处理,在初始化配置中加入自己的scriptsession管理类:DwrscriptSessionManagerUtil

public class DwrScriptSessionManagerUtil extends DefaultScriptSessionManager{
 public static final String SS_ID="DWR_ScriptSession_Id";
 public DwrScriptSessionManagerUtil(){
  try{
   addScriptSessionListener(new ScriptSessionListener() {
    
    public void sessionDestroyed(ScriptSessionEvent event) {
     
    }
    
    public void sessionCreated(ScriptSessionEvent event) {
     ScriptSession scriptSession = event.getSession();//得到新创建的scriptSession
     HttpSession httpSession = WebContextFactory.get().getSession();//得到产生的httpSession
     Users user = (Users) httpSession.getAttribute(Constant.LG_SESSION_USER);//得到当前用户

     //如果当前用户已经退出系统,然后销毁这个scriptsession

     if(user==null)
     {
      scriptSession.invalidate();  
      httpSession.invalidate();  
      return;
     }
     String ssId = (String) httpSession.getAttribute(SS_ID);//查找SS_ID
     if(ssId!=null)
     {
      //说明已经存在旧的scriptSession.注销这个旧的scriptSession
      DefaultScriptSession oldScriptSession = sessionMap.get(ssId);
      if(oldScriptSession!=null)
      {
       invalidate(oldScriptSession);
      }
     }
     httpSession.setAttribute(SS_ID, scriptSession.getId());
     scriptSession.setAttribute(Constant.LG_USER_ID, user.getId());//绑定用户ID到ScriptSession上
    }
   });
  }catch (Exception e) {
   System.out.println("zhelichucuo");
   e.printStackTrace();
  }
 }
 
 public static void invalidate(String ssId)
 {
  Browser.withSession(ssId, new Runnable() {
   
   public void run() {
     Collection<ScriptSession> sessions = Browser.getTargetSessions();
     for(ScriptSession session : sessions)
     {
      session.invalidate();
     }
   }
  }); 
 }
 

}

 

3.js中开启dwr推功能

在window.onload中使用dwr.engine.setActiveReverseAjax(true);

 

4、java代码中使用dwr推功能

 

//dwr3使用新的方式进行推送,2的方法已被注明为过时,一般来说,很少是做全推送的,基本都是有针对性的半推

//送,所以我们将使用一个过滤器来过滤要推送的scriptsession,这里涉及到我的具体业务,这个过滤器的原理是会对

//所有的进行循环过滤后,将符合条件的scriptsession存放到一个集合中,才开启新线程进行处理的。

Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
    
    public boolean match(ScriptSession session) {
     Long loginUserId = (Long) session.getAttribute(Constant.LG_USER_ID);
     if(loginUserId!=null && targetIdList.contains(loginUserId))
     {
      targetIdList.remove(loginUserId);//如果找到了,说明将被推送,所以不用再处理,剩下的都是要被处理的
      return true;
     }
     return false;
    }
    }, new Runnable() {
    
     private ScriptBuffer script = new ScriptBuffer();
     public void run()
     {
      script.appendCall("这里写你页面的js函数", 这个参数是传给js函数的);
      Collection<ScriptSession> sessions = Browser.getTargetSessions();
               for (ScriptSession scriptSession : sessions)
               {
                   scriptSession.addScript(script);
               }
     //ScriptSessions.addFunctionCall("ExtTalk.updateMegGroup", data);//之所以不用这个方法,是因为这个

//方法有bug的存在,会对所有不论过不过滤的scriptsession进行推送,相当于全推送,没有起到过滤的作用
    }
   });

dwr的推功能还是很方便的,用来做消息通知还是不错的

2
1
分享到:
评论
2 楼 yazihaohao 2013-04-20  
很好,没有源码包吗
1 楼 仅此而已 2011-11-16  
写的很好,学习了!!!

相关推荐

Global site tag (gtag.js) - Google Analytics