zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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<String,String> map=(Map<String, String>)JSONObject.fromObject(str) ;
            //3 遍历map
            for(Entry<String, String>  m: map.entrySet()) {
                //4 判断key类型,进入相应的方法
                String key=m.getKey();
                if(key.equals("connection")) {//建立连接,回写客户端信息
                    ctx.getChannel().write("服务端回写:重新获取连接成功");
                }/*else if(key.equals("workOrderSts")) {//工单状态反馈MES
                    *//** 工单状态:运行,完成,反馈状态MES
                    Map<Object, Object> mapBean= JSONUtil.jsonToMap(m.getValue());
                    for( Entry<Object, Object> 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();
        }
    }
 
}