?? sendermainapplication.java
字號:
package com.rainbow.mms.gateway;
import java.io.IOException;
import java.util.Timer;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
* 發(fā)送型彩信網(wǎng)關的主線程,負責加載網(wǎng)關配置、啟動發(fā)送者線程、讀取數(shù)據(jù)庫中要發(fā)送的彩信信息
* @author Rainbow MMS Group Leader —— TrWorks
*/
public class SenderMainApplication {
/**
* 網(wǎng)關配置
*/
private ConfigGateWay config = null;
/**
* 發(fā)送者線程組
*/
private Thread[] threads = null;
/**
* 讀取數(shù)據(jù)庫的定時器
*/
private Timer dbAccessTimer = null;
/**
* 統(tǒng)計發(fā)送量用的定時器
*/
private Timer staticTimer = null;
/**
* 日志
*/
private Logger log = Logger.getLogger(SenderMainApplication.class);
/**
* 唯一的Main實體
*/
private static final SenderMainApplication mainApp = new SenderMainApplication();
/**
* 通過工廠方法來獲得唯一的一個Main實體
* @return
*/
public static SenderMainApplication getInstance() {
return mainApp;
}
private SenderMainApplication() {
// 加載Log4j的配置
PropertyConfigurator.configure("conf/log4j.properties");
}
/**
* 啟動網(wǎng)關主線程,該函數(shù)是運行網(wǎng)關必須要調(diào)用的函數(shù) 它負責加載網(wǎng)關配置、啟動發(fā)送者線程、啟動定時讀取數(shù)據(jù)庫的定時器
*/
public void startGateWay() {
log.info("----------------------------------");
log.info("啟動彩信發(fā)送型網(wǎng)關...");
// 加載配置
config = new ConfigGateWay();
if (false == config.loadConfig("conf/GateWaySysConfig.properties")) {
log.error("啟動彩信發(fā)送型網(wǎng)關失敗");
return;
}
// 配置數(shù)據(jù)庫和網(wǎng)關連接
log.info("GateWay配置,SpID: " + config.getGwSpID());
log.info("GateWay配置,ServiceID: " + config.getGwSpServiceID());
log.info("GateWay配置,User: " + config.getGwLinkUser());
log.info("GateWay配置,Passwd: " + config.getGwLinkPasswd());
log.info("GateWay配置,Url: " + config.getGwLinkUrl());
log.info("GateWay配置,WantReport: " + config.getGwWantReport());
log.info("GateWay配置,ContainerClass: " + config.getGwContainClassName());
log.info("GateWay配置,SenderThreadClass: "
+ config.getGwSenderThreadClassName());
// 配置發(fā)送者線程的發(fā)送連接參數(shù)
SenderThread.setSenderConfig(config.getGwSpID(), config
.getGwSpServiceID(), config.getGwLinkUser(), config
.getGwLinkPasswd(), config.getGwLinkUrl());
// 建立發(fā)送者線程的實體
log.debug("建立發(fā)送者線程的實體");
SenderThread threads[] = new SenderThread[config.getGwThreadNum()];
for (int i = 0; i < config.getGwThreadNum(); i++) {
try {
String className = config.getGwSenderThreadClassName();
Class threadClass = Class.forName(className);
threads[i] = (SenderThread)(threadClass.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
log.error("在startGateWay里做類型轉(zhuǎn)換異常");
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
log.error("在startGateWay里做類型轉(zhuǎn)換異常");
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
log.error("沒有找到指定發(fā)送者線程的class文件");
return;
}
}
// 啟動發(fā)送線程
log.debug("啟動發(fā)送線程");
for (int j = 0; j < config.getGwThreadNum(); j++) {
threads[j].start();
}
// 啟動數(shù)據(jù)庫定時訪問線程
int listMaxSize = 10 * (config.getDbGetTimeInterval() / 1000) * config.getGwThreadNum();
SenderDataGetter dateGetter = new SenderDataGetter(
config.getGateWayID(),
listMaxSize,
config.getGwContainClassName(),
(int)(listMaxSize * (0.5)),
(int)(listMaxSize * (0.1)));
dbAccessTimer = new Timer();
dbAccessTimer.schedule(dateGetter, 0, config.getDbGetTimeInterval());
// 啟動統(tǒng)計發(fā)送量用的定時器
SenderStaticInfo staticInfo = SenderStaticInfo.getInstance();
staticTimer = new Timer();
staticTimer.schedule(staticInfo, 0, (60*60*1000));
log.info("啟動彩信發(fā)送型網(wǎng)關成功!");
}
/**
* 停止網(wǎng)關的運行,負責將發(fā)送者線程關閉、讀取數(shù)據(jù)庫的定時器關閉
*
*/
public void stopGateWay() {
log.info("停止彩信發(fā)送型網(wǎng)關");
// 停止對數(shù)據(jù)庫的訪問
if (dbAccessTimer != null) {
dbAccessTimer.cancel();
}
// 停止工作者線程的發(fā)送
SenderThread.setBRun(false);
/*
int threadsNum = threads.length;
while (threadsNum <= 1) {
if (threads[threadsNum - 1].isAlive() == true) {
try {
wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
--threadsNum;
}
}
*/
}
// 單元測試
public static void main(String[] args) throws IOException {
SenderMainApplication mainThread = SenderMainApplication.getInstance();
mainThread.startGateWay();
/*VaspSendTestTemp.test();*/
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -