📄 smgpsendthread.java~1~
字号:
package com.pansonlu.smgp;/** * <p>Title: SMGPSendThread</p> * <p>Description: 湖南电信短信网关通讯程序(实现对短信网关的连接,短信的收发)</p> * <p>Description: SMGPSendThread类用于建立与ISMG的连接</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: sunun Ltd.</p> * @author pansonlu * @version 1.0 */import java.io.*;import java.net.*;import com.pansonlu.common.util.*;public class SMGPSendThread extends Thread{ /** 该线程运行标志 true 在运行,false停止 **/ public static boolean isAvail = false; private static DataInputStream inputStream; private static DataOutputStream outputStream; /** 初始化构造器 * 使用SMGPSocketProcess唯一的实例的输入流和输出流**/ public SMGPSendThread() { outputStream = SMGPSocketProcess.getInstance().outputStream; isAvail = true; } public void run(){ Debug.outInfo("[SMGPMaster]"+PublicFunction.getFormatTime()+" MT线程("+SMGParameter.ServerPort+")->发送线程启动 ..."); /** 最后一次链路测试时间 **/ long LastActiveTime = System.currentTimeMillis(); try{ while (isAvail && SMGPSocketProcess.getInstance().isAvail) { SMGPDBAccess.getInstance().fectchMTDataToBuffer(); int sendFlag = sendDataFromBuffer(); //System.out.println("sendDataFromBuffer sendFlag = "+sendFlag); if (sendFlag == 1) { //如果缓冲中无发送数据 //如果在10秒内无数据交换,则发送链路测试包 if((System.currentTimeMillis() - LastActiveTime) > (SMGParameter.ActiveTestTime - 5000)) //毫秒发一次测试包 { SMGP activeMsg = new SMGP(SMGP.SMGP_ACTIVE_TEST); PublicFunction.send(outputStream,activeMsg.pack()); LastActiveTime = System.currentTimeMillis(); } PublicFunction.sleep(500); //适当延时。以减轻SMGP通讯服务器端的压力,可以去掉 } else { //当成功发送数据成功后,更新上次链路测试时间 LastActiveTime = System.currentTimeMillis(); } } //end while } catch(Exception ex){ ex.printStackTrace(); this.destory(ex); } } /** * 从发送缓冲队列中取数据发送 * @return * 0 成功发送 * 1无数据, * 2有数据,但发送过程中出现异常 */ private int sendDataFromBuffer() throws Exception{ boolean haveData = false; //是否有数据 long start = 0; try { SMGP submitMsg ; //要发送给SP的submit message if (SMGPDBAccess.getInstance().vctMTData.size() > 0) { start = System.currentTimeMillis(); haveData = true; } else { return 1; } //当发送缓冲队列中有数据时,发送MT信息 while (SMGPDBAccess.getInstance().vctMTData.size() > 0) { //取缓冲中的第一条信息 synchronized (SMGPDBAccess.getInstance().vctMTData) { submitMsg = (SMGP) SMGPDBAccess.getInstance().vctMTData.elementAt(0); } //判断是否发送成功,成功返回0,失败返回2 PublicFunction.send(outputStream,submitMsg.pack()); synchronized (SMGPDBAccess.getInstance().vctMTData) { SMGPDBAccess.getInstance().vctMTData.removeElementAt(0); } int iSleep = (1000 / SMGParameter.MTSpeed); PublicFunction.sleep(iSleep); } // end-while } catch (Exception ex) { throw new Exception("PublicFunction.Send() in Thread Send Error "+ex.toString()); } return 0; } /** 销毁线程 **/ public void destory(Exception ex){ if(ex !=null){ ex.printStackTrace(); Debug.outInfo("[SMGP]MO连接(发)线程异常停止,异常原因:" + ex.getMessage()); } else{ Debug.outInfo("[SMGP]MO连接(发)线程异常停止"); } SMGPSocketProcess.getInstance().disclose(); this.isAvail = false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -