?? cmppmaster.java
字號:
package com.pansonlu.cmpp;
/**
* <p>Title: CMPPMaster</p>
* <p>Description: 湖南移動短信網(wǎng)關通訊程序
* CMPPMaster是網(wǎng)關通訊程序的主線程,通過她初始化CMPP通訊的參數(shù),啟動并維護收發(fā)線程</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: Sunrise tech ltd.</p>
* @author pansonlu
* @version 1.0
*/
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Properties;
import com.pansonlu.common.util.*;
import com.pansonlu.common.database.*;
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;
public CMPPMaster() {
//加載配置參數(shù)
loadInitCfg();
//設置日志路徑
LogUtil.setLogPath(CMPParameter.LogFilePath);
//初始化連接池
initConnectionPool();
}
public void run(){
while(true){
//判斷數(shù)據(jù)庫是否連接
CMPPDBAccess.getInstance();
//當數(shù)據(jù)庫連接失效時候,需斷開所有的Socket連接,以防止數(shù)據(jù)丟失
//單連接時候
if(CMPParameter.ISMGType == 2){
maintenanceMOProcess();
}
//雙連接時候
else{
maintenanceMOProcess();
maintenanceMTProcess();
}
PublicFunction.sleep(3000);
}
}
/**初始化連接池 **/
public boolean initConnectionPool(){
//初始化數(shù)據(jù)庫連接池:
try{
ConnectionPool.initConnectionPool(
CMPParameter.DBDriver,
CMPParameter.Connect_URL,
CMPParameter.DBUser,
CMPParameter.DBPass,
CMPParameter.DBPoolSize,
20
);
Debug.outInfo("[CMPPMaster]"+PublicFunction.getFormatTime()+" 初始化數(shù)據(jù)庫連接池成功,大小"+CMPParameter.DBPoolSize+",超時間隔20秒");
return true;
}catch(Exception e){
Debug.outInfo("[CMPPMaster]"+PublicFunction.getFormatTime()+" 初始化數(shù)據(jù)庫連接池失敗 " +e.toString());
return false;
}
}
/** 維護MT線程組**/
public void maintenanceMTProcess(){
MTSocketProcess = CMPPMTSocketProcess.getInstance();
if(!MTSocketProcess.isAvail){
//MOSocketProcess連接無效時,停止MO連接的收發(fā)線程
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連接的收發(fā)線程
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通訊配置參數(shù) **/
CMPParameter.ServerIp = pt.getProperty("ServerIP", "127.0.0.1");
Debug.outInfo("ServerIp: \t" + CMPParameter.ServerIp);
CMPParameter.MTServerPort = Integer.parseInt(pt.getProperty("MTServerPort", "1020"));
Debug.outInfo("MTServerPort: \t" + CMPParameter.MTServerPort);
CMPParameter.MOServerPort = Integer.parseInt(pt.getProperty("MOServerPort", "1020"));
Debug.outInfo("MOServerPort: \t" + CMPParameter.MOServerPort);
CMPParameter.SP_Id = pt.getProperty("SP_Id", "901234");
Debug.outInfo("SP_Id: \t" + CMPParameter.SP_Id);
CMPParameter.SP_Pwd = pt.getProperty("SP_Pwd", "1234");
Debug.outInfo("SP_Pwd: \t" + CMPParameter.SP_Pwd);
CMPParameter.ISMGType = Integer.parseInt(pt.getProperty("ISMGType", "2"));
Debug.outInfo("ISMGType: \t" + CMPParameter.ISMGType);
CMPParameter.Version = Integer.parseInt(pt.getProperty("Version", "2")) * 16;
Debug.outInfo("Version: \t" + CMPParameter.Version);
CMPParameter.ActiveTestTime = Integer.parseInt(pt.getProperty("ActiveTestTime", "30")) * 1000;
Debug.outInfo("ActiveTestTime: \t" + CMPParameter.ActiveTestTime);
CMPParameter.MTSpeed = Integer.parseInt(pt.getProperty("MTSpeed", "20"));
Debug.outInfo("MTSpeed: \t" + CMPParameter.MTSpeed);
/** 更改數(shù)據(jù)庫連接配置參數(shù) **/
CMPParameter.DBType = Integer.parseInt(pt.getProperty("DBType", "1"));
Debug.outInfo("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";
Debug.outInfo("DBDriver: \t" + CMPParameter.DBDriver);
CMPParameter.Connect_URL = pt.getProperty("Connect_URL", "jdbc:microsoft:sqlserver://192.168.0.44:1433;DatabaseName=stock");
Debug.outInfo("Connect_URL:" + CMPParameter.Connect_URL);
CMPParameter.DBUser = pt.getProperty("DBUser", "sa");
Debug.outInfo("DBUser: \t" + CMPParameter.DBUser);
CMPParameter.DBPass = pt.getProperty("DBPass", "sa");
Debug.outInfo("DBPass: \t" + CMPParameter.DBPass);
CMPParameter.DBPoolSize = Integer.parseInt(pt.getProperty("DBPoolSize", "20"));
Debug.outInfo("DBPoolSize: \t" + CMPParameter.DBPoolSize);
CMPParameter.LogFilePath = pt.getProperty("LogFilePath", "sa");
Debug.outInfo("LogFilePath: \t" + CMPParameter.LogFilePath);
// 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[]){
new CMPPMaster().start();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -