?? 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>功能: 短信發(fā)送模塊,負(fù)責(zé)從短信隊列中取出短信,發(fā)送到短信網(wǎng)關(guān)</p>
* <p>版權(quán): Copyright (c) 2002</p>
* <p>公司: 深訊信科</p>
* <p>版本: 1.0</p>
* @程序 xuke
* @修改紀(jì)錄
*/
/**
*
* @author: Administrator
*/
public class SMSender {
/** sgip連接參數(shù) */
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";
/** 消息的最大發(fā)送嘗試次數(shù) */
public static int maxRetryTimes = 3;
/** 最大并發(fā)線程數(shù) */
private static int threadThreshold = 1;
/** 重新連接最長等待時間 */
private static int reconnectWaitingTime = 5000;
/** 檢測子線程活動狀態(tài)間隔,無消息時子線程等待時間 */
public static int threadWaitingTime = 5000;
/** 主線程重新連接短信網(wǎng)關(guān)的時間間隔 */
private static int checkThreadInterval = 5000;
/** 一秒鐘內(nèi)發(fā)送的短信數(shù)量 */
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;
/**
* 構(gòu)造函數(shù)
*/
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 命令行參數(shù)數(shù)組
*/
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();
//創(chuàng)建發(fā)送線程組
tg = new ThreadGroup("Sender");
mainBeginTime = new long[threadThreshold];
//發(fā)送短信
while (true) {
try {
//連接到短信網(wǎng)關(guān)
sgipConn = new SgipConnection();
sgipConn.connect(smscHost, smscPort);
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"與短信網(wǎng)關(guān)建立連接");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"與短信網(wǎng)關(guān)建立連接");
sgipConn.bind(loginName,loginPassword,loginType,corpID);
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"連接到短信網(wǎng)關(guān)成功");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"連接到短信網(wǎng)關(guān)成功");
//啟動各條發(fā)送線程
for (int i = 0; i < threadThreshold; i++) {
int j=i+1;
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"啟動第"+j+"發(fā)送線程");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"啟動第"+j+"發(fā)送線程");
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()))+"等待發(fā)送線程運行...");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"等待發(fā)送線程運行...");
}
//主線程休眠,直到所有發(fā)送線程異常中止
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()))+"所有發(fā)送線程都停止了");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"所有發(fā)送線程都停止了");
}
catch (Exception e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
//停止所有發(fā)送線程
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);
}
//斷開與短信網(wǎng)關(guān)的連接
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()))+"斷開與短信網(wǎng)關(guān)的連接");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"斷開與短信網(wǎng)關(guān)的連接");
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后,重新下一輪與短信網(wǎng)關(guān)的連接
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()))+"時間到開始新的發(fā)送過程");
// log.println(df.format(new Timestamp(System.currentTimeMillis()))+"時間到開始新的發(fā)送過程");
}
}
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);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -