您的位置:首页 > 编程语言 > Java开发

Spring Aop和websocket

2016-04-27 13:59 676 查看
SSH框架,extjs 前端框架,谷歌浏览器

设备产生故障,在后台向数据库插入故障方法前配置Aop,joinpiont获取故障等级和设备Id,利用websocket传送到前台页面,前台页面遍历树节点,判断设备Id,更改设备故障等级图标。

application.xml

<!-- 配置spring aop切面Bean -->
<bean id="check" class="com.njbh.fault.Check"/>

<aop:config>
<aop:aspect id="myAop" ref="check">
<aop:pointcut id="target" expression="execution(* com.njbh.fault.dao.FaultInfoHome.persist*(..))"/>
<aop:before method="checkValidity" pointcut-ref="target"/>
<aop:after method="addLog" pointcut-ref="target"/>
</aop:aspect>
</aop:config>check类
package com.njbh.fault;

import net.sf.json.JSONObject;

import org.aspectj.lang.JoinPoint;

import com.njbh.fault.pojo.FaultInfo;

public class Check {
public void checkValidity(){
System.out.println("------------------开始切面----------------");
}

public void addLog(JoinPoint j) throws Exception{
Object[] args = j.getArgs();
FaultInfo info = (FaultInfo) args[0];
Long FaultLevel = info.getFaultLevel();
String ModuleId = info.getModuleId();
JSONObject s=new JSONObject();
s.put("FaultLevel", FaultLevel);
s.put("ModuleId", ModuleId);
String message=s.toString();
System.out.println(message);
for (MyWebSocket set :MyWebSocket.webSocketSet) {
set.sendMessage(message);
}
//        new MyWebSocket().sendMessage(message);
System.out.println("------------------故障等级,设备Id传输完成----------------");
}

}
websocket的后台
package com.njbh.fault;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

//该注解用来指定一个URI,客
4000
户端可以通过这个URI来连接到WebSocket。类似Servlet的注解mapping。无需在web.xml中配置。
@ServerEndpoint(value = "/fualtinfohome")
public class MyWebSocket {
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;

//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>();

//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;

/**
* 连接建立成功调用的方法
* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
@OnOpen
public void onOpen(Session session){
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在线数加1
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
}

/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose(){
webSocketSet.remove(this); //从set中删除
subOnlineCount(); //在线数减1
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
}

/**
* 收到客户端消息后调用的方法
* @param message 客户端发送过来的消息
* @param session 可选的参数
* @throws Exception
*/
@OnMessage
public void onMessage(String message,Session session) throws Exception {
System.out.println(message);
this.session.getBasicRemote().sendText(message);
}

/**
* 发生错误时调用
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error){
System.out.println("发生错误");
error.printStackTrace();
}

/**
* 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
* @param message
*/
public void sendMessage(String message) throws Exception{
onMessage(message, session);
//this.session.getAsyncRemote().sendText(message);
}

public static synchronized int getOnlineCount() {
return onlineCount;
}

public static synchronized void addOnlineCount() {
MyWebSocket.onlineCount++;
}

public static synchronized void subOnlineCount() {
MyWebSocket.onlineCount--;
}
}websocket前台
var websocket = null;

//判断当前浏览器是否支持WebSocket
if('WebSocket' in window){
//websocket = new WebSocket("ws://localhost:8080/websocket/websocket");
var url = "ws://192.168.5.195:8080/receiver/fualtinfohome";
websocket = new WebSocket(url );
}
else{
alert('Not support websocket')
}

//连接发生错误的回调方法
websocket.onerror = function(){
alert('error');
};

//连接成功建立的回调方法
websocket.onopen = function(event){
alert('open');
}

//接收到消息的回调方法
websocket.onmessage = function(){
alert('信息开始:')
var faultinfo = JSON.parse(event.data);
var faultLevel = faultinfo.FaultLevel;
var moduleId = faultinfo.ModuleId;
var roonodes = treeForTab.getRootNode().childNodes; //获取主节点
findchildnode(roonodes[0]); //开始递归

function findchildnode(roonodes){
var childnodes = roonodes.childNodes;
for(var i=0;i<childnodes.length;i++){ //从节点中取出子节点依次遍历
var rootnode = childnodes[i];
// var childrootnode = rootnode.childNodes;
var maxLevel = rootnode.get("faultMaxLevel");
var rootnodeModuleId = rootnode.get("moduleId");
if(moduleId == rootnodeModuleId){
if(faultLevel > maxLevel ){
var imgUrl = '';
if(faultLevel == 5){
imgUrl = "image/Notice16.png"
}else if (faultLevel == 10){
imgUrl = "image/GeneralWarning16.png"
}else if (faultLevel == 15){
imgUrl = "image/SeriousWarning16.png"
}else {
imgUrl = "image/UrgentWarning16.png"
}
if(imgUrl != ''){
rootnode.set("icon",imgUrl);
setParentIcon(rootnode ,imgUrl);

}

}
}

if(rootnode.childNodes.length>0){ //判断子节点下是否存在子节点,个人觉得判断是否leaf不太合理,因为有时候不是leaf的节点也可能没有子节点
findchildnode(rootnode); //如果存在子节点 递归
}
}
}

function setParentIcon(node,icon){
var parent = node.parentNode;
if(parent != null){
parent.set("icon",icon);
setParentIcon(parent,icon)
}
}

}

//连接关闭的回调方法
websocket.onclose = function(){
alert('close');
}

//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function(){
websocket.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: