一个Socket连接管理器(心跳机制)

时间:2025-01-27 08:34:50
  • package ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • /** 
  •  * @说明 变量保持 
  •  * @author cuisuqiang 
  •  * @version 1.0 
  •  * @since 
  •  */  
  • public class SocketKeep {  
  •     private static Log logger = (SocketKeep.class);  
  •     /** 
  •      * 配置信息加载 
  •      */  
  •     public static List<SocketEntity> socketEntityList = new ArrayList<SocketEntity>();  
  •     /** 
  •      * 连接对象保持,只保持需要系统保持的连接 
  •      */  
  •     public static Map<String, SocketCui> socketMap = new LinkedHashMap<String, SocketCui>();  
  •     /** 
  •      * 连接对象是否锁定 1:锁定,其他未锁定 
  •      */  
  •     public static Map<String, String> socketIsLock = new LinkedHashMap<String, String>();  
  •     /** 
  •      * 共用连接检测间隔 
  •      */  
  •     public static int commonCheckTime = 2;  
  •     /** 
  •      * 连接的数量,一定要和实际配置的数量匹配 
  •      */  
  •     public static int socketConnCount = 0;    
  •     public static ExecutorService executorService = null;// 线程池  
  •     /** 
  •      * 初始化所有连接信息 
  •      */  
  •     public static void initSocketKeep() {  
  •         Properties properties = null;  
  •         try {  
  •             properties = new Properties();  
  •             (SocketKeep.class.getClassLoader().getResourceAsStream(""));  
  •             ("加载文件成功!");  
  •         } catch (Exception e) {  
  •             ("加载文件失败!", e);  
  •             properties = null;  
  •         }  
  •         if (null != properties) {  
  •             try {  
  •                 commonCheckTime = (("commonCheckTime"));  
  •                 socketConnCount = (("socketConnCount"));  
  •                 executorService = (socketConnCount + 1);  
  •             } catch (Exception e) {  
  •                 executorService = (1);  
  •                 ("解析共用信息时错误!", e);  
  •                 // 系统忽略这两个属性的加载异常  
  •             }  
  •             SocketEntity socketEntity = null;  
  •             for (int i = 1; i <= socketConnCount; i++) {  
  •                 String name = ("socket" + i);  
  •                 if(null != name){  
  •                     socketEntity = new SocketEntity();  
  •                     String ip = ("socket" + i + "_ip");  
  •                     String port = ("socket" + i + "_port");  
  •                     String isKeep = ("socket" + i + "_isKeep");  
  •                       
  •                     (name);  
  •                     (ip);  
  •                     ((port));  
  •                     boolean keepConn = false;  
  •                     if(null != isKeep && "1".equals(isKeep)){  
  •                         keepConn = true;  
  •                     }  
  •                     (keepConn);   
  •                     (socketEntity);  
  •                 }  
  •             }  
  •         }  
  •         ("加载Socket连接配置信息结束!");         
  •         ("开始初始化Socket连接!");  
  •         SocketCui socket = null;  
  •         for(SocketEntity socketEntity : socketEntityList){  
  •             if(null != socketEntity && ()){  
  •                 try {  
  •                     socket = new SocketCui((),());  
  •                     (0);  
  •                     (true);  
  •                     (());  
  •                 } catch (Exception e) {  
  •                     ("初始化某个连接时错误!错误的连接将放弃!资源名称:" + (), e);  
  •                     socket = null;  
  •                 }  
  •                 if(null != socket){  
  •                     ((), socket);  
  •                 }else{  
  •                     ((), new SocketCui());                   
  •                 }  
  •                 ((), "0");  
  •             }  
  •         }         
  •         // 开始执行检查  
  •         (new CheckThread());  
  •         ("初始化Socket连接结束!");  
  •     }  
  • }