liurunyu
2023-12-01 c65b4c8e4edafaa22f8269d1a117096f19cc5328
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
package com.dy.testClient.httpCl;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.dy.testClient.rmiClient.RmiClUnit;
import com.dy.testClient.ServerProperties;
import com.dy.common.threadPool.ThreadPool;
import com.dy.common.threadPool.TreadPoolFactory;
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.mw.UnitStartedCallbackInterface;
 
public class HttpClUnit implements UnitInterface {
 
    private static Logger log = LogManager.getLogger(HttpClUnit.class) ;
    
    private static HttpClUnit instance = new HttpClUnit() ;
    
    public static HttpClUnitAdapter adapter ;
    public static HttpClUnitConfigVo confVo ;
    
    private static ThreadPool.Pool pool ;
    
    private static Integer totalRunedClientCount = 0;
    private static Integer totalOverClientCount = 0;
    
    private static Long startTime = 0L ;
    
    private HttpClUnit(){} ;
    
    public static HttpClUnit getInstance(){
        return instance ;
    }
    
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("Http Client模块适配器对象不能为空!") ;
        }
        HttpClUnit.adapter = (HttpClUnitAdapter)adapter ; 
        HttpClUnit.confVo = HttpClUnit.adapter.getConfig() ;
        if(HttpClUnit.confVo == null){
            throw new Exception("Http Client模块配置对象不能为空!") ;
        }
    }
 
    @Override
    public void start(UnitStartedCallbackInterface callback) throws Exception {
        pool = TreadPoolFactory.getThreadPoolLong() ;
        System.out.println("Http Client模块成功启动");
        this.doStart();
        callback.call(null) ;
    }
 
    @Override
    public void stop(UnitStartedCallbackInterface callback) throws Exception {
        callback.call(null);
    }
    
    private void doStart(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    while(true){
                        if(!ServerProperties.startWork){
                            Thread.sleep(100L);
                        }else{
                            startTime = System.currentTimeMillis() ;
                            for(Long i = ServerProperties.rtuAddrStart ; i <= ServerProperties.rtuAddrEnd; i++){
                                totalRunedClientCount++ ;
                                startClient(i) ;
                            }
                            while(true){
                                if(totalOverClientCount.longValue() >= totalRunedClientCount.longValue()){
                                    Long seconds = (System.currentTimeMillis() - startTime)/1000 ;
                                    RmiClUnit.getInstance().reportHadReportOver(seconds) ;
                                    System.out.println("共用时" + seconds + "秒");
                                    break ;
                                }else{
                                    Thread.sleep(100L);
                                }
                            }
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    
    private void startClient(Long imei){
        try {
            pool.putJob(new MyThreadJob("" + imei));
        } catch (Exception e) {
            log.error("HttpClUnit.startClient() ", e);
        }
    }
    
 
    public static synchronized void clientOver(){
        totalOverClientCount++;
        if(totalOverClientCount % 100 == 0){
            RmiClUnit.getInstance().reportHadReportCount(totalOverClientCount);
            System.out.println("已经发送" + totalOverClientCount + "条数据");
        }
    }
    
}