DWR高级主题之反向Ajax(DWR3介绍)

来源:互联网 发布:淘宝平价彩妆店铺 编辑:程序博客网 时间:2024/04/29 17:06
DWR高级主题之反向Ajax(DWR3介绍)
----------
我们在前面使用的DWR主要是基于DWR2.X版本的,这里我们针对DWR3进行介绍,介绍一些提示或技巧。
1. ScriptSession生命周期(创建ScriptSession以及让ScriptSession失效)
当/dwr/engine.js被包含进页面时ScriptSessions就创建了。默认情况下,ScriptSessions的生命周期由org.directwebremoting.impl.DefaultScriptSessionManager维护。

如果你调用下面这个javascript方法:

dwr.engine.setNotifyServerOnPageUnload(true);
当页面被卸载(比如强制刷新页面,卸载再加载)时,将对ScriptSessionManager发出一个远程的DWR调用,通知它让ScriptSession失效。DWR通过这个默认的同步调用,可以很好地让ScriptSession失效。关闭浏览器时,此同步调用可能会导致延迟。如果不喜欢,你可以传递第二个参数给dwr.engine.setNotifyServerOnPageUnload:
dwr.engine.setNotifyServerOnPageUnload(true, true);
第二个可选参数告诉DWR调用异步卸载器。
注意:在DWR2.X中,页面每刷新一次会多创建一个新的ScriptSession,使用上面的方式可以有效解决这个问题。
由于ScriptSession的创建机制不同于HttpSession,它会在每次页面刷新的时候都会重新创建,而销毁机制却是失去连接或者失效之后一定时间才会自动销毁,这样就可能造成服务端可能就保存了很多的无用的ScriptSession,所以不仅仅会影响性能问题,更重要的是,可能就不能实现你想要的功能。
解决方法是在接收消息的页面,也就是你调用dwr.engine.setActiveReverseAjax(true);的页面调用一个dwr的方法。
dwr.engine.setNotifyServerOnPageUnload(true);
这个方法的功能就是在销毁或刷新页面时销毁当前ScriptSession,这样就保证了服务端获取的ScriptSession集合中没有无效的ScriptSession对象。


2.非DWR线程(关于请求信息传递到非DWR线程)
非DWR线程都没有提到DWR线程创建他们。正因为如此:
WebContextFactory().get().getScriptSession()在非DWR线程中将返回null。你需要通过DWR线程向非DWR线程传递数据。


3.ScriptSessionManager(从一个非DWR线程获取ScriptSessionManager)
可以使用下面的代码:
Container container = ServerContextFactory.get().getContainer();ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);


4.可扩展性(应用程序线程是可扩展的,而请求线程则不是)
大多数反向AJAX实现需要一个单独的线程将数据推给客户端。为每一个DWR请求创建一个线程没有可扩展性。我们建议你使用线程池结合application范围内的DWR创建器。


5.Browser API(如何针对特定的ScriptSessions)
DWR的Browser API包含几个比较有用的方法用来更新浏览器。一些Browser方法需要ScriptSessionFilter,并根据ScriptSession的attribute来过滤并针对指定的ScriptSessions。如何使用ScriptSessionFilter与Browser API来区分用户:
5.1 implement ScriptSessionFilter
public class TestScriptSessionFilter implements ScriptSessionFilter{    private String attributeName;        public TestScriptSessionFilter(String attributeName){        this.attributeName = attributeName;    }    public boolean match(ScriptSession session){        Object check = session.getAttribute(attributeName);        return (check != null && check.equals(Boolean.TRUE));    }  }
5.2 在ScriptSession上设置一个属性

//在使用Filter之前的某个时间添加一个属性到ScriptSession中ScriptSession scriptSession = WebContextFactory.get().getScriptSession();String attributeName = "attr";scriptSession.setAttribute(attributeName, true);
注:这必须是从DWR发起的一个线程(WebContextFactory需要它)。

5.3 在你的反向AJAX线程中使用ScriptSessionFilter

ScriptSessionFilter filter = new TestScriptSessionFilter(attributeName);Browser.withPageFiltered(page, filter, new Runnable(){    public void run(){        //调用DWR的Util类中的setValue方法,用页面上某个元素的ID作为第一个参数的值,第二个参数就是要设置给这个元素的值        Util.setValue("divID", "value of div");    }});
或者调用一个命名函数:
ScriptSessionFilter filter = new TestScriptSessionFilter(attributeName);Browser.withPageFiltered(page, filter, new Runnable(){    public void run(){        // 调用你页面上的一个命名函数(js方法)。注:用ScriptsSessions.addFunctionCall        // 发起这个函数调用的ScriptSessions要匹配TestScriptSessionFilter        ScriptSessions.addFunctionCall("yourJavaScriptFunctionName", arg1, arg2, etc.);    }});
重要的是要注意几个Browser方法需要一个WebContext,请求必须来自DWR线程。目前这几个方法需要它:withCurrentPageFiltered,withCurrentPage和getTargetSessions.所有其它方法都能在非DWR线程里安全调用。

6. ScriptSession(在ScriptSession中设置区分属性)
区分用户在同一页上最常见的方式之一,是在ScriptSession上设置属性与在一个反向Ajax线程上获取它们。目前在ScriptSession上有两个最好的设置属性的方法:
6.1 调用一个远程的DWR方法

public void remoteMethod() {   String value = "someValue"; // this may come from the HttpSession for example   ScriptSession scriptSession = WebContextFactory.get().getScriptSession();   scriptSession.setAttribute("key", value);}
6.2 使用ScriptSessions创建和销毁时,将通知ScriptSessionListener。有关详细信息,请参见ScriptSessionListener。
public void sessionCreated(ScriptSessionEvent ev) {    HttpSession session = WebContextFactory.get().getSession();    String userId = (String) session.getAttribute("userId");    ev.getSession().setAttribute("userId", userId);}
一旦ScriptSession已填充属性,反向Ajax线程可以使用Browser API(推荐)与ScriptSessionFilter来针对指定ScriptSession或从ScriptSession来检索属性来区分用户。


7. ScriptSessionListeners(当ScriptSession创建或销毁时,如何使用ScriptSessionListeners做处理)
你需要如下代码:
Container container = ServerContextFactory.get().getContainer();ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);ScriptSessionListener listener = new ScriptSessionListener() {    public void sessionCreated(ScriptSessionEvent ev) {        HttpSession session = WebContextFactory.get().getSession();        String userId = (String) session.getAttribute("userId");        ev.getSession().setAttribute("userId", userId);    }    public void sessionDestroyed(ScriptSessionEvent ev) { }};manager.addScriptSessionListener(listener);
这里要注意的是,必须在DWR已初始化后添加ScriptSessionListeners。通常有两种方法做到这一点:
7.1 在DWR servlet初始化后,扩展DWR Servlet并执行上述代码
7.2 在一个Servlet中执行上述代码,并把这个Servlet的
<load-on-startup/>值设置得要比DWR servlet的<load-on-startup/>值高。




原创粉丝点击