📄 mmsender.java
字号:
/**
* <pre>
* 这里说明修改记录,包括修改人,修改时间和修改目的和内容
* ---修改时间---修改人---修改目的和内容----------------------
* 2006/2/22 刘华锋 返回消息-104的时候,再初始化一次socket连接
* 2006/3/1 刘华锋 修改当statuscode为4007时,无法更新数据库的bug(sqlexception为插入的列过大)
* </pre>
*/
package com.sxit.mms.sender;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import org.apache.log4j.Logger;
import com.cmcc.mm7.vasp.conf.MM7Config;
import com.cmcc.mm7.vasp.message.MM7RSRes;
import com.cmcc.mm7.vasp.message.MM7SubmitReq;
import com.cmcc.mm7.vasp.message.MM7SubmitRes;
import com.cmcc.mm7.vasp.service.MM7Sender;
/**
* 1、提交失败的处理,重新发送几次后,删除之并更新发送结果 <br>
* 2、应考虑发送MM的时候,是否开辟新的线程来处理?现在为没有新开辟线程 <br>
* 3、考虑发送的优先级。如果考虑优先级的话,应有一新的线程来处理优先级问题,将优先级高的先插入发送队列 <br>
* 4、考虑定时发送的问题。设立某个字段为定时发送时间,获取数据的时候,判断发送的时间 <br>
*
*
*
* @author HuaFeng
* @version 1.0 (2006-1-13 23:21:35)
*
*/
public class MMSender extends Thread {
/**
* 数据库连接。自己建立
*/
private Connection con;
/**
* 存储mm7submitreq对象的队列
*/
private LinkedList sendqueue;
/**
* mm7api的参数配置对象
*/
MM7Config mm7Config;
/**
* mm7api的发送调用对象
*/
MM7Sender mm7Sender = null;
private final static Logger log = Logger.getLogger(MMSendWorker.class);
/**
* 建立和数据库之间的连接,设计为连接不成功的话,则一直建立连接
*
*/
private void getConnection() {
boolean isconnected = false;
while (!isconnected) {
try {
// getConnection();
Class.forName(CommonProperties.driver);
con = DriverManager.getConnection(CommonProperties.dburl, CommonProperties.dbuser,
CommonProperties.dbpass);
log.info("更新发送日志之数据库连接建立成功!");
isconnected = true;
}
catch (Exception e) {
log.error("更新发送日志之数据库连接建立失败!");
try {
Thread.sleep(CommonProperties.intervalGetWaitingTime);
}
catch (Exception ee) {
log.error(e);
}
log.error(e);
}
}
}
/**
* 发送的mm7配置文件,放在和类的同一目录下
*
*/
public MMSender() {
File file = new File(this.getClass().getResource("MMSender.class").getFile());
String path = file.getParent();
sendqueue = new LinkedList();
mm7Config = new MM7Config(path + "/mm7Config.xml");
mm7Config.setConnConfigName(path + "/ConnConfig.xml");
try {
mm7Sender = new MM7Sender(mm7Config);
log.info("mm7发送对象初始化成功!");
// log.info("-------------------------------");
}
catch (Exception e) {
log.error("mm7Sender初始化失败!" + e);
}
}
public void run() {
MM7SubmitReq submit = null;
int reSendCount = 0;
// int perSecondSendCount = 0;
try {
CommonProperties.init();
log.info("系统参数初始化配置成功!");
getConnection();
MMSendWorker worker = new MMSendWorker(sendqueue);
worker.start();
log.info("获取彩信发送队列线程启动!");
// 发送彩信队列里面的数据
while (true) {
try {
if (sendqueue.isEmpty()) {
synchronized (sendqueue) {
// log.info("要等待阿");
// If queue is empty, wait for tasks to be added
// 这个地方要考虑如果长时间彩信队列是空的话,如何处置这个,长时间的等待是否会造成发送程序僵死
// sendqueue.wait(CommonProperties.maxWaitingTime);
// 每隔CommonProperties.maxWaitingTime时间发送测试彩信,看连接还掉不
sendqueue.wait();
// mm7Sender = new MM7Sender(mm7Config);
// if(log.isDebugEnabled()){
// log.debug("我怀疑是连接掉了造成的发布出去了");
// }
if (log.isDebugEnabled())
log.debug("主线程被唤醒了,重新判断发送队列是否为空!");
// log.info("----------------可以暂时不等了,接下来继续了!");
}
}
else {
long begin = System.currentTimeMillis();// 控制每秒钟发送的个数用
// 从发送队列里面获取需要发送的短信
submit = (MM7SubmitReq) sendqueue.removeFirst();
if (log.isDebugEnabled())
log.debug("获取到了彩信对象,现在时间:" + begin + ",submit.getTo()=" + submit.getTo()+",submit.getContent().getSize()="+submit.getContent().getSize());
MM7RSRes res = mm7Sender.send(submit);
if (log.isDebugEnabled())
log.debug("返回对象:" + res.getClass()+",原因:"+res.getStatusText());
if (res instanceof MM7SubmitRes) {
reSendCount = 0;
MM7SubmitRes submitres = (MM7SubmitRes) res;
log.info("彩信发送成功:" + submit.getTo() + ",MSGID:" + submitres.getMessageID() + ",statusCode="
+ res.getStatusCode());
updateMsgID(con, submitres.getMessageID(), String.valueOf(submitres.getStatusCode()),
submitres.getStatusText(), Long.parseLong(submit.getTransactionID()));
}
else {
log.info("彩信发送失败:" + submit.getTo() + ",状态:" + res.getStatusCode());
// 如果是因为socket不通的话,则再初始化一次MM7Sender
// 2006/02/22新增
if (res.getStatusCode() == -104) {
mm7Sender = new MM7Sender(mm7Config);
log.info("SOCKET不通,重新初始化发送对象!");
}
reSendCount++;
updateMsgID(con, "", String.valueOf(res.getStatusCode()), res.getStatusText(), Long
.parseLong(submit.getTransactionID()));
// 将此发送失败的发送彩信重新插入到发送队列中,插入到最前面
if (reSendCount < CommonProperties.reSendCount) // 重发次数设置,
synchronized (sendqueue) {
// 考虑放在队列的最后,是否可行?
// sendqueue.addLast(submit);
sendqueue.addFirst(submit);
// 不做唤醒的动作,让主线程等到最大时间后再来取,以免唤醒后不能马上处理
// sendqueue.notify();
}
log.info("发送失败,重新发送,发送次数现在为:" + reSendCount);
}
long now = System.currentTimeMillis();
int sendInterval = 1000 / CommonProperties.sendCountPerSecond;
if (now - begin < sendInterval) {
try {
if (log.isDebugEnabled())
log.debug("休眠时间:" + (sendInterval - (now - begin)));
Thread.currentThread().sleep(sendInterval - (now - begin));// 休眠一段时间
}
catch (Exception e) {
log.error("发送间隔休眠异常:" + e);
}
}
// 这里要更新日志表mmsendlog中的response字段
}
}
catch (Exception e) {
log.error("发送异常:" + e);
}
}
}
catch (Exception e) {
log.error("初始化异常:" + e);
}
finally {
try {
MMSendWorker.stop = true;
log.info("关闭获取彩信队列线程!");
if (con != null)
con.close();
log.info("更新日志表数据库连接关闭成功!");
}
catch (Exception e) {
log.error(e);
}
}
}
/**
* 彩信下发并有消息返回时,更新此条彩信的msgid,retcode,retmsg,
*
* @param con
* @param msgid
* 如果下发失败,则msgid为空
* @param retcode
* @param retmsg;
* @param queueid
* @throws SQLException
*/
private void updateMsgID(Connection con, String msgid, String retcode, String retmsg, long queueid) {
String sql = "update mmsendlog set msgid=?,retcode=?,retmsg=?,senddate=sysdate where queueid=?";
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement(sql);
stmt.setString(1, msgid);
stmt.setString(2, retcode);
stmt.setString(3, retmsg);
stmt.setLong(4, queueid);
stmt.executeUpdate();
stmt.close();
}
catch (SQLException e) {
// System.out.println(e);
log.error(e);
}
finally {
try {
if (stmt != null)
stmt.close();
}
catch (SQLException e) {
// System.out.println(e);
log.error(e);
}
}
}
public static void main(String args[]) {
MMSender sender = new MMSender();
sender.start();
log.info("彩信发送进程启动......");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -