📄 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;
/**
* 发送型彩信网关的主线程,负责加载网关配置、启动发送者线程、读取数据库中要发送的彩信信息
* @author Rainbow MMS Group Leader —— TrWorks
*/
public class SenderMainApplication {
/**
* 网关配置
*/
private ConfigGateWay config = null;
/**
* 发送者线程组
*/
private Thread[] threads = null;
/**
* 读取数据库的定时器
*/
private Timer dbAccessTimer = null;
/**
* 统计发送量用的定时器
*/
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");
}
/**
* 启动网关主线程,该函数是运行网关必须要调用的函数 它负责加载网关配置、启动发送者线程、启动定时读取数据库的定时器
*/
public void startGateWay() {
log.info("----------------------------------");
log.info("启动彩信发送型网关...");
// 加载配置
config = new ConfigGateWay();
if (false == config.loadConfig("conf/GateWaySysConfig.properties")) {
log.error("启动彩信发送型网关失败");
return;
}
// 配置数据库和网关连接
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());
// 配置发送者线程的发送连接参数
SenderThread.setSenderConfig(config.getGwSpID(), config
.getGwSpServiceID(), config.getGwLinkUser(), config
.getGwLinkPasswd(), config.getGwLinkUrl());
// 建立发送者线程的实体
log.debug("建立发送者线程的实体");
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里做类型转换异常");
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
log.error("在startGateWay里做类型转换异常");
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
log.error("没有找到指定发送者线程的class文件");
return;
}
}
// 启动发送线程
log.debug("启动发送线程");
for (int j = 0; j < config.getGwThreadNum(); j++) {
threads[j].start();
}
// 启动数据库定时访问线程
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());
// 启动统计发送量用的定时器
SenderStaticInfo staticInfo = SenderStaticInfo.getInstance();
staticTimer = new Timer();
staticTimer.schedule(staticInfo, 0, (60*60*1000));
log.info("启动彩信发送型网关成功!");
}
/**
* 停止网关的运行,负责将发送者线程关闭、读取数据库的定时器关闭
*
*/
public void stopGateWay() {
log.info("停止彩信发送型网关");
// 停止对数据库的访问
if (dbAccessTimer != null) {
dbAccessTimer.cancel();
}
// 停止工作者线程的发送
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 + -