📄 cmppsender.java
字号:
package com.fetion.cmpp.server.sender;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.fetion.cmpp.common.Message;
import com.huawei.smproxy.comm.cmpp.message.CMPPSubmitMessage;
/**
* 发送短信类
*
*
*
*
*/
public class CmppSender {
/** 操作日志对象 */
private static Log logger = LogFactory.getLog(CmppSender.class);
/**
* 把message包装成CMPPSubmitMessage信息
* @param message
* @return
* @throws IllegalArgumentException
* @throws UnsupportedEncodingException
*/
private static CMPPSubmitMessage getCMPPSubmitMessage(Message message)
throws IllegalArgumentException, UnsupportedEncodingException {
CMPPSubmitMessage msg = null;
if (message != null) {
String sourceTel = message.getSourceTelNumber();
String[] rcvMobile = message.getDestinationTelNumber();
String content = message.getContent();
msg = new CMPPSubmitMessage(CMPPMessageConstants.pk_Total, CMPPMessageConstants.pk_Number, CMPPMessageConstants.registered_Delivery, CMPPMessageConstants.msg_Level, CMPPMessageConstants.service_Id, CMPPMessageConstants.fee_UserType, sourceTel, CMPPMessageConstants.tp_Pid,
CMPPMessageConstants.tp_Udhi, CMPPMessageConstants.msg_Fmt, CMPPMessageConstants.msg_Src, CMPPMessageConstants.fee_Type, CMPPMessageConstants.fee_Code, CMPPMessageConstants.valid_Time, CMPPMessageConstants.at_Time, sourceTel, rcvMobile,
content.getBytes("UnicodeBigUnmarked"), CMPPMessageConstants.reserve);
}
return msg;
}
/**
* 根据message发送信息
* @param message
* @return
* @throws CMPPProcessException
*/
public static boolean send(Message message) throws CMPPProcessException {
boolean flag = false;
// 发送次数
int sendcount = 1;
int sendinterval = 1;
CMPPSubmitMessage msg = null;
try {
msg = getCMPPSubmitMessage(message);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (msg == null)
return flag;
else {
try {
for (int i = 0; i < sendcount; i++) {
logger.info("ATTEMP SEND CMPP");
if (WebSMSender.getInstance().send(msg)) {
logger.info("SEND CMPP SUCCUESS");
} else {
logger.info("SEND CMPP FAIL");
// 处理异常情况,如果长连接建立失败
while (WebSMSender.getInstance().getConnState() != null) {
// 10秒尝试一次
Thread.sleep(10000);
logger.info("ATTEMP GET CONNECTION");
if (WebSMSender.getInstance().getConnState() == null) {
logger.info("ATTEMP RE_SEND CMPP");
send(message);
}
}
}
try {
Thread.sleep(1000 * sendinterval);
} catch (Exception exception) {
throw new CMPPProcessException(exception);
}
flag = true;
}
} catch (Exception e) {
throw new CMPPProcessException(e);
}
}
return flag;
}
/*
public static void main(String[] args) {
DBToolImp dbTool = new DBToolImp();
try {
dbTool.openDB();
Map<Long, Message> newMessages = dbTool.getAllNewMessages();
dbTool.closeDB();
List<Message> historys = new ArrayList(newMessages.values());
for(Message m : historys){
System.out.println("未读的信息有: "+ m);
System.out.println("status: "+CmppSender.send(m));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -