dwr消息推送和tomcat集群
网友的提问:
项目中用到了dwr消息推送。而服务端是通过一个http请求后 触发dwr中的推送方法。而单个tomcat中。服务器发送的http请求和用户都在一个tomcat服务器中。这样就能精准推送到每个客户端用户 中。现在配置了apache集群后,通过负载均衡,apache把服务器的这个http请求也给分配给一个tomcat容器中。这样的话,在别的tomcat容器中的用户就接受不要消息了。有什么好的办法让这个请求分配给每个tomcat容器。
提取到的知识点是
dwr消息推送、apache集群、负载均衡
http://tonl.iteye.com/blog/1399027
使用DWR反转实现信息推送(一)
除了利用Pushlet实现信息推送外,DWR反转同样可以实现推送。
DWR的简单配置方法已经在以前的博客中写过。所以这里直接贴代码:
新建web工程,这里命名为DwrEg,包结构如下:
web.xml代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>dwr-invoke</servlet-name>
- <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
- <init-param>
- <param-name>debug</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>activeReverseAjaxEnabled</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> -->
- </servlet>
- <servlet-mapping>
- <servlet-name>dwr-invoke</servlet-name>
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>client.html</welcome-file>
- </welcome-file-list>
- </web-app>
dwr.xml代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr//dwr20.dtd">
- <dwr>
- <allow>
- <create javascript="myrevsrse" creator="new">
- <param name="class" value="com.src.MyReverse"/>
- </create>
- </allow>
- </dwr>
MyReverse.java代码如下:
- package com.src;
- import java.util.Collection;
- import org.directwebremoting.ScriptBuffer;
- import org.directwebremoting.ScriptSession;
- import org.directwebremoting.ServerContext;
- import org.directwebremoting.ServerContextFactory;
- import org.directwebremoting.WebContext;
- import org.directwebremoting.WebContextFactory;
- import org.directwebremoting.proxy.dwr.Util;
- public class MyReverse {
- public void sendMes(String mes){
- System.out.println("kao shi si bestllll...");
- send("系统消息:"+mes);
- }
- public void send(final String output) {
- org.directwebremoting.WebContext web = WebContextFactory.get();
- String page = web.getServletContext().getContextPath()+"/client.html";
- Collection sessions = web.getScriptSessionsByPage(page);
- System.out.println("size=="+sessions.size());
- Util utilAll = new Util(sessions);
- utilAll.addFunctionCall("callBack", output); //callBack是client.jsp里面的javascript函数,output是传递过去的参数
- //utilAll.setValue("news_id", output, false);
- //这种方法也可以,直接将client.html里id为news_id的textarea的值设置为output的值
- }
- public void noticeNewOrder(int id) {
- WebContext wctx = WebContextFactory.get();
- ScriptBuffer script = new ScriptBuffer();
- script.appendScript("receiveMessages(")
- .appendData(id)
- .appendScript(");");
- ServerContext sctx = ServerContextFactory.get(wctx.getServletContext());
- Collection<ScriptSession> pages = sctx.getScriptSessionsByPage("/DwrEg/client.html");
- for (ScriptSession session : pages) {
- session.addScript(script);
- }
- }
- }
client.html代码如下:
- <html>
- <script type="text/javascript" src="dwr/engine.js"></script>
- <script type="text/javascript" src="dwr/util.js"></script>
- <head>
- <script type="text/javascript">
- function callBack(data){
- alert(data);
- }
- function receiveMessages(id) {
- $('orderNotice').innerHTML = "收到id为" + id + "的新订单!";
- $('orderNotice').show();
- }
- </script>
- </head>
- <body onload="dwr.engine.setActiveReverseAjax(true);">
- <textarea rows="20" cols="20" id="news_id"></textarea>
- <br />
- <div id="orderNotice"></div>
- </body>
- </html>
推送界面dopush.html的代码如下:
- <html>
- <script type="text/javascript" src="dwr/engine.js"></script>
- <script type="text/javascript" src="dwr/util.js"></script>
- <script type="text/javascript" src="dwr/interface/myrevsrse.js"></script>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- <script type="text/javascript">
- function sendnews(){
- var new_content = dwr.util.getValue("newcontent");
- myrevsrse.sendMes(new_content);
- }
- function sendinfo(){
- myrevsrse.noticeNewOrder(12);
- }
- </script>
- </head>
- <body>
- <input type="text" name="newcontent">
- <input type = "button" value="发消息" onclick="sendnews()"/>
- <br />
- <input type = "button" value="send 2" onclick="sendinfo()"/>
- <br />
- </body>
同样,这里的问题也是怎么实现点对点的信息推送,这儿只是实现了向所有的clent.html页面推送信息,以后接着总结。。。。
tomcat集群http://blog.csdn.net/lifetragedy/article/details/7712691