?? cmppmaster.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 + -