DWR3.0框架入門(3) —— ScriptSession的維護及優化

1.ScriptSession使用中存在的問題

     在上一節實現了服務器的推送功能,可是根據 ScriptSession的生命週期咱們能夠得出如下幾點的問題:

(1)ScriptSession不會與HttpSession同時建立
當咱們訪問一個頁面的時候,若是是第一次訪問,就會建立一個新的HttpSession,以後再訪問的時候,就會保持當前的Session,即便是刷新,也能保持當前的HttpSession。
可是,ScriptSession不一樣,第一次訪問,會建立一個ScriptSession,可是,若是你刷新,就會建立一個新的ScriptSession.

(2)如何獲得ScriptSession
在DWR中,咱們能夠經過WebContextFactory.get()來取得一個WebContext對象,進而經過WebContext的getScriptSession()取得ScriptSession對象。
可是要注意,在咱們自定義的Servlet中,咱們也能夠經過WebContextFactory.get()來取得一個WebContext,可是這種方法卻不能取得ScriptSession對象。由於,此WebContext對象其實不是經過DWR的上下文環境獲得的,因此,就根本沒有建立ScriptSession對象。
假設這種方式也能獲得ScriptSession的話,那麼咱們實現「推」也就能夠不侷限在DWR的上下文環境中了,那麼其靈活性就會大不少了。因此,這就是咱們不能在Servlet中實現推的緣由。

(3) 關於刷新就建立一個新的ScriptSession問題
 在咱們須要推送的頁面中,若是你刷新一下,那麼就提交一個Http的request,此時,若是是第一次,那麼就會建立一個httpSession對象,同時,請求由DwrServlet來處理後,就會建立一個ScriptSession.這個ScriptSession會和你的request請求的URI綁定放在一個由ScriptSessionManager維護的Map裏面(這裏面實際上是一個URI對應的Set,在Set裏面放置的是URI綁定的全部ScriptSession)。
當你刷新的時候,一樣的一個HttpSession,卻會建立一個新的ScriptSession,而後綁定到對應的URI上。

 (4)向全部的頁面訪問者推送
當咱們想向全部的頁面訪問者推送的時候,咱們只須要,取得全部的頁面訪問者,就能夠「推」了。
如何取得全部的頁面訪問者?

DWR3.0能夠經過 javascript


//獲得全部ScriptSession
Collection<ScriptSession> sessions = Browser.getTargetSessions();
DWR2.x能夠經過


Collection pages = webContext.getScriptSessionsByPage("/yourPage.jsp");
經過此方法,就能夠實現調用客戶端的javascript函數,實現對客戶端的操做。

(5) 在上面的推送中產生的問題
上面的方法已經能夠實現向全部的訪問者推送。可是問題是,在客戶端,若是用戶刷新一次或屢次,那麼,Collection裏面可能就保存了不少的無用的ScriptSession,因此不單單會影響性能問題,更重要的是,可能就不能實現你想要的功能。

2.如何管理有效的ScriptSession

     因爲上面的問題,咱們就須要本身管理ScriptSession。其實,有效地HttpSession,就是那個和當前的HttpSession匹配的ScriptSession。
因此,咱們就能夠本身維護一個Map,在這個Map裏面,咱們定義key就是HttpSession的Id,其值就是ScriptSession對象。
在每一次頁面載入的時候,都去註冊此ScriptSession,那麼就會把新的ScriptSession綁定到httpSession上面了。

在DWR3.0中推出了 ScriptSessionListener用來監聽ScriptSession的建立及銷燬事件。咱們可使用該監聽器來維護咱們本身的Map。

在上一節的代碼上修改Demo:

1.新建一個類實現 ScriptSessionListener接口


package sugar.dwr;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;

public class DWRScriptSessionListener implements ScriptSessionListener {
     
     //維護一個Map key爲session的Id, value爲ScriptSession對象
     public static final Map<String, ScriptSession> scriptSessionMap = new HashMap<String, ScriptSession>();

     /**
      * ScriptSession建立事件
      */
     public void sessionCreated(ScriptSessionEvent event) {
           WebContext webContext = WebContextFactory. get();
           HttpSession session = webContext.getSession();
           ScriptSession scriptSession = event.getSession();
            scriptSessionMap.put(session.getId(), scriptSession);     //添加scriptSession
           System. out.println( "session: " + session.getId() + " scriptSession: " + scriptSession.getId() + "is created!");
     }

