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

📄 cmppmaster.java

📁 cmpp的开发的短信端口实例
💻 JAVA
字号:
package com.zhanghao.cmpp;
/**
 * <p>Title: CMPPMaster</p>
 * <p>Description: 湖南移动短信网关通讯程序
 * CMPPMaster是网关通讯程序的主线程,通过她初始化CMPP通讯的参数,启动并维护收发线程</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: sunun tech ltd.</p>
 * @author zhanghao
 * @version 1.0
 */

import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Properties;

import com.zhanghao.common.util.*;
import com.zhanghao.common.database.*;


import com.zhanghao.provision.*;

public class CMPPMaster extends Thread{

  private String configProperty ="";

  /** 双连接 MT连接 **/
  private CMPPMTSocketProcess MTSocketProcess;

  private CMPPMTSendThread MTSendThread;

  private CMPPMTReceiveThread MTReceiveThread;

  /** 双连接的MO连接或单连接的Socket连接 **/
  private CMPPMOSocketProcess MOSocketProcess;

  private CMPPMOSendThread MOSendThread;

  private CMPPMOReceiveThread MOReceiveThread;

  private HttpServices httpServices;

  private boolean isRun = false;

  public CMPPMaster() {

    //加载配置参数
    loadInitCfg();

    //设置日志路径
    LogUtil.setLogPath(CMPParameter.LogFilePath);

    //初始化连接池
    initConnectionPool();


  }


  public boolean startHttpService(){
    try{
      new HttpServices().start();
      return true;
    }
    catch (Exception ex) {
      return false;
    }
  }

  public void run(){

    while(true){

      //判断数据库是否连接
      CMPPDBAccess.getInstance();

      //当数据库连接失效时候,需断开所有的Socket连接,以防止数据丢失

      //单连接时候
      if(CMPParameter.ISMGType == 2){
        maintenanceMOProcess();
      }
      //双连接时候
      else{
        maintenanceMOProcess();
        maintenanceMTProcess();
      }

      PublicFunction.sleep(3000);
    }
  }



  /**初始化连接池 **/
  public boolean initConnectionPool(){
    //初始化数据库连接池:
    try{
      ConnectionPool.initConnectionPool(
          CMPParameter.DBDriver,
          CMPParameter.Connect_URL,
          CMPParameter.DBUser,
          CMPParameter.DBPass,
          CMPParameter.DBPoolSize,
          20
          );
      Debug.outInfo("[CMPPMaster]"+PublicFunction.getFormatTime()+" 初始化数据库连接池成功,大小"+CMPParameter.DBPoolSize+",超时间隔20秒");
      return true;
    }catch(Exception e){
      Debug.outInfo("[CMPPMaster]"+PublicFunction.getFormatTime()+" 初始化数据库连接池失败 "  +e.toString());
      return false;
    }

  }

  /** 维护MT线程组**/
  public void maintenanceMTProcess(){
    MTSocketProcess = CMPPMTSocketProcess.getInstance();
    if(!MTSocketProcess.isAvail){
      //MOSocketProcess连接无效时,停止MO连接的收发线程
      if (MTReceiveThread != null) {
        MTReceiveThread.isAvail = false;
      }
      if (MTSendThread != null) {
        MTSendThread.isAvail = false;
      }
      //等待1秒
      PublicFunction.sleep(1000);

      try {
        MTSocketProcess.connectSMSG();
        if (MTSocketProcess.isAvail) {
          MTReceiveThread = new CMPPMTReceiveThread();
          MTReceiveThread.start();

          MTSendThread = new CMPPMTSendThread();
          MTSendThread.start();
        }
      }
      catch (Exception ex) {
        Debug.outError(ex);
      }
    }
  }


  /** 维护MO线程组**/
  public void maintenanceMOProcess(){
    MOSocketProcess = CMPPMOSocketProcess.getInstance();
    if(!MOSocketProcess.isAvail){
      //MOSocketProcess连接无效时,停止MO连接的收发线程
      if(MOReceiveThread != null){
        MOReceiveThread.isAvail = false;
      }
      if(MOSendThread != null){
        MOSendThread.isAvail = false;
      }

      //等待1秒
      PublicFunction.sleep(1000);

      try{
        MOSocketProcess.connectSMSG();
        if(MOSocketProcess.isAvail){
          MOReceiveThread = new CMPPMOReceiveThread();
          MOReceiveThread.start();

          MOSendThread = new CMPPMOSendThread();
          MOSendThread.start();
        }
      }
      catch(Exception ex){
        Debug.outError(ex);
      }
    }
  }

