目的:通过一个线程,判断SESSION是否过期,如果过期,调用客户端页面的JS,把信息推给客户端。
我们要让DWR程序支持反向AJAX。需要在web.xml中DWRServlet里添加一个初始化参数,另外要配置一个监听器,在适合的时候关闭线程。
如下是web.xml中的配置
<listener>
<listener-class>
org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener
</listener-class>
</listener>
<listener>
<listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextListener</listener-class>
</listener>
<listener>
<listener-class>CheckSessionDwrServletContextListener</listener-class>
</listener>
<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>
<!--激活轮询和Comet功能-->
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<!-- 程序开始时初始化ReverseAjaxTracker-->
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>100</param-value>
</init-param>
<!--采用polling方式推
<init-param>
<param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
<param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
定义服务器端的发布者:
public class CheckSessionDwr implements Runnable {
protected static Thread worker;
private ServerContext serverContext;
public CheckSessionDwr() {
WebContext webContext = WebContextFactory.get();//获取DWR WEB 上下文
ServletContext servletContext = webContext.getServletContext();//获得DWR SERVLET上下文
serverContext = ServerContextFactory.get(servletContext);//
webContext.getScriptSessionsByPage("");
synchronized (CheckSessionDwr.class) {
if (null == worker) {
worker = new Thread(this, "CheckSessionDwr");
worker.start();
}
}
}
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
Collection sessions = serverContext
.getScriptSessionsByPage("/reverseAjax/index.jsp");//通过getScriptSessionsByPage方法获得所有访问/reverseAjax/index.jsp这个资源的客户端浏览器的ScriptSession,并为这些session建立代理(ScriptProxy),
ScriptProxy proxy = new ScriptProxy(sessions);//这里编写判断SESSION的业务逻辑
proxy.addFunctionCall("sessionTimeOut");//通过这个代理,让客户端执行sessionTimeOut的js方法
Thread.sleep(2000);
}
} catch (InterruptedException ex) {
System.out.println("ERROR");
} finally {
System.out.println("FINALLY");
}
}
}
监听器配置
public class CheckSessionDwrServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
}
public void contextDestroyed(ServletContextEvent sce)
{
synchronized (CheckSessionDwr.class)
{
if (CheckSessionDwr.worker != null)
{
CheckSessionDwr.worker.interrupt();
}
}
}
}
WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();
ScriptBuffer script = new ScriptBuffer();
script.appendScript("receiveMessages(")
.appendData(messages) // messages is a List
.appendScript(");");
// Loop over all the users on the current page
Collection pages = wctx.getScriptSessionsByPage(currentPage);
for (Iterator it = pages.iterator(); it.hasNext();)
{
ScriptSession otherSession = (ScriptSession) it.next();
otherSession.addScript(script);
}
本文介绍如何使用DWR实现反向AJAX,通过一个后台线程定期检查SESSION状态,一旦发现过期则调用客户端页面的JS方法通知用户。文章详细展示了web.xml配置、服务器端发布者实现及监听器配置。

257

被折叠的 条评论
为什么被折叠?



