jiazx0107
2025-09-01 ce4f9b9f72a4269a1f25812dadd59bfb92c7b3cf
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
package com.ld.io.netty;
 
import com.ld.io.api.IoSession;
import com.ld.io.api.IoSessionListener;
import com.ld.io.api.IoSessionQuery;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
public class NettySessionFactory implements IoSessionQuery {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static Map<String, NettySession> sessionsMap = new ConcurrentHashMap<>(50);
    private IoSessionListener sessionListener;
 
    NettySessionFactory(IoSessionListener sessionListener) {
        this.sessionListener = sessionListener;
    }
 
    @Override
    public List<IoSession> getAllSession() {
        Set<Map.Entry<String, NettySession>> entries = sessionsMap.entrySet();
        List<IoSession> list = new ArrayList<>(entries.size());
        for (Map.Entry<String, NettySession> entry : entries) {
            list.add(entry.getValue());
        }
        return list;
    }
 
    @Override
    public IoSession getSession(String id) {
        if ("".equals(id) || id == null) {
            return null;
        }
        return sessionsMap.get(id);
    }
 
    @Override
    public IoSession getSessionByBusinessKey(String businessKey) {
        if ("".equals(businessKey) || businessKey == null) {
            return null;
        }
        return sessionsMap.entrySet().stream().map(Map.Entry::getValue)
                .filter(session -> businessKey.equals(session.getBusinessKey())).findFirst().orElse(null);
    }
 
    public IoSession getSession(NettyChannel nettyChannel) {
        return sessionsMap.get(nettyChannel.getCtx().channel().id().asLongText());
    }
 
    IoSession getSession(ChannelHandlerContext ctx) {
        return sessionsMap.get(ctx.channel().id().asLongText());
    }
 
    void create(NettyChannel nettyChannel) {
        Channel channel = nettyChannel.getCtx().channel();
        String id = channel.id().asLongText();
        InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
 
        NettySession nettySession = new NettySession();
        nettySession.setId(id);
        nettySession.setAddress(address.getAddress().getHostAddress());
        nettySession.setPort(address.getPort());
        nettySession.setCreateTime(new Date());
        nettySession.setNettyChannel(nettyChannel);
        nettySession.setNettySessionFactory(this);
 
        sessionsMap.put(id, nettySession);
        logger.info("Add new session to IoSessionFactory, nettySession=" + nettySession.toString());
        fireCreateEvent(nettySession);
    }
 
    void destroy(ChannelHandlerContext ctx) {
        this.destroy(ctx.channel().id().asLongText());
    }
 
    private void destroy(String id) {
        NettySession nettySession = sessionsMap.get(id);
        if (nettySession != null) {
            destroy(nettySession);
        }
    }
 
    void destroy(NettySession nettySession) {
        logger.info("Destroy session by ctx, id={}, businessKey={}", nettySession.getId(), nettySession.getBusinessKey());
        nettySession.getNettyChannel().destroy();
        sessionsMap.remove(nettySession.getId());
        fireDestroyEvent(nettySession);
    }
 
    private void fireCreateEvent(NettySession nettySession) {
        if (sessionListener != null) {
            try {
                sessionListener.onCreate(nettySession);
            } catch (Exception e) {
                logger.error("Create session error, bizKey=" + nettySession.getBusinessKey(), e);
            }
 
        }
    }
 
    private void fireDestroyEvent(NettySession nettySession) {
        if (sessionListener != null) {
            try {
                sessionListener.onDestroy(nettySession);
            } catch (Exception e) {
                logger.error("Destroy session error, bizKey=" + nettySession.getBusinessKey(), e);
            }
 
        }
    }
 
 
}