<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-194  文章-0  trackbacks-0
    Reverse Ajax主要是在BS架構中,從服務器端向多個瀏覽器主動推數據的一種技術。它的一種實現就是客戶端向服務器請求后,服務器不立即回應,從而導致一個http長連接,等到有更新數據的時候,再利用這個連接“主動”向客戶端回送。
    如果是初次接觸,那一定要看下這篇文章
    其中,詳述了這種技術和JETTY服務器Continuations功能結合時的強大性能:運行在非阻塞方式下,當多個客戶端請求時不會占用過多線程。
    最后,此文申明DWR的實現已經天然使用了JETTY這一功能。所以使用DWR還是非常有好處的。如何使用及示例上面這篇文章已經有說明,下面就以我實際使用中碰到的問題和要點做個說明。


    首先,說一下代碼的組織和聲明。
    將使用到Reverse Ajax的代碼歸入一個類,比如是NotifyClient,并用spring的bean來聲明。在將要用到這個類的地方(NodeSvcImpl類),也通過成員變量引入:

        <bean id="notifyClient" class="com.hhh.nms.remote.NotifyClient">
        
    </bean>
           
           
    <bean id="nodeSvcImpl" class="com.hhh.nms.magic.NodeSvcImpl">
            
    <property name="notifyClient" ref="notifyClient"/>
           
    </bean>


    然后在dwr.xml里這樣聲明:

    <dwr>
        
    <allow>
            
    <create creator="spring" javascript="NotifyClient">
                
    <param name="beanName" value="notifyClient"/>
                
    <include method="updateService" />
        
    </create>        
        
    </allow>
    </dwr>


    其次一個要點是,如果你不是在DWR所開的線程中使用Reverse Ajax,那WebContextFactory.get()會返回空,這是因為只有DWR自己的線程才會初始化它。那如果你是在DWR之外使用,比如說收到JMS消息,或者UDP消息后,想通知所有客戶端,你就要用到ServerContext。
    要得到ServerContext,就需要用到spring的ServletContextAware接口,下面是完整代碼:

    package com.hhh.nms.remote;

    import org.apache.log4j.Logger;

    import javax.servlet.ServletContext;
    import org.springframework.web.context.ServletContextAware;

    import java.util.Collection;

    import org.directwebremoting.ScriptBuffer;
    import org.directwebremoting.ScriptSession;
    import org.directwebremoting.WebContext;
    import org.directwebremoting.WebContextFactory;
    import org.directwebremoting.ServerContext;
    import org.directwebremoting.ServerContextFactory;
    import org.directwebremoting.proxy.dwr.Util;


    public class NotifyClient implements ServletContextAware {
        
    static Logger logger = Logger.getLogger (NotifyClient.class.getName());

        
    private ServletContext servletContext = null;
          
    public void setServletContext( ServletContext servletContext )
          {
            
    this.servletContext = servletContext;
          }    
          
        
    public int serviceUpdate (String str1, String str2) {
            logger.info (
    "entered");
            
            logger.info (
    "WebContext1"+servletContext);
            
            ServerContext ctx 
    = ServerContextFactory.get(servletContext );

    // Generate JavaScript code to call client-side

            
    // WebContext  ctx = WebContextFactory.get();
            logger.info ("WebContext"+ctx);
            
    if (ctx != null) {
                  
    //String currentPage = ctx.getCurrentPage();
                  
    //logger.info ("current page:" + currentPage);
                  
                  ScriptBuffer script 
    = new ScriptBuffer();
                  script.appendScript(
    "updatePoint(")
                      .appendData(str1)
                      .appendScript(
    ",")
                      .appendData (str2)
                      .appendScript(
    ");");
            
                
    // Push script out to clients viewing the page
                Collection<ScriptSession> sessions = 
                          ctx.getScriptSessionsByPage(
    "/ebnms/index.eb?do=dwrtest");

                logger.info (
    "jsp session size:" + sessions.size ());
                
                
    // or

                Collection
    <ScriptSession> sessions2 = 
                          ctx.getAllScriptSessions ();

                logger.info (
    "all session size:" + sessions2.size ());
                
                
                
    for (ScriptSession session : sessions2) {
                  session.addScript(script);
                }        
            }

            
    return 0;
        }
    }


    另外,ScriptBuffer的appendScript方法是插入原始字串,appendData會根據參數類型做相應轉換。
    posted on 2007-11-01 17:35 我愛佳娃 閱讀(5226) 評論(3)  編輯  收藏 所屬分類: AJAX

    評論:
    # re: DWR Reverse Ajax功能實踐的要點 2007-11-02 10:41 | benson
    謝謝樓主提供~~有機會試試長連接的應用。  回復  更多評論
      
    # re: DWR Reverse Ajax功能實踐的要點 2014-09-13 19:43 | zwl
    logger.info ("WebContext1"+servletContext);  回復  更多評論
      
    # re: DWR Reverse Ajax功能實踐的要點 2014-09-13 19:44 | zwl
    logger.info ("WebContext1"+servletContext);得到的值為空啊  回復  更多評論
      
    主站蜘蛛池模板: 白白国产永久免费视频| 亚洲AV成人片色在线观看 | 国产精品亚洲一区二区三区| 在线观看国产情趣免费视频| www在线观看免费视频| 亚洲天堂中文资源| 啦啦啦www免费视频| 免费萌白酱国产一区二区三区| 亚洲成av人片不卡无码| 亚洲AV无码之日韩精品| 免费A级毛片av无码| 偷自拍亚洲视频在线观看99| 久久久久久亚洲精品中文字幕| 免费黄色大片网站| 中文字幕无码一区二区免费| 亚洲αⅴ无码乱码在线观看性色| 亚洲乱码无码永久不卡在线 | 亚洲中文字幕无码mv| 亚洲中文字幕第一页在线| 国产人在线成免费视频| 久久久久久久久久久免费精品| 亚洲日韩乱码中文无码蜜桃 | 亚洲精品国产肉丝袜久久| 亚洲国产一区视频| 日韩免费a级毛片无码a∨| 免费无码又爽又刺激网站| 美女黄频a美女大全免费皮| 亚洲专区在线视频| 亚洲色欲久久久久综合网| 最新猫咪www免费人成| 日本免费一区二区久久人人澡 | a级毛片免费观看网站| 亚洲无吗在线视频| 亚洲av日韩av天堂影片精品| 亚洲国产成人精品91久久久| 我想看一级毛片免费的| 日本zzzzwww大片免费| 久久精品成人免费观看| ww在线观视频免费观看w| 色欲aⅴ亚洲情无码AV| 国产成人精品日本亚洲11|