package com.shlanbao.tzsc.data.runtime.netty; import java.util.Map; import java.util.Map.Entry; import com.shlanbao.tzsc.base.interceptor.PMSXmlDataInterceptor; import com.shlanbao.tzsc.data.webservice.client.SendMessageClient; import org.jboss.netty.channel.ChannelHandler; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; import net.sf.json.JSONObject; public class ServerHandler extends IdleStateAwareChannelHandler implements ChannelHandler { /** * channel关闭的时候触发 */ @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelClosed"); super.channelClosed(ctx, e); } /** * channelConnected:建立新连接, * 我们通常可以用来检测IP是否是黑名单,比如有人恶意攻击服务器,我们在连接时可以监听发送来的请求, * 如果请求过于频繁和流量太大就可以将这个IP加入黑名单 * * */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelConnected"); NettyServer.getInstance().getMapChannel().put(ctx.getChannel(), ctx.getChannel());//有新的连接,添加Map super.channelConnected(ctx, e); } /** * 必须是链接已经建立,关闭通道的时候会触发 * channelDisconnected:链接关闭,可以再用户断线的时候清除用户的缓存数据等。 */ @Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelDisconnected"); closeMapChannels(ctx);//关闭管道,删除Map管道 super.channelDisconnected(ctx, e); } /** * 异常断开 * * */ @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { System.out.println("异常断开。。。。"); closeMapChannels(ctx);//关闭管道,删除Map管道 super.exceptionCaught(ctx, e); } /** * 接收消息 */ @Override @SuppressWarnings("unchecked") public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { try { //1 得到字符串信息 String str=(String) e.getMessage(); System.out.println(str); //2 将字符串转换成map Map map=(Map)JSONObject.fromObject(str) ; //3 遍历map for(Entry m: map.entrySet()) { //4 判断key类型,进入相应的方法 String key=m.getKey(); if(key.equals("connection")) {//建立连接,回写客户端信息 ctx.getChannel().write("服务端回写:重新获取连接成功"); }/*else if(key.equals("workOrderSts")) {//工单状态反馈MES *//** 工单状态:运行,完成,反馈状态MES Map mapBean= JSONUtil.jsonToMap(m.getValue()); for( Entry bean : mapBean.entrySet()) { System.out.println(bean.getKey().toString()+":"+bean.getValue()); }*//* System.out.println(m.getValue()); //通过webService接口反馈MES SendMessageClient.SendMsgToOrderStsDetail(m.getValue()); PMSXmlDataInterceptor.getInstance().addLog(true,"工单状态反馈接口", 1, m.getValue()); }*/ } } catch (Exception e2) { e2.printStackTrace(); } super.messageReceived(ctx, e); } /** * 空闲连接下线处理 * * @Override public void channelIdle(final ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { if ( e.getState() == IdleState.ALL_IDLE) { ChannelFuture write = ctx.getChannel().write("time out,you will close"); write.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { ctx.getChannel().close(); } }); } } */ /** *  异常,正常断开的管道关闭,删除 * * * */ public void closeMapChannels(ChannelHandlerContext ctx) { try { NettyServer.getInstance().getMapChannel().remove(ctx.getChannel()); ctx.getChannel().close(); } catch (Exception e2) { System.out.println("关闭异常。。。。"); e2.printStackTrace(); } } }