  /**
   * 读取配置文件
   */
  public void loadInitCfg(){
    String cfgFile = "";
    try{
      cfgFile = System.getProperties().getProperty(configProperty);
      cfgFile = cfgFile == null ? "Cmpp.cfg":cfgFile;
      Debug.outDebug("Config file path:"+cfgFile);
      InputStream is = new FileInputStream(cfgFile) ;
      Properties pt = new Properties() ;
      pt.load(is) ;

      /** 更改SOCKET通讯配置参数 **/
      CMPParameter.ServerIp = pt.getProperty("ServerIP", "127.0.0.1");
      System.out.println("ServerIp: \t" + CMPParameter.ServerIp);

      CMPParameter.MTServerPort = Integer.parseInt(pt.getProperty("MTServerPort", "1020"));
      System.out.println("MTServerPort: \t" + CMPParameter.MTServerPort);
      CMPParameter.MOServerPort = Integer.parseInt(pt.getProperty("MOServerPort", "1020"));
      System.out.println("MOServerPort: \t" + CMPParameter.MOServerPort);

      CMPParameter.SP_Id = pt.getProperty("Gateway_ID", "181");
      System.out.println("Gateway_ID: \t" + CMPParameter.Gateway_ID);

      CMPParameter.SP_Id = pt.getProperty("SP_Id", "901234");
      System.out.println("SP_Id: \t" + CMPParameter.SP_Id);

      CMPParameter.SP_Pwd = pt.getProperty("SP_Pwd", "1234");
      System.out.println("SP_Pwd: \t" + CMPParameter.SP_Pwd);

      CMPParameter.ISMGType = Integer.parseInt(pt.getProperty("ISMGType", "2"));
      System.out.println("ISMGType: \t" + CMPParameter.ISMGType);

      CMPParameter.Version = Integer.parseInt(pt.getProperty("Version", "2")) * 16;
      System.out.println("Version: \t" + CMPParameter.Version);

      CMPParameter.ActiveTestTime = Integer.parseInt(pt.getProperty("ActiveTestTime", "30")) * 1000;
      System.out.println("ActiveTestTime: \t" + CMPParameter.ActiveTestTime);
      CMPParameter.MTSpeed = Integer.parseInt(pt.getProperty("MTSpeed", "20"));
      System.out.println("MTSpeed: \t" + CMPParameter.MTSpeed);


      /** 更改数据库连接配置参数 **/
      CMPParameter.DBType = Integer.parseInt(pt.getProperty("DBType", "1"));
      System.out.println("DBType: \t" + CMPParameter.DBType);

      if(CMPParameter.DBType==1) CMPParameter.DBDriver ="oracle.jdbc.driver.OracleDriver";
      if(CMPParameter.DBType==2) CMPParameter.DBDriver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
      if(CMPParameter.DBType==3) CMPParameter.DBDriver ="com.sybase.jdbc.SybDriver";
      System.out.println("DBDriver: \t" + CMPParameter.DBDriver);

      CMPParameter.Connect_URL = pt.getProperty("Connect_URL", "jdbc:microsoft:sqlserver://192.168.0.44:1433;DatabaseName=stock");
      System.out.println("Connect_URL:" + CMPParameter.Connect_URL);

      CMPParameter.DBUser = pt.getProperty("DBUser", "sa");
      Debug.outDebug("DBUser: \t" + CMPParameter.DBUser);

      CMPParameter.DBPass = pt.getProperty("DBPass", "sa");
      Debug.outDebug("DBPass: \t" + "****************");

      CMPParameter.DBPoolSize = Integer.parseInt(pt.getProperty("DBPoolSize", "20"));
      System.out.println("DBPoolSize: \t" + CMPParameter.DBPoolSize);

      CMPParameter.LogFilePath = pt.getProperty("LogFilePath", "sa");
      System.out.println("LogFilePath: \t" + CMPParameter.LogFilePath);

      CMPParameter.IfDebug = Integer.parseInt(pt.getProperty("IfDebug", "0"));
      Debug.outDebug("IfDebug: \t" + CMPParameter.IfDebug);


//      CMPParameter.MTPrefixWord = new String(pt.getProperty("MTPrefixWord", "").getBytes("ISO-8859-1"),"GB2312");
    }
    catch(Exception ex){
      Debug.outDebug("[CMPP] loadInitCfg error :" +ex) ;
    }
  }


  public static void main(String args[]){

    try{
      new HttpServices().start();
      new SubscribeProcess().start();
      new CMPPMaster().start();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    System.out.println("按任意键继续.....");
    try{
       System.in.read();
    }
    catch(Exception ex){
    }

  }
}

⌨️ 快捷键说明

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