110,533
社区成员
发帖
与我相关
我的任务
分享
partial class MSGSendService : ServiceBase {
/// <summary>
/// 检查间隔时间(单位毫秒)
/// </summary>
private int ExamineDelayCount {
get {
int time = Config.ExamineDelayCount;
int count = time * 60 * 1000;
return count;
}
}
/// <summary>
/// 触发检查的时间控件
/// </summary>
private System.Timers.Timer ExamineTimer;
// 构造函数
public MSGSendService() {
InitializeComponent();
this.ExamineTimer = new System.Timers.Timer(this.ExamineDelayCount);
this.ExamineTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.ExamineTimer_Elapsed);
}
/// <summary>
/// 服务启动
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args) {
try {
this.SendThreadStart();
} finally {
LogHelper.WriteLog("***************************** Start MSGSend! *****************************");
}
}
/// <summary>
/// 服务停止
/// </summary>
protected override void OnStop() {
try {
this.SendThreadClose();
} finally {
LogHelper.WriteLog("***************************** Stop MSGSend! *****************************");
}
}
#region 按设定间隔时间检查线程是否处于开启状态
private void ExamineTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
//
}
#endregion
#region 定时发送
// 短信服务处理对象
MessageService msgService = new MessageService();
/// <summary>
/// 发送总数量
/// </summary>
public int SendCount { get; private set; }
/// <summary>
/// 下发成功数量
/// </summary>
public int SuccessCount { get; private set; }
/// <summary>
/// 下发错误数量
/// </summary>
public int ErrorCount { get; private set; }
/// <summary>
/// 发送线程
/// </summary>
private Thread SendThread = null;
/// <summary>
/// 是否处于激活状态
/// </summary>
private bool IsAlive { get; set; }
/// <summary>
/// 初始化发送短信线程
/// </summary>
public void SendThread_Init() {
if (this.SendThread == null || !this.SendThread.IsAlive) {
this.IsAlive = true;
ThreadStart starter = delegate { SendMsg(); };
this.SendThread = new Thread(starter);
this.SendThread.IsBackground = true;
this.SendThread.Priority = ThreadPriority.AboveNormal;
this.SendThread.SetApartmentState(ApartmentState.MTA);
this.SendThread.Start();
}
}
/// <summary>
/// 终止发送短信线程
/// </summary>
public void SendThread_Abort() {
lock (this) {
if (this.SendThread != null) {
try {
this.IsAlive = false;
this.SendThread.Abort();
} catch {
this.SendThread = null;
} finally {
GC.Collect();
}
}
// 关闭监听时钟
this.ExamineTimer.Enabled = false;
Thread.Sleep(100);
}
}
/// <summary>
/// 开启发送短信线程
/// </summary>
public void SendThreadStart() {
lock (this) {
if (this.SendThread == null || !this.SendThread.IsAlive) {
this.SendThread_Init();
}
// 激活
this.IsAlive = true;
// 激活监听时钟
//this.ExamineTimer.Enabled = true;
}
}
/// <summary>
/// 关闭发送短信线程
/// </summary>
public void SendThreadClose() {
this.IsAlive = false;
// 关闭监听时钟
this.ExamineTimer.Enabled = false;
}
/// <summary>
/// 重启发送短信线程
/// </summary>
public void SendThreadRestart() {
this.SendThread_Abort();
this.SendThreadStart();
}
/// <summary>
/// 获取待发短信
/// </summary>
private void SendMsg() {
if (null == this.msgService) {
return;
}
//
this.msgService.Url = Config.MessageServiceUrl;
while (true) {
if (this.IsAlive) {
try {
//获取待发对象详细发送清单并进行发送
IList<HX.MIAP.Model.SMSSendDetails> committedList_TD = BLL.BLLSMSSendDetails.GetCommitted(Config.SendPageSize, Config.MinAfter, Config.IsGetSendReport);
for (int i = 0; i < committedList_TD.Count; i++) {
HX.MIAP.Model.SMSSendDetails item = committedList_TD[i];
if (null != item && this.IsAlive) {
if (Config.SendAsync) {
#region 进行异步发送
this.msgService.BeginSendSMS(Config.SystemID, Config.MsgSendSign,
Config.MsgSendSign, new string[] { item.MOB },
item.MsgContent, item.MsgContent,
new AsyncCallback(this.SendMsgCallback), item.ID);
#endregion
} else {
#region 同步发送
string msgId = string.Empty;
string msgError = string.Empty;
bool result = this.msgService.SendSMS(Config.SystemID, Config.MsgSendSign,
Config.MsgSendSign, new string[] { item.MOB },
item.MsgContent, item.MsgContent,
out msgId, out msgError);
// 累计下发数量
this.SendCount++;
if (Config.IsGetSendReport) {
// 更新短信状态
int updateResult = BLL.BLLSMSSendDetails.UpdateState(item.ID, result ? "已发送" : msgError);
}
// 记录错误日志
if (result) {
// 累计下发成功数量
this.SuccessCount++;
LogHelper.WriteLog(string.Format("短信下发成功:手机号码【{0}】内容【{1}】", item.MOB, item.MsgContent));
} else {
LogHelper.WriteLog(string.Format("短信下发时发生错误:错误标识:{0},错误信息{1}", msgId, msgError));
// 累计下发失败数量
this.ErrorCount++;
}
#endregion
}
}
}
committedList_TD = null;
} catch (OutOfMemoryException omex) {
// 发生内存溢出时强制垃圾回收
GC.Collect();
LogHelper.WriteLog("短信下发时发生内存溢出异常!内存已回收!");
} catch (Exception ex) {
LogHelper.WriteLog(string.Format("短信下发时发生异常:{0}", ex.Message));
}
}
// 暂停
Thread.Sleep(Config.SendInterval);
}
}
/// <summary>
/// 异步回调事件(完成短信下发)
/// </summary>
/// <param name="asyncResult"></param>
private void SendMsgCallback(IAsyncResult asyncResult) {
if (!asyncResult.IsCompleted) {
asyncResult.AsyncWaitHandle.WaitOne();
}
if (null != msgService && null != asyncResult) {
string msgId = string.Empty;
string msgError = string.Empty;
int smsdetailsID = Convert.ToInt32(asyncResult.AsyncState);
try {
bool result = msgService.EndSendSMS(asyncResult, out msgId, out msgError);
// 累计下发数量
this.SendCount++;
if (Config.IsGetSendReport) {
// 更新短信状态
int updateResult = BLL.BLLSMSSendDetails.UpdateState(smsdetailsID, result ? "已发送" : msgError);
}
// 记录错误日志
if (result) {
// 累计下发成功数量
this.SuccessCount++;
LogHelper.WriteLog("异步短信下发成功 反馈信息:" + msgError);
} else {
// 累计下发失败数量
this.ErrorCount++;
LogHelper.WriteLog(string.Format("短信下发时发生错误:错误标识:{0},错误信息{1}", msgId, msgError));
}
} catch (OutOfMemoryException omex) {
// 发生内存溢出时强制垃圾回收
GC.Collect();
LogHelper.WriteLog("短信下发时发生内存溢出异常!内存已回收!");
} catch (Exception ex) {
LogHelper.WriteLog(string.Format("短信下发时发生异常:{0}", ex.Message));
}
}
}
#endregion
}