📄 smsenderthread.java
字号:
package com.tssx.ebiz.sgip;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
/**
* Insert the type's description here.
* Creation date: (1/7/2001 5:03:48 PM)
* @author: Administrator
*/
public class SMSenderThread implements Runnable {
/** 到短信平台的连接 */
private SgipConnection m_sgipConn;
/** 线程编号 */
private int m_id;
/** 消息的最大发送尝试次数 */
private int maxRetryTimes = 3;
/** 线程在无消息发送情况下,最长等待重试间隔 */
private int threadWaitingTime = 30000;
/**
* Insert the method's description here.
* Creation date: (2001-2-8 10:03:50)
* @return java.sql.Connection
*/
protected static Connection getConnection() throws FileNotFoundException,IOException,Exception {
String driverName = SMSender.driverName;
String url = SMSender.url;
String user=SMSender.user;
String password=SMSender.password;
Connection con = null;
//连接数据库
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url,user,password);
return con;
}
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public void run() {
Connection m_dbConn=null;
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒:");
//定时发送
/**
SimpleDateFormat df = new SimpleDateFormat("yyMMddhhmmss032+");
java.util.Calendar calendar1=java.util.Calendar.getInstance();
java.util.Calendar calendar2=java.util.Calendar.getInstance();
calendar1.add(calendar1.DATE,1);
calendar2.add(calendar2.MINUTE,10);
String expireTime=df.format(calendar1.getTime());
String scheduleTime=df.format(calendar2.getTime());
*/
String expireTime="";
String scheduleTime="";
SMQueue queue = null;
SMMessage message = null;
int count = 0;
long beginTime = 0;
int i=0;
int num=0;
try {
m_dbConn = getConnection();
queue = new SMQueue(m_dbConn);
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"线程 开始处理数据");
// SMSender.log.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"线程 开始处理数据");
while (true) {
if (count == 0){
beginTime = System.currentTimeMillis();
}
//获取下一条要发送的信息
int bj=0;
while (true) {
message = queue.getMessage();
if (message != null) {
SMSender.mainBeginTime[m_id-1] = System.currentTimeMillis();
bj=0;
break;
}
long mainCurTime = System.currentTimeMillis();
if (mainCurTime - SMSender.mainBeginTime[m_id-1] >= SMSender.connectionWaitingTime) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"没有新的消息退出");
// SMSender.log.println(df.format(new Timestamp(System.currentTimeMillis()))+"没有新的消息退出");
bj=1;
break;
}
//休眠一段时间,等待信息到达
try {
//System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"休眠5秒");
Thread.currentThread().sleep(threadWaitingTime);
} catch (InterruptedException e) {
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
// SMSender.log.println(df.format(new Timestamp(System.currentTimeMillis()))+e);
}
}
if(bj==1) break;
//消息的发送次数是否超过了最大发送次数
if (message.getSendCount() >= maxRetryTimes) {
queue.remove(message.getQueueID());
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"线程 发送失败删除数据");
// SMSender.log.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"线程 发送失败删除数据");
continue;
}
Exception sendException = null;
String resp="";
SMSSubmitThread smssubmitthread=null;
Thread newThread=null;
try {
switch(message.submitMulti){
case 0:
resp=m_sgipConn.submit(message.getSourceAddr(),message.getDestinationAddr(),
message.getContent(),message.getSpNumber(),message.getCorpID(),message.getServiceType(),
message.getFeeType(),message.getFeeValue(),message.getGivenValue(),
message.getMorelatetoMTFlag(),message.getReportFlag(),15,"","");
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"条线程 成功单发文本");
// SMSender.log.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"条线程 成功单发文本");
smssubmitthread=new SMSSubmitThread(resp,message.getSourceAddr(),message.getDestinationAddr(),
message.getContent(),message.getSpNumber(),message.getCorpID(),message.getServiceType(),
message.getFeeType(),message.getFeeValue(),message.getGivenValue(),
message.getMorelatetoMTFlag(),message.getReportFlag(),15,"","");
newThread = new Thread(smssubmitthread);
newThread.start();
break;
case 1:
resp=m_sgipConn.submitMulti(message.getSourceAddr(),message.getDestAddr(),
message.getContent(),message.getSpNumber(),message.getCorpID(),message.getServiceType(),
message.getFeeType(),message.getFeeValue(),message.getGivenValue(),
message.getMorelatetoMTFlag(),message.getReportFlag(),15,"","");
System.out.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"条线程 成功群发文本");
// SMSender.log.println(df.format(new Timestamp(System.currentTimeMillis()))+"第"+m_id+"条线程 成功群发文本");
smssubmitthread=new SMSSubmitThread(resp,message.getSourceAddr(),message.getDestAddr(),
message.getContent(),message.getSpNumber(),message.getCorpID(),message.getServiceType(),
message.getFeeType(),message.getFeeValue(),message.getGivenValue(),
message.getMorelatetoMTFlag(),message.getReportFlag(),15,"","");
newThread = new Thread(smssubmitthread);
newThread.start();
break;
case 2:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -