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 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();
}
设备产生故障,在后台向数据库插入故障方法前配置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();
}
相关文章推荐
- 数字签名
- Spring MVC坑汇总+Stackoverflow巧解答
- 通过eclipse生成doc
- final关键字用于String时的一个问题
- 使用MyEclipse格式化JSP设置
- Spring注解详解
- 练习springMVC的一个小例子,为了方便以后继续学习springmvc和别的框架整合
- SpringMVC @RequestBody接收Json对象字符串
- 设计模式之单例详解
- java IO流操作文章
- 415 Unsupported Media Type
- Java内存机制
- java中的String类常量池详解
- jdk和jre有什么不同?_jdk与jre的区别
- JAVA常用集合类
- 正则表达式知识详解之匹配开头或结尾 (java版示例)
- 正则表达式知识详解之单词边界 (java版示例)
- 正则表达式知识详解之匹配时忽略大小写 (java版示例)
- Java EE中filter(过滤器)的使用
- Struts学习之ValueStack学习