     /**
      * ScriptSession銷燬事件
      */
     public void sessionDestroyed(ScriptSessionEvent event) {
           WebContext webContext = WebContextFactory. get();
           HttpSession session = webContext.getSession();
           ScriptSession scriptSession = scriptSessionMap.remove(session.getId());  //移除scriptSession
           System. out.println( "session: " + session.getId() + " scriptSession: " + scriptSession.getId() + "is destroyed!");
     }
     
     /**
      * 獲取全部ScriptSession
      */
     public static Collection<ScriptSession> getScriptSessions(){
            return scriptSessionMap.values();
     }
}
2.新建一個類繼承 DefaultScriptSessionManager,用來綁定 DWRScriptSessionListener


package sugar.dwr;

import org.directwebremoting.impl.DefaultScriptSessionManager;

public class DWRScriptSessionManager extends DefaultScriptSessionManager {
     public DWRScriptSessionManager(){
            //綁定一個ScriptSession增長銷燬事件的監聽器
            this.addScriptSessionListener( new DWRScriptSessionListener());
           System. out.println( "bind DWRScriptSessionListener");
     }
}
3.在web.xml中將 DWRScriptSessionManager  配置在 dwr-invoker servlet中


<init-param>
      <param-name >org.directwebremoting.extend.ScriptSessionManager </param-name>
      <param-value >sugar.dwr.DWRScriptSessionManager </param-value>
</init-param>
這樣在服務器啓動時即會綁定 ScriptSessionListener, ScriptSession在建立時會自動添加到咱們維護的Map中

4.經過如下方法獲取全部的 ScriptSession


//獲得全部ScriptSession
Collection<ScriptSession> sessions = DWRScriptSessionListener.getScriptSessions();
3.使用 ScriptSessionFilter過濾
     若是咱們不想要給全部的客戶端 推送消息,只想給特定的客戶端推送,那麼咱們可使用 ScriptSessionFilter來實現。在filter中去斷定session中的Attribute值是否是咱們給定的。

1.使用如下方法推送消息


//執行推送
Browser.withAllSessionsFiltered(filter, run);    //注意這裏調用了有filter功能的方法
2.經過參數可知咱們須要一個 ScriptSessionFilter對象,此時send的方法改寫成以下:

public void send(final String content){
           
            //過濾器
           ScriptSessionFilter filter = new ScriptSessionFilter() {
                
                 public boolean match(ScriptSession scriptSession) {
                     String tag = (String)scriptSession.getAttribute("tag" );
                     System. out.println(tag);
                      return "receiverTag" .equals(tag);
                }
           };

           Runnable run = new Runnable(){
                 private ScriptBuffer script = new ScriptBuffer();
                 public void run() {
                      //設置要調用的 js及參數
                      script.appendCall( "show", content);
                      //獲得全部ScriptSession
                     Collection<ScriptSession> sessions = DWRScriptSessionListener.getScriptSessions();
                      //遍歷每個ScriptSession
                      for (ScriptSession scriptSession : sessions){
                           scriptSession.addScript( script);
                     }
                }
           };
            //執行推送
           Browser. withAllSessionsFiltered(filter, run);    //注意這裏調用了有filter功能的方法
}
3.在打開jsp頁面時須要在 ScriptSession 中注入設定的attribute,MessagePush中的方法


public void onPageLoad(final String tag){
        //獲取當前的ScriptSession
        ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
        scriptSession.setAttribute( "tag", tag);
        System. out.println( "setAttribute");
}
4.在jsp中調用該方法

<script type= "text/javascript">
         //這個方法用來啓動該頁面的ReverseAjax功能
         dwr.engine.setActiveReverseAjax( true);
         //設置在頁面關閉時,通知服務端銷燬會話
         dwr.engine.setNotifyServerOnPageUnload( true);
        
         var tag = "receiverTag";    //自定義一個標籤
         messagePush.onPageLoad(tag);
        
         //這個函數是提供給後臺推送的時候 調用的
         function show(content){ 
             $( "#content").text(content);
         }     
</script >



這樣咱們能夠給不一樣客戶端的jsp中導入不一樣的tag值,過濾推送的客戶端
相關文章
相關標籤/搜索