⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientcontroller.java

📁 中国联通短信通信协议
💻 JAVA
字号:
package com.wireless.sms.gwif.smsagent.workthread;

import java.util.*;
import com.wireless.sms.gwif.smsagent.global.*;
import com.wireless.gwif.socketconn.*;

public class ClientController {

  private static Timer timer = null;
  private static Vector templetContainer = new Vector();
  private static Map templetStatus = new Hashtable();
  private static final long MILLISECONDS = 10000;
  private static final long SLEEPTIME = 60 * 1000;
  //为了防止一次断开引起多次重连时的机制,对重连的对象进行hash映射
  //对于在table里存在的对象,不进行重连
  public static Hashtable htConnObject = new Hashtable();

  public synchronized static void restart(ClientDefaultConnTemplet templet){
    if (templetStatus.containsKey(templet)) {
      long time = Long.parseLong( (String) templetStatus.get(templet));
      if (System.currentTimeMillis() - time < MILLISECONDS) {
        LoggerConstant.mt_log.info("Same templet restart in " + MILLISECONDS + " milliseconds will be descard  ...");
        return;
      }

      LoggerConstant.mt_log.info("Templet " + templet + " will be restart !");
    }
    LoggerConstant.mt_log.info("压入堆栈 :"+templet);
    templetContainer.add(templet);
    templetStatus.put(templet, System.currentTimeMillis() + "");
  }
  /** 启动连接控制 */
  public static void start(){
    if( timer == null ){
      LoggerConstant.mt_log.info("启动连接控制器 ...");

      timer = new Timer(true);
      timer.schedule(new TimerTask(){
        public void run(){

          ClientDefaultConnTemplet templet = null;
          try{
            if( templetContainer.size() == 0 )
              return;

            int total = templetContainer.size();
            LoggerConstant.mt_log.info("需要重新启动的templet数 :"+total);
            for (int i=0 ; i < total; i++) {
              templet = (ClientDefaultConnTemplet) templetContainer.elementAt(i);
              LoggerConstant.mt_log.info("准备重启 :"+templet);
              restartByTemplet(templet);
            }

            while( (--total) >= 0 ){
              templetContainer.removeElementAt(total);
            }
            LoggerConstant.mt_log.info("检测容量templetContainer.size() :"+templetContainer.size());
          }
          catch(Exception e){
            SmsGWIFGlobal.sendMonitor("I000001");
            LoggerConstant.mt_log.error("Exception : ", e);
            LoggerConstant.mt_log.info("有异常产生,Timer重启 :"+templet);
            restart(templet);
          }
        }
      }, 1000, 1000);
    }
  }

  public static void stop(){
    if( timer != null ){
      timer.cancel();
      timer = null;
    }
  }


  public static void restartAll(){
    try{
      LoggerConstant.mt_log.info("重新启动所有连接 ...");

      for (int i = 0; i < SmsGWIFGlobal.vManage.size(); i++) {
        Object tmpClient = SmsGWIFGlobal.vManage.elementAt(i);
        if (tmpClient instanceof Client) {
          Client client = (Client) tmpClient;
          restart(client.getConnTemplet());
        }
      }
    }catch(Exception e){
      LoggerConstant.mt_log.error("Restart All Exception : ", e);
    }
  }


  public static void quickRestart(ClientDefaultConnTemplet templet){
    try{
      if(htConnObject.contains(templet)){
        String stime = (String) htConnObject.get(templet);
        if ( System.currentTimeMillis() - (Integer.parseInt(stime) ) < SLEEPTIME) {
          return;
        }
      }

      htConnObject.put(templet, System.currentTimeMillis() + "");
      LoggerConstant.mt_log.info("快速重新连接 ...");

      for (int i = 0; i < SmsGWIFGlobal.vManage.size(); i++) {
        Object tmpClient = SmsGWIFGlobal.vManage.elementAt(i);
        if (tmpClient instanceof Client) {
          Client client = (Client) tmpClient;
          if (client.getConnTemplet() == templet) {
            client.restart();
            break;
          }
        }
      }
    }
    catch(Exception e){
      LoggerConstant.mt_log.error("Quick Restart Exception : ", e);
    }
  }


  public static void quickClose(ClientDefaultConnTemplet templet){
    try{
      LoggerConstant.mt_log.info("关闭连接 ...");

      for (int i = 0; i < SmsGWIFGlobal.vManage.size(); i++) {
        Object tmpClient = SmsGWIFGlobal.vManage.elementAt(i);
        if (tmpClient instanceof Client) {
          Client client = (Client) tmpClient;
          if (client.getConnTemplet() == templet) {
            client.close();
            break;
          }
        }
      }
    }
    catch(Exception e){
      LoggerConstant.mt_log.error("Quick Close Exception : ", e);
    }
  }


  private static void restartByTemplet(ClientDefaultConnTemplet templet){ 
    LoggerConstant.mt_log.info("重新连接...");

    for (int j = 0; j < SmsGWIFGlobal.vManage.size(); j++) {
      Object tmpClient = SmsGWIFGlobal.vManage.elementAt(j);
      if (tmpClient instanceof Client) {
        Client client = (Client) tmpClient;
        if (client.getConnTemplet() == templet) {
          restartClient(client);
          break;
        }
      }
    }

  }


  private static void restartClient(Client client){
    client.stop();

    int j = 0;
    for (; !client.isFinishStop() && j < 60; j++) {
      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException ex) {}
    }

    LoggerConstant.mt_log.info("客户端已关闭,等待重起 ... " + j);

    try {
      Thread.sleep(SLEEPTIME);
    }
    catch (Exception e) {
    }

    client.start();
    LoggerConstant.mt_log.info("客户端重起完毕!");

    try {
      Thread.sleep(5000);
    }
    catch (Exception e) {}

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -