目的:当数据操作发生某种异常时向相应的用户发送即时消息。例如:xx文件处理有N条数据是垃圾数据。
实现:创建数据信息表T_MESSAGE;
当某文件数据处理有这种异常时,向数据库插入一条信息数据;
前台页面监控,每个用户登录或每刷新此页面一次都调用后台程序并把用户编码传至后台;
后台dao层操作,看数据库中有无信息,有则推送至前台目标用户的指定页面,同时删除数据库此条信息。
前台该页面长连接实现接收推送信息脚本并执行。
一、需要文件:1、dwr.xml(与web.xml放在同一目录下) 2、engine.js util.js (引入相应的接受或发送页面)3、dwr.jar 将它放在你webapp的WEB-INF/lib目录下。
二、编辑配置文件
1、web.xml 如下:
<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>
<init-param>
<description>使用服务器推技术(反转AJAX)</description>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>100</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
注意,要把<servlet>和其他<servlet>放在一起,<servlet-mapping>要和其他<servlet-mapping>放在一起!!
2、dwr.xml 在web.xml的同一目录下,创建dwr.xml,并且将要被调用的java类写入其中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="Remote">
<param name="class" value="com.asc.dps.function.datamanager.sendmes.Remote" />
<include method="noticeNewOrder" />
<include method="getData" />
</create>
</allow>
</dwr>
三、编写被调用的java类
注:service rubishSendInfo业务的具体类,被Spring管理 这里不做重点。
有关rubishSendInfo的代码不用了解,知道是一部操作即可,与实现发送信息无直接的关系。
public class Remote {
RubishSendInfoService rubishSendInfo = null;
//前台传参 用户CODE
public void getData(String userCode) {
rubishSendInfo = getRubishSendInfo();
//从数据库得到数据信息集合
List<RubishMessage> rms = rubishSendInfo.getMessages();
if (rms != null && rms.size() > 0) {
//得到首条信息
RubishMessage rm = rms.get(0);
//删除信息
rubishSendInfo.deleteMessage(rm);
this.noticeNewOrder(rm.getFileName(), rm.getCount(),userCode);
}
}
// 得到推送信息并推送
public void noticeNewOrder(String fileName, int con,String userCode) {
WebContext wctx = WebContextFactory.get();
//得到当前页面的session
ScriptSession scriptSession = wctx.getScriptSession();
//设置session属性值 用户code
scriptSession.setAttribute("usercode", userCode);
String currentPage = "/项目名/index.jsp";
ScriptBuffer script = new ScriptBuffer();
script.appendScript("InitMsgBox(").appendData(fileName)
.appendScript("," + con + ");");
//得到登录此页面的scriptSession的集合
Collection<ScriptSession> pages = wctx.getScriptSessionsByPage(currentPage);
for (ScriptSession session: pages) {
if(session.getAttribute("usercode")!=null){
String usercode=(String)session.getAttribute("usercode");
System.out.println("sessionattri:"+usercode );
//判定目标用户推信息
if(rubishSendInfo.checkTargetUser(usercode)){
session.addScript(script);
}
}
}
}
public void setRubishSendInfo(RubishSendInfoService rubishSendInfo) {
this.rubishSendInfo = rubishSendInfo;
}
private RubishSendInfoService getRubishSendInfo() {
if (rubishSendInfo == null) {
rubishSendInfo = (RubishSendInfoService) SpringContext
.getBean("rubishSendInfo");
}
return rubishSendInfo;
}
}
四、测试dwr
将代码放入应用服务器(比如Tomcat),启动。
然后在地址栏输入http://localhost:8080/你的工程/dwr
然后点击Remote 可以看见它的两个方法getData(),noticeNewOrder() 说明dwr已配置成功!
五、编写jsp 和 消息框js
1、消息框activeReverseAjax.js 代码如下:
//后台向页面推送的方法
function putInfo(data) {
var MyName = document.getElementByIdx_x_x_x_x("userName").value;
if (data == null) {
return;
}
var str = data.split(",");
var time = str[0];
var name = str[1];
var message = str[2];
if (MyName == name) {
var d = decodeURI(data);
var text = dwr.util.getValue("info");
dwr.util.setValue('info', text + '/n' + d);
InitMsgBox(time, name, message);
}
}
//消息框初始化
function InitMsgBox(fileName, mun) {
var messageBox = document.getElementByIdx_x_x_x_x("myMessageBox");
messageBox.style.width = 180;
messageBox.style.height = 110;
messageBox.style.border = "solid black 1px";
messageBox.style.position = "absolute";
messageBox.style.right = 0;
messageBox.style.bottom = 0;
messageBox.style.display = "none";
var titleContent = "";// 消息框内容
var CSStext = "margin:1px;color:black; border:2px outset;background-color:buttonface;width:16px;height:14px;font-size:12px;line-height:11px;cursor:hand;";
titleContent = titleContent
+ "<table width=100% height=100% cellpadding=0 cellspacing=0 border=0 >";
titleContent = titleContent
+ "<tr style=';font-size:12px;background:#0099CC;height:20px;cursor:default'>";
titleContent = titleContent
+ "<td style='color:white;padding-left:5px'>消息提示</td>";
titleContent = titleContent
+ "<td style='color:#ffffff;padding-right:5px;' align=right>";
titleContent = titleContent
+ "<span id=Close οnclick='parent.pophide()' style=/"" + CSStext
+ "font-family:System;padding-right:2px;/" title='关闭'>x</span>";
titleContent = titleContent + "</td></tr><tr><td colspan=2>";
titleContent = titleContent
+ "<div id=include style='overflow:scroll;overflow-x:hidden;overflow-y:auto;HEIGHT:100%;padding-left:5px;padding-top:3px;font-size:12px;'>";
titleContent = titleContent + "文件"+fileName ;
titleContent = titleContent + ",有垃圾数据" + mun + "条";
titleContent = titleContent + "请查看!<br>";
titleContent = titleContent + "<br><br><br><br>";
titleContent = titleContent + "</div>";
titleContent = titleContent + "</td></tr></table>";
messageBox.innerHTML = titleContent;
// 消息框弹出方法
$("#myMessageBox").slideDown(1000);
}
// 消息框关闭方法
function pophide() {
$('#myMessageBox').slideUp(1000);
}
// 读取消息时提示信息内容更改
function readMessage() {
document.getElementByIdx_x_x_x_x("newMessage1").style.display = "none";
document.getElementByIdx_x_x_x_x("newMessage").style.display = "block";
}
// 页面body onload方法
function tempOnLoad() {
dwr.engine.setActiveReverseAjax(true);
readMessage();
SendMessage.receiveMessage();
}
2、监控发送页面index.jsp 在webapp目录下。 页面运用spring security的标签,不做介绍重点。
可以不用此框架直接从session中获得用户信息,也可以从后台的HttpServletRequest得session进而得到你想要的信息。
注意:引入Remote.js、engine.js、util.js路径以测试页面上的路径为准。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=7" />
<title>登录成功</title>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/index/index.css"/>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/index/menu.css"/>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/interface/Remote.js"></script>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type='text/javascript' src="<%=request.getContextPath()%>/js/dwr/activeReverseAjax.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
var path="<%=request.getContextPath()%>";
$(function(){
var code=$('#usercode').val();
Remote.getData(code);//调用后台程序
})
</script>
</head>
<body>
<div id="admin_header">
<div class="admin_head" id="admin_head">
<table width="100%" style="table-layout:fixed">
<tr class="vt">
<td width="145"><a href="index.jsp" class="admin_logo fl">
<h1>登录成功</h1>
</a></td>
<td style="vertical-align:bottom;" class="admin_nav_bg">
<div class="admin_topbar cc">
<div style="width:500px;" class="fr">
<div class="fr">
<span class="ml20">
<input type="hidden" id="usercode" value='<sec:authentication property="principal.code"/>'/>
用户名:<sec:authentication property="principal.userText"/>|
登录次数:<sec:authentication property="principal.loginTimes"/>|
上次登录时间:<span title="<sec:authentication property="principal.lastLoginTime"/>"><sec:authentication property="principal.lastLoginTimeText"/></span>|
<a href="<%=request.getContextPath()%>/j_spring_security_logout">[注销]</a>
</span>
</div>
</div>
</div>
</tr>
</table>
</div>
</div>
<div id="myMessageBox"></div>
</body>
</html
http://wenku.baidu.com/link?url=s9gKjTB7epkk3KQCSxQeDppy1gT2JJLZ8ADxYnqFHy6FgTSsehC3xW0HG0KqDVRetacaqUO7QTSqWfIEcY7uzhjQBM4-O4yg1RsYD85HMNq