📄 smsender.java
字号:
package com.tssx.ebiz.sgip;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.*;
/**
* <p>类名: SMSender</p>
* <p>功能: 短信发送模块,负责从短信队列中取出短信,发送到短信网关</p>
* <p>版权: Copyright (c) 2002</p>
* <p>公司: 深讯信科</p>
* <p>版本: 1.0</p>
* @程序 xuke
* @修改纪录
*/
/**
*
* @author: Administrator
*/
public class SMSender {
/** sgip连接参数 */
private static String smscHost = "192.168.8.110";
private static int smscPort = 8801;
private static String loginName = "internet";
private static String loginPassword = "internet";
private static int loginType=1;
private static String corpID="42014";
private static String spNumber="1165";
/** 消息的最大发送尝试次数 */
public static int maxRetryTimes = 3;
/** 最大并发线程数 */
private static int threadThreshold = 1;
/** 重新连接最长等待时间 */
private static int reconnectWaitingTime = 5000;
/** 检测子线程活动状态间隔,无消息时子线程等待时间 */
public static int threadWaitingTime = 5000;
/** 主线程重新连接短信网关的时间间隔 */
private static int checkThreadInterval = 5000;
/** 一秒钟内发送的短信数量 */
public static int countOfSeconds = 3;
/** 连接无消息时候最大等待时间 **/
public static int connectionWaitingTime = 30000;
public static long mainBeginTime[];
public static String driverName = "oracle.jdbc.driver.OracleDriver";
public static String url = "jdbc:oracle:thin:@192.168.8.227:1521:orcl";
public static String user="hnuninet";
public static String password="hnuninet";
// public static PrintWriter log=null;
/**
* 构造函数
*/
private SMSender() {
super();
}
private static void init() throws FileNotFoundException,IOException{
File f=new File("smsender.properties");
InputStream is=new BufferedInputStream(new FileInputStream(f));
//InputStream is = getClass().getResourceAsStream("send.properties");
Properties sgipProps = new Properties();
sgipProps.load(is);
smscHost = sgipProps.getProperty("smscHost");
smscPort = Integer.parseInt(sgipProps.getProperty("smscPort"));
loginName = sgipProps.getProperty("loginName");
loginPassword = sgipProps.getProperty("loginPassword");
corpID = sgipProps.getProperty("corpID");
spNumber = sgipProps.getProperty("spNumber");
//maxRetryTimes = Integer.parseInt(sgipProps.getProperty("maxRetryTimes"));
//threadThreshold = Integer.parseInt(sgipProps.getProperty("threadThreshold"));
//reconnectWaitingTime = Integer.parseInt(sgipProps.getProperty("reconnectWaitingTime"));
//threadWaitingTime = Integer.parseInt(sgipProps.getProperty("threadWaitingTime"));
//checkThreadInterval = Integer.parseInt(sgipProps.getProperty("checkThreadInterval"));
//countOfSeconds = Integer.parseInt(sgipProps.getProperty("countOfSeconds"));
//connectionWaitingTime = Integer.parseInt(sgipProps.getProperty("connectionWaitingTime"));
driverName = sgipProps.getProperty("driverName");
url = sgipProps.getProperty("url");
user = sgipProps.getProperty("user");
password = sgipProps.getProperty("password");
is.close();
// log = new PrintWriter(new FileWriter("smsender.log", true), true);
}
/**
* 程序运行
* @param args 命令行参数数组
*/
public static void main(java.lang.String[] args) {
SgipConnection sgipConn = null;
ThreadGroup tg = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒:");
try {
init();
//创建发送线程组
tg = new ThreadGroup("Sender");
mainBeginTime = new long[threadThreshold];
//发送短信
while (true) {
try {
//连接到短信网关
sgipConn = new SgipConnection();
sgipConn.connect(smscHost, smscPort);
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"与短信网关建立连接");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"与短信网关建立连接");
sgipConn.bind(loginName,loginPassword,loginType,corpID);
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"连接到短信网关成功");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"连接到短信网关成功");
//启动各条发送线程
for (int i = 0; i < threadThreshold; i++) {
int j=i+1;
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"启动第"+j+"发送线程");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"启动第"+j+"发送线程");
mainBeginTime[i] = System.currentTimeMillis();
SMSenderThread newSender = new SMSenderThread(sgipConn, i, maxRetryTimes, threadWaitingTime/threadThreshold);
Thread newThread = new Thread(tg, newSender, "Sender Thread " + i);
newThread.start();
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"等待发送线程运行...");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"等待发送线程运行...");
}
//主线程休眠,直到所有发送线程异常中止
while (tg.activeCount() > 0) {
try {
//System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"主线程休眠");
Thread.currentThread().sleep(SMSender.checkThreadInterval);
}
catch (InterruptedException e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
}
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"所有发送线程都停止了");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"所有发送线程都停止了");
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
//停止所有发送线程
try {
tg.interrupt();
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
//断开与短信网关的连接
try {
sgipConn.unBind(corpID);
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"断开与短信网关的连接");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"断开与短信网关的连接");
try {
sgipConn.close();
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
//主线程等待WAITING_TIME后,重新下一轮与短信网关的连接
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"等待"+SMSender.reconnectWaitingTime/1000+"秒");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"等待"+SMSender.reconnectWaitingTime/1000+"秒");
try {
Thread.sleep(SMSender.reconnectWaitingTime);
}
catch (InterruptedException e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"时间到开始新的发送过程");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"时间到开始新的发送过程");
}
}
catch (FileNotFoundException e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"没有找到属性文件");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"没有找到属性文件");
}
catch (IOException e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"读属性文件出错");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"读属性文件出错");
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
finally {
try {
if (sgipConn != null) {
sgipConn.close();
}
// if(log!=null){
// log.close();
// }
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -