java 长短信_java长连接发短信-smppapi-0.3.9

该代码片段展示了如何使用Java通过SMPP协议建立长连接发送短信。类SMPPUtil包含了打开、关闭连接,发送心跳包以及发送短信的逻辑。它使用了PropertiesUtil来读取配置参数,并通过线程处理心跳包的发送和接收。
摘要由CSDN通过智能技术生成

packagecn.richinfo.cmail.vasp.common.util;importie.omk.smpp.Address;importie.omk.smpp.Connection;importie.omk.smpp.message.BindResp;importie.omk.smpp.message.EnquireLink;importie.omk.smpp.message.EnquireLinkResp;importie.omk.smpp.message.SMPPPacket;importie.omk.smpp.message.SMPPResponse;importie.omk.smpp.message.SubmitSM;importie.omk.smpp.message.SubmitSMResp;importie.omk.smpp.util.EncodingFactory;importorg.apache.commons.lang.StringUtils;importorg.apache.log4j.Logger;importcn.richinfo.cmail.common.config.PropertiesUtil;/*** @todo 长连接发短信

* @date 2017年8月15日

*@authorlinying*/

public classSMPPUtil {private Logger logger = Logger.getLogger(SMPPUtil.class);private boolean connected = false; //是否已连接

private boolean received = false; //是否接收心跳包

private Connection conn = null;//ESME到SMSC的连接

privateSMPPUtil() {

}public static finalSMPPUtil getInstance() {returnSMPPUtilHolder.SMPP_UTIL;

}/*** @todo 打开链接

*@throwsException*/

private void openLink() throwsException {

logger.info("open smsc service...");

String host= PropertiesUtil.getProperty("zte.notice.smsc.host");int port = PropertiesUtil.getPropertyInt("zte.notice.smsc.port", 0);

String systemId= PropertiesUtil.getProperty("zte.notice.smsc.systemId");

String password= PropertiesUtil.getProperty("zte.notice.smsc.password");

String systemType= PropertiesUtil.getProperty("zte.notice.smsc.systemType");

logger.info(String.format("openLink parameter | host=%s | port=%s | systemId=%s | password=%s | systemType=%s", host, port, systemId, password, systemType));if (StringUtils.isEmpty(host) || StringUtils.isEmpty(systemId) || StringUtils.isEmpty(password) || StringUtils.isEmpty(systemType) || port == 0) {

logger.error("parameter is empty");return;

}

conn= newConnection(host, port);

conn.autoAckLink(true);

conn.autoAckMessages(true);

BindResp resp=conn.bind(Connection.TRANSMITTER, systemId, password, systemType);if (null != resp && resp.getCommandStatus() == 0) {

connected= true;//new Thread(new KeepHeartbeat()).start();//发送心跳

new Thread(new ReceivePackets()).start(); //接收心跳

}

}/***

* @todo 发送心跳

* @date 2017年8月15日

*@authorlinying*/

class KeepHeartbeat implementsRunnable {private long delay = PropertiesUtil.getPropertyLong("zte.notice.smsc.keep.delay");public voidrun() {while(connected) {try{

Thread.sleep(delay);//心跳延时

EnquireLinkResp resp =conn.enquireLink();if (null != resp && resp.getCommandStatus() == 0) {

logger.info("[system request packet ok]");

}else{

logger.info("[system request packet err]");

}

}catch(Exception e) {

logger.error(e);

closeLink();

}

}

}

}/***

* @todo 接收心跳

* @date 2017年8月15日

*@authorlinying*/

class ReceivePackets implementsRunnable {public voidrun() {while(connected) {try{if(received) {

SMPPPacket packet=conn.readNextPacket();if (null != packet && packet.getCommandStatus() == 0) {

logger.info("[smsc request packet ok]");

EnquireLink link= newEnquireLink();

link.setSequenceNum(packet.getSequenceNum());

conn.ackEnquireLink(link);

}else{

logger.info("[smsc request packet err]");

}

}

logger.info("[smsc request packet stop]");

Thread.sleep(10000);

}catch(Exception e) {

logger.error(e);

closeLink();

}

}

}

}/*** @todo 关闭链接

*@returnvoid*/

private voidcloseLink() {

logger.info("close smsc service...");

connected= false;

received= false;try{if (conn != null &&conn.isBound()) {

conn.unbind();

}

conn= null;

}catch(Exception e) {

logger.error(e);

}

}/*** @todo 发送短信

*@paramaddress

*@paramtext

*@returnString

*@throwsException*/

public String sendSms(String address, String text) throwsException {int ton = PropertiesUtil.getPropertyInt("zte.notice.sms.ton", 0);int npi = PropertiesUtil.getPropertyInt("zte.notice.sms.npi", 0);

String source= PropertiesUtil.getProperty("zte.notice.sms.sourceAddress");int encode = PropertiesUtil.getPropertyInt("zte.notice.sms.encoding", 8);int count = 1;returnsendSms(ton, npi, source, address, text, encode, count);

}private String sendSms(int ton, int npi, String source, String address, String text, int encode, int count) throwsException {//发送短信中,停止接收心跳

received = false;//没有绑定

if (!connected) {

openLink();

}

logger.info("connected is true");

SubmitSM sm=(SubmitSM) conn.newInstance(SMPPPacket.SUBMIT_SM);if(StringUtils.isNotEmpty(source)) {

sm.setSource(newAddress(ton, npi, source));

}

sm.setDestination(new Address(0, 0, address));

sm.setMessageText(text);

sm.setMessageEncoding(EncodingFactory.getInstance().getEncoding(encode));

SMPPResponse response=(SubmitSMResp) conn.sendRequest(sm);

received= true;//发送失败,递归重试3次

if (response == null || response.getCommandStatus() != 0) {

Thread.sleep(300);

logger.error("send failed is retrying " +count);

count++;if (count > 3) {return "send failed";

}

sendSms(ton, npi, source, address, text, encode, count);

}

logger.info("send success");return "send success";

}/*** @todo SMPPUtil单例

* @date 2017年8月15日

*@authorlinying*/

private static classSMPPUtilHolder {private static final SMPPUtil SMPP_UTIL = newSMPPUtil();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值