SSH项目中使用DWR框架实现即时精确消息推送

前端之家收集整理的这篇文章主要介绍了SSH项目中使用DWR框架实现即时精确消息推送前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本来自己想写的但是写了几句,发现语言组织能力有限。我还是在这篇文章基础上作写我的东西吧

实现项目需求及方法
目的:当数据操作发生某种异常时向相应的用户发送即时消息。例如:xx文件处理有N条数据是垃圾数据。
实现:
当数据有相应异常时,向数据库插入一条数据,并向指定用户(没有指定用户就不推送)推送消息;
前台页面监控,每个用户登录或每刷新此页面一次都调用后台程序并把用户编码传至后台存入ScriptSession,并判断当前用户有无消息,有消息立马推送;
前台页面长连接实现接收推送信息脚本并执行。
一、需要文件:1、dwr.xml(与web.xml放在同一目录下) 2、engine.js util.js (引入相应的接受或发送页面)3、dwr.jar 将它放在你webapp的WEB-INF/lib目录下。

二、编辑配置文件
1、web.xml加入 如下:
  1. <servlet>
  2. <servlet-name>dwr-invoker</servlet-name>
  3. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  4. <init-param>
  5. <param-name>debug</param-name>
  6. <param-value>true</param-value>
  7. </init-param>
  8. <init-param>
  9. <description>使用服务器推技术(反转AJAX)</description>
  10. <param-name>activeReverseAjaxEnabled</param-name>
  11. <param-value>true</param-value>
  12. </init-param>
  13. <init-param>
  14. <param-name>initApplicationScopeCreatorsAtStartup</param-name>
  15. <param-value>true</param-value>
  16. </init-param>
  17. <init-param>
  18. <param-name>crossDomainSessionSecurity</param-name>
  19. <param-value>false</param-value>
  20. </init-param>
  21. <init-param>
  22. <param-name>maxWaitAfterWrite</param-name>
  23. <param-value>100</param-value>
  24. </init-param>
  25. <load-on-startup>4</load-on-startup>
  26. </servlet>
  27. <servlet-mapping>
  28. <servlet-name>dwr-invoker</servlet-name>
  29. <url-pattern>/dwr/*</url-pattern>
  30. </servlet-mapping>


注意,要把<servlet>和其他<servlet>放在一起,<servlet-mapping>要和其他<servlet-mapping>放在一起!!

2、dwr.xml 在web.xml的同一目录下,创建dwr.xml,并且将要被调用的java类写入其中。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <dwr>
  3. <allow>
  4. <create creator="spring" javascript="Remote">
  5. <param name="beanName" value="remote" />
  6. <include method="noticeNewOrder" />
  7. <include method="getData" />
  8. <include method="putMessger" />
  9. </create>
  10. </allow>
  11. </dwr>



applicationContext.xml中加入<bean id="remote" class="包名.Remote"></bean>



三、编写被调用的java类
注:UserMng 和OrderMng 业务的具体类,被Spring管理 这里不做重点。
有关UserMng 和OrderMng的代码不用了解,知道是一部操作即可,与实现发送信息无直接的关系。

  1. package com.yds.common;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7.  
  8. import org.directwebremoting.ScriptBuffer;
  9. import org.directwebremoting.ScriptSession;
  10. import org.directwebremoting.WebContext;
  11. import org.directwebremoting.WebContextFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import com.yds.sym.entity.User;
  14. import com.yds.sym.service.UserMng;
  15. import com.yds.taskoder.service.OrderMng;
  16. public class Remote{
  17. @Autowired
  18. UserMng umng;
  19. @Autowired
  20. OrderMng oMng;
  21. //负责将当前用id户存入ScriptSession,并判断当前用户是否要推送消息
  22. public void getData(String userCode) {
  23. int con=0;//当前用户或者需推送用户待处理任务条数
  24. try {
  25. WebContext wctx = WebContextFactory.get();
  26. //得到当前页面的session
  27. ScriptSession scriptSession = wctx.getScriptSession();
  28. User user=umng.findById(Integer.parseInt(userCode));//当前用户信息(写自己代码
  29. Integer a=oMng.loadUserTask(user);//当前用户消息数
  30. con= a==null?0:a;
  31. //设置session属性用户code
  32. scriptSession.setAttribute("usercode",user.getId());
  33. ScriptBuffer script = new ScriptBuffer();
  34. if(con>0){
  35. script.appendScript("InitMsgBox(").appendData(user.getRealName()).appendScript("," + con + ");");
  36. //像当前访问页面用户推送消息
  37. scriptSession.addScript(script);
  38. }
  39. } catch (Exception e) {
  40. // TODO: handle exception
  41. e.printStackTrace();
  42. }
  43. }
  44. //负责推送消息
  45. public void putMessger(String departementid){
  46. int con=0;//当前用户或者需推送用户待处理任务条数
  47. try {
  48. Integer a=oMng.countOfOrders(Integer.parseInt(departementid));
  49. con= a==null?0:a+1;
  50. Map<Integer,Integer> map=new HashMap<Integer,Integer>();//存放需要推送的用户id
  51. List<User> list=umng.findUserBydpId(Integer.parseInt(departementid));//获取需要推送的所有用户
  52. for (int i = 0; i < list.size(); i++) {
  53. User u=list.get(i);
  54. map.put(u.getId(),u.getId());
  55. }
  56. if(!map.isEmpty()){
  57. this.noticeNewOrder(con,map);
  58. }
  59. } catch (Exception e) {
  60. // TODO: handle exception
  61. e.printStackTrace();
  62. }
  63. }
  64. // 得到推送信息并推送
  65. public void noticeNewOrder(int con,Map<Integer,Integer> umap) {
  66. WebContext wctx = WebContextFactory.get();
  67. ScriptBuffer script = new ScriptBuffer();
  68. //指定的页面(可以是动态的哟。。。我这个地址指向的是目标页面
  69. String currentPage = "/sycj/login/symSubmit.action";
  70. //得到登录页面的scriptSession的集合
  71. Collection<ScriptSession> pages = wctx.getScriptSessionsByPage(currentPage);
  72. for (ScriptSession session: pages) {
  73. if(session.getAttribute("usercode")!=null){
  74. Integer userinfo=(Integer)session.getAttribute("usercode");
  75. //判定目标用户推信息
  76. if(umap.containsValue(userinfo)){
  77. User user=umng.findById(userinfo);
  78. script.appendScript("InitMsgBox(").appendData(user.getRealName()).appendScript("," + con + ");");
  79. session.addScript(script);
  80. }
  81. }
  82. }
  83. }
  84. }


四、测试dwr

代码放入应用服务器(比如Tomcat),启动。
然后在地址栏输入http://localhost:8080/你的工程/dwr
然后点击Remote 可以看见它的两个方法getData(),noticeNewOrder() ,putMessger()说明dwr已配置成功!

五、编写jsp 和 消息框js

1、消息框activeReverseAjax.js 代码如下:
  1. //消息框初始化
  2. function InitMsgBox(fileName,mun) {
  3. var messageBox = document.getElementById("myMessageBox");
  4. messageBox.style.width = 180;
  5. messageBox.style.height = 110;
  6. messageBox.style.border = "solid black 1px";
  7. messageBox.style.position = "absolute";
  8. messageBox.style.right = 0;
  9. messageBox.style.bottom = 0;
  10. messageBox.style.display = "none";
  11.  
  12. var titleContent = "";// 消息框内容
  13. var CSStext = "margin:1px;color:black; border:2px outset;background-color:buttonface;width:16px;height:14px;font-size:12px;line-height:11px;cursor:hand;";
  14.  
  15. titleContent = titleContent
  16. + "<table width=100% height=100% cellpadding=0 cellspacing=0 border=0 >";
  17. titleContent = titleContent
  18. + "<tr style=';font-size:12px;background:#0099CC;height:20px;cursor:default'>";
  19. titleContent = titleContent
  20. + "<td style='color:white;padding-left:5px'>消息提示</td>";
  21. titleContent = titleContent
  22. + "<td style='color:#ffffff;padding-right:5px;' align=right>";
  23. titleContent = titleContent
  24. + "<span id=Close onclick='pophide()' style='" + CSStext
  25. + "font-family:System;padding-right:2px;' title='关闭'>x</span>";
  26. titleContent = titleContent + "</td></tr><tr><td colspan=2>";
  27. titleContent = titleContent
  28. + "<div id=include style='overflow:scroll;overflow-x:hidden;overflow-y:auto;HEIGHT:100%;padding-left:5px;padding-top:3px;font-size:12px;'>";
  29. titleContent = titleContent + "您好!"+fileName ;
  30. titleContent = titleContent + ",有" + mun + "条任务单等待您处理,";
  31. titleContent = titleContent + "请及时处理!<br>";
  32. titleContent = titleContent + "<br><br><br><br>";
  33. titleContent = titleContent + "</div>";
  34. titleContent = titleContent + "</td></tr></table>";
  35. messageBox.innerHTML = titleContent;
  36. // 消息框弹出方法
  37. $("#myMessageBox").slideDown(1000);
  38. }
  39.  
  40. // 消息框关闭方法
  41. function pophide() {
  42. $('#myMessageBox').slideUp(1000);
  43. }
  44. function updatePollStatus(pollStatus) {
  45. dwr.util.setValue("pollStatus",pollStatus ? "Online" : "Offline",{escapeHtml:false});
  46. }
  47.  
  48. // 页面body onload方法
  49. function tempOnLoad() {
  50. dwr.engine.setActiveReverseAjax(true);
  51. }



2、监控发送页面 在webapp目录下不做介绍重点。

注意:引入Remote.js、engine.js、util.js路径以测试页面上的路径为准。
  1. <script type='text/javascript' src="<%=request.getContextPath()%>/dwr/interface/Remote.js"></script>
  2. <script type='text/javascript' src="<%=request.getContextPath()%>/dwr/engine.js"></script>
  3. <script type='text/javascript' src="<%=request.getContextPath()%>/dwr/util.js"></script>
  4. <script type='text/javascript' src="<%=request.getContextPath()%>/js/dwr/activeReverseAjax.js"></script>
  5. <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></script>
  6. <script type="text/javascript">
  7. var path="<%=request.getContextPath()%>";
  8. $(function(){
  9. var code=$('#usercode').val();//这里是你传入的参数,可以是直接写自己的值
  10. Remote.getData(code);//调用后台程序
  11. })
  12. </script>
  13. 你的目标页面必须要有<div id="myMessageBox"></div>这是消息框,



当你在页面进行了操作需要向指定用户发送消息时调用这个js Remote.putMessger(自己的值)函数后台判断推送的目标用户

猜你在找的Ajax相关文章