liurunyu
2023-11-27 c475f9ad3290c2593897736144758b54e2b2f407
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
package com.dy.testClient.tcpConnect;
 
import java.util.*;
import org.apache.mina.core.session.IoSession;
 
 
public class MinaConnectPool {
    /**
     * 空闲会话的池(Hashtable是线程安全的)
     */
    private static Hashtable<String , Vector<IoSession>> freeSessions = new Hashtable<String ,Vector<IoSession>>();
 
    /**
     * 从会话池中得到空闲的会话,如果没有空闲的会话,本着快速反应的原则,新建一个会话,不再等待繁忙会话的释放
     * @param host
     * @param port
     * @return
     * @throws Exception
     */
    public static IoSession getSession(String host , int port , int connectTimeout) throws Exception{
        Vector<IoSession> vecter = freeSessions.get(getKey(host , port)) ;
        if(vecter == null){
            vecter = new Vector<IoSession>() ;
            freeSessions.put(getKey(host , port), vecter) ;
        }
 
        MinaConnect con = new MinaConnect() ;
 
        IoSession se = popSession(vecter , con) ;
        if(se == null){
            MinaHandler handler = new MinaHandler(connectTimeout) ;
            se = createNewSession(host, port, connectTimeout, con, handler) ;
        }
        return se ;
    }
 
    /**
     * 将会话放回池中
     * @param host
     * @param port
     * @param se
     */
    public static void freeSession(String host , int port , IoSession se){
        if(se == null){
            return ;
        }
        Vector<IoSession> vecter = freeSessions.get(getKey(host , port)) ;
        if(vecter == null){
            vecter = new Vector<IoSession>() ;
            freeSessions.put(getKey(host , port), vecter) ;
        }
        putSession(vecter, se) ;
    }
 
    /**
     * 从会话池中取得空闲的会话
     * @param vecter
     * @return
     */
    private static IoSession popSession(Vector<IoSession> vecter , MinaConnect con){
        IoSession se = null ;
        while(se == null && !vecter.isEmpty()){
            se = (IoSession)vecter.firstElement() ;
            if(se != null){
                vecter.removeElementAt(0) ;
            }
            if(!con.isConnected(se)){
                se = null ;
            }
        }
        return se ;
    }
 
    /**
     * 放新会话
     * @param vecter
     * @param se
     */
    private static void putSession(Vector<IoSession> vecter , IoSession se){
        vecter.addElement(se) ;
    }
 
    /**
     * 创建新会话
     * @param host
     * @param port
     * @param connectTimeout
     * @param con
     * @return
     * @throws Exception
     */
    private static IoSession createNewSession(String host , int port , int connectTimeout, MinaConnect con , MinaHandler handler) throws Exception{
        return con.createSession(host, port, connectTimeout , handler) ;
    }
 
    /**
     * 得到会话池的key
     * @param url
     * @param port
     * @return
     */
    private static String getKey(String host , int port){
        return host + port ;
    }
 
}