liurunyu
2024-09-07 9ad6b0179be4d351105f1507779d4f41a3a41953
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
package com.dy.common.mw.channel.rmi;
 
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
 
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.mw.UnitCallbackInterface;
 
public class RmiUnit implements UnitInterface {
    
    private static RmiUnit instance = new RmiUnit() ;
    private static boolean started = false ;
    
    private static RmiUnitAdapter adapter ;
    
    private static RmiFrameWork rmiFrm = null ;
 
    
    private RmiUnit(){}
    
    public static RmiUnit getInstance(){
        return instance ;
    }
    
    
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("Rmi模块适配器对象不能为空!") ;
        }
        RmiUnit.adapter = (RmiUnitAdapter)adapter ; 
        RmiConfigVo vo = RmiUnit.adapter.getConfig() ;
        if(vo == null){
            throw new Exception("Rmi模块属性配置对象不能为空!") ;
        }
        if(vo.port == null){
            throw new Exception("Rmi模块port属性值不能为空!") ;
        }
        if(vo.context == null || vo.context.trim().equals("")){
            throw new Exception("Rmi模块context属性值不能为空!") ;
        }
    }
    /**
     * 启动模块
     */
    public void start(UnitCallbackInterface callback) throws Exception {
        if(!started){
            started = true ;
 
            RmiConfigVo vo = RmiUnit.adapter.getConfig() ;
            
            boolean isException = false ;
            try {
                
                //改局域变量为静态全局变量,防止局域变量在无引用时被垃圾回收,从而客户端得不到RMI服务端
                if(rmiFrm == null){
                    rmiFrm = new RmiFrameWorkImpl(RmiUnit.adapter);
                }
                
                //连接到本地JNDI
                Registry registry = LocateRegistry.createRegistry(vo.port);
 
                //解除注册
                UnicastRemoteObject.unexportObject(rmiFrm, true);
 
                //注册
                registry.bind(vo.context, UnicastRemoteObject.exportObject(rmiFrm, 0));
                
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Rmi通信模块启动失败!");
                isException = true ;
            }
 
            if(!isException){
                if(vo.showStartInfo != null && vo.showStartInfo){
                    System.out.println("Rmi模块成功启动,端口:" + vo.port + ",上下文为:" + vo.context );
                }
            }
            callback.call(null);
        }
    }
 
    @Override
    public void stop(UnitCallbackInterface callback) {
    }
    
 
    public static boolean isStarted() {
        return started;
    }
 
    public RmiUnitAdapter getAdapter() {
        return adapter;
    }
 
    
}