dwr + spring 實時推送消息,通過網絡技術學習,做了個小功能,總結如下:
1、下載dwr.jar
官網地址:http://directwebremoting.org/dwr/downloads/
下載最新版本v3.0
2、導入jar
3、配置web.xml
<!-- DWR配置 START -->
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DWRServlet</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!--commet 方式-->
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<!--測試環境下,需要開啓debug模式,線上環境需要關閉-->
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<!--允許跨域-->
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<!--允許腳本標籤遠程-->
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DWRServlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- DWR配置 END -->
4、由Spring管理dwr
配置applicationContext-common.xml
<!--由spring管理dwr -->
<bean id="deviceInfo" class="com.dc.rcms.DWRDeviceInfoStatus"></bean>
5、頁面引用以下js
<!--dwr消息推送 -->
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
並且頁面 初始化,加
$(function(){
dwr.engine.setActiveReverseAjax(true);
dwr.engine.setNotifyServerOnPageUnload(true);
});
6、後臺直接要推送的內容
public DWRDeviceInfoStatus(){
//定時向前臺推送設備信息
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
<span style="white-space:pre"> </span>try{
ServerContext serverContext = ServerContextFactory.get();
Container container = serverContext.getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
Collection sessions = manager.getAllScriptSessions();
if(sessions.size() == 0 ){
return;
}
//得到設備部位信息狀態數量
Util util = new Util(sessions);
List<SEntity> list=baseDao.search("select count(id) as allnum,sum(case when status=1 then 1 end) as gznum,sum(case when status=0 then 1 end) as zcnum from rcms_device ",null);
if(list!=null && list.size()>0){
SEntity entity=list.get(0);
String jsonStr="{\"allnum\":"+repaceNull(entity.getValueAsString("allnum"))
+",\"gznum\":"+repaceNull(entity.getValueAsString("gznum"))
+",\"zcnum\":"+repaceNull(entity.getValueAsString("zcnum"))+"}";
util.addFunctionCall("showDeviceInfo", jsonStr);
}
//得到所有故障點信息
Util utilgzd = new Util(sessions);
List<SEntity> gzlist=baseDao.search("select device_code,count(id) n from rcms_device where status =1 group by device_code order by device_code", null);
if(gzlist!=null && gzlist.size()>0){
String gzdjsonStr="[";
for(int i=0;i<gzlist.size();i++){
SEntity gzdentity=gzlist.get(i);
gzdjsonStr += "{";
gzdjsonStr +="\"device_code\":\""+repaceNull(gzdentity.getValueAsString("DEVICE_CODE"))+"\",";
gzdjsonStr +="\"gzdnum\":\""+repaceNull(gzdentity.getValueAsString("n"))+"\"";
gzdjsonStr +="},";
}
gzdjsonStr = gzdjsonStr.substring(0,gzdjsonStr.length()-1);
gzdjsonStr = gzdjsonStr+"]" ;
utilgzd.addFunctionCall("showDeviceGzdInfo", gzdjsonStr);
}
}catch(Exception e){
}
}
}, 10*1000, 1000*3);
}
7、頁面接收後臺推送的消息
腳本處理
/* 後臺自動推送設備信息 */
//顯示設備正常與否信息
function showDeviceInfo(data){
var jsonstr = eval( "(" + data + ")" );
if(jsonstr!=null){
$("#bottomcount").html("<p style=\"margin-left: 20px;margin-top: 20px; font-size: 16px;\">共<span id=\"allnum\" style=\"margin-left: 10px;margin-right: 10px;\">"+jsonstr.allnum+"</span>個 正常<span id=\"zcnum\" style=\"margin-left: 10px;margin-right: 10px;\"><a class=\"icon\" target=\"dialog\" rel=\"main\" title=\"設備部位的正常數量\" width=\"850\" height=\"500\" οnclick=\"getsitezc_gznum(0);\"><font color=\"green\">"+jsonstr.zcnum+"</font></a></span>個 故障<span id=\"gznum\" style=\"margin-left: 10px;margin-right: 10px;\"><a class=\"icon\" target=\"dialog\" rel=\"main\" title=\"設備部位碼的故障數量\" width=\"850\" height=\"500\"οnclick=\"getsitezc_gznum(1);\"><font color=\"red\">"+jsonstr.gznum+"</font></a></span>個</p>");
}
}
//顯示所有故障點信息
function showDeviceGzdInfo(data){
var gzdjsonstr = eval( "(" + data + ")" );
if(gzdjsonstr!=null){
$("#gzdcount").html("<ul>");
for(var i=0; i<gzdjsonstr.length; i++) {
$("#gzdcount").append("<a target=\"dialog\" rel=\"main\" title=\"設備部位的故障點數量\" width=\"850\" height=\"500\" οnclick=\"getsitegzdnum('"+gzdjsonstr[i].device_code+"');\"><li style=\"list-style-type:none;\"><img alt=\"警示\" src=\"img/warning.png\"/><span id=\"gzsnum\" style=\"margin-left: 5px;margin-top: 20px; font-size: 14px;\">"+gzdjsonstr[i].device_code+" ( 故障 <span style=\"margin-left: 5px;margin-right: 5px;margin-top: 10px;\"><font color=\"red\" >"+gzdjsonstr[i].gzdnum + "</font></span>個)</span></li></a></ul>");
}
}
isfirst=2;
}
頁面如下
至此,大功告成。。。