加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dwr消息推送和tomcat集群

发布时间:2020-12-16 00:52:45 所属栏目:百科 来源:网络整理
导读:网友的提问: 项目中用到了dwr消息推送。而服务端是通过一个http请求后 触发dwr中的推送方法。而单个tomcat中。服务器发送的http请求和用户都在一个tomcat服务器中。这样就能精准推送到每个客户端用户 中。现在配置了apache集群后,通过负载均衡,apache把服

网友的提问:

项目中用到了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代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. servletservlet-name>dwr-invoke</servlet-class>uk.ltd.getahead.dwr.DWRServletinit-paramparam-name>debugparam-value>true>activeReverseAjaxEnabled <!--
  8. >classes>java.lang.Object>--
  9. servlet-mappingurl-pattern>/dwr/*welcome-file-listwelcome-file>client.htmlweb-app>

dwr.xml代码如下:

<!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting2.0//EN""http://getahead.org/dwr//dwr20.dtd"dwrallowcreatejavascript="myrevsrse"creator="new"paramname="class"value="com.src.MyReverse"/>
  • create MyReverse.java代码如下:

    Java代码
      packagecom.src;
    1. importjava.util.Collection;
    2. importorg.directwebremoting.ScriptBuffer;
    3. importorg.directwebremoting.ScriptSession;
    4. importorg.directwebremoting.ServerContext;
    5. importorg.directwebremoting.ServerContextFactory;
    6. importorg.directwebremoting.WebContext;
    7. importorg.directwebremoting.WebContextFactory;
    8. importorg.directwebremoting.proxy.dwr.Util;
    9. publicclassMyReverse{
    10. voidsendMes(Stringmes){
    11. System.out.println("kaoshisibestllll...");
    12. send("系统消息:"+mes);
    13. }
    14. voidsend(finalStringoutput){
    15. org.directwebremoting.WebContextweb=WebContextFactory.get();
    16. Stringpage=web.getServletContext().getContextPath()+"/client.html";
    17. Collectionsessions=web.getScriptSessionsByPage(page);
    18. System.out.println("size=="+sessions.size());
    19. UtilutilAll=newUtil(sessions);
    20. utilAll.addFunctionCall("callBack",output);//callBack是client.jsp里面的javascript函数,output是传递过去的参数
    21. //utilAll.setValue("news_id",output,false);
    22. //这种方法也可以,直接将client.html里id为news_id的textarea的值设置为output的值
    23. voidnoticeNewOrder(intid){
    24. WebContextwctx=WebContextFactory.get();
    25. ScriptBufferscript=newScriptBuffer();
    26. script.appendScript("receiveMessages(")
    27. .appendData(id)
    28. .appendScript(");");
    29. ServerContextsctx=ServerContextFactory.get(wctx.getServletContext());
    30. Collection<ScriptSession>pages=sctx.getScriptSessionsByPage("/DwrEg/client.html");
    31. for(ScriptSessionsession:pages){
    32. session.addScript(script);
    33. }

    client.html代码如下:

    Html代码 htmlscripttype="text/javascript"src="dwr/engine.js">scriptscripttype="text/javascript"src="dwr/util.js"headscripttype="text/javascript" functioncallBack(data){
  • alert(data);
  • functionreceiveMessages(id){
  • $('orderNotice').innerHTML="收到id为"+id+"的新订单!";
  • $('orderNotice').show();
  • bodyonload="dwr.engine.setActiveReverseAjax(true);"textarearows="20"cols="20"id="news_id"textareabrdivid="orderNotice"divbody 推送界面dopush.html的代码如下:

    scripttype="text/javascript"src="dwr/interface/myrevsrse.js"metahttp-equiv="Content-Type"content="text/html;charset=utf-8"title>Inserttitlehere functionsendnews(){
  • varnew_content=dwr.util.getValue("newcontent");
  • myrevsrse.sendMes(new_content);
  • functionsendinfo(){
  • myrevsrse.noticeNewOrder(12);
  • inputtype="text"name="newcontent"inputtype="button"value="发消息"onclick="sendnews()"inputtype="button"value="send2"onclick="sendinfo()" 同样,这里的问题也是怎么实现点对点的信息推送,这儿只是实现了向所有的clent.html页面推送信息,以后接着总结。。。。



    tomcat集群http://www.52php.cn/article/p-dnqaqzvd-gc.html

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    • 推荐文章
        热点阅读