📄 synctransmitterexample30.java
字号:
package com.bci.cmpp.example;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
import com.bci.cmpp.AlreadyBoundException;
import com.bci.cmpp.BadCommandIDException;
import com.bci.cmpp.CMPPConstants;
import com.bci.cmpp.Connection;
import com.bci.cmpp.UnsupportedOperationException;
import com.bci.cmpp.message.*;
import com.bci.cmpp.util.*;
public class SyncTransmitterExample30 extends CMPPAPIExample {
private Log logger = LogFactory.getLog(SyncTransmitterExample30.class);
public SyncTransmitterExample30() {
}
protected boolean reConnect(int type, boolean asyc) {
try {
Thread.sleep(1000);
myConnection = new Connection(this.hostName, this.port, asyc);
myConnection.setVersion(CMPPConstants.CMPP_VERSION_3);
logger.info("Binding to the SMSG..");
CMPPConnectResp30 resp = (CMPPConnectResp30)myConnection.bind(type, this.icpId,
this.password);
if (resp != null ){
logger.info("The SMSG version is "+resp.getVersion());
if(resp.getCommandStatus() != 0)
logger.info("SMSG bind failed.");
}
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (InterruptedException e) {
e.printStackTrace();
return false;
} catch (InvalidParameterValueException e) {
e.printStackTrace();
return false;
} catch (AlreadyBoundException e) {
e.printStackTrace();
return false;
} catch (CMPPProtocolException e) {
e.printStackTrace();
return false;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
logger.info("Bind successful.");
return true;
}
public void execute() {
// 发送短信 使用同步模式
while (!isConnected())
reConnect(Connection.TRANSMITTER, false);
// Submit a simple message
CMPPSubmit30 sm = null;
try {
sm = (CMPPSubmit30) myConnection
.newInstance(CMPPConstants.CMPP_SUBMIT);
} catch (BadCommandIDException e1) {
e1.printStackTrace();
}
// sm.setSequenceNum(0);
sm.setSpCode(this.spCode);
sm.setServiceId(this.serviceId);
sm.setSourceAddr(this.sourceAddr);
sm.setFeeTerminalId(getProperty("feeTerm"));
sm.setDestTerminalId(getProperty("destTerm"));
sm.setFeeUserType(3);
sm.setRegistered(1);
sm.setFeeType(getProperty("feeType"));
sm.setFeeCode(getProperty("feeCode"));
sm.setLinkId(getProperty("linkId"));
// sm.setSequenceNum();
try {
sm.setEncoding(UCS2Encoding.getInstance());
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
sm.setMsgCoding(8);
sm.setMsgText("CMCCTEST");
// sm.setMessage("注意:关于短信群发的问题,若SP对于群发消息不要求状态报告的回送时".getBytes());
// logger.info(encodeHex(sm.getBytes()));
logger.info("submit message:" + sm.getFeeType() + "|" + sm.getFeeCode()
+ "|" + sm.getDestTerminalId()[0] + "|" + sm.getMsgText());
try {
CMPPSubmitResp30 smr = (CMPPSubmitResp30) myConnection.sendRequest(sm);
if (smr == null)
logger.info("Submitted message returned null.");
else if (smr.getCommandStatus() == 0)
logger.info("Submitted message ID: " + smr.getMsgIdStr());
else
logger.info("Submit message returned error:"
+ smr.getCommandStatus());
} catch (SocketTimeoutException e1) {
e1.printStackTrace();
} catch (AlreadyBoundException e1) {
e1.printStackTrace();
} catch (CMPPProtocolException e1) {
e1.printStackTrace();
} catch (UnsupportedOperationException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
while (!isConnected())
reConnect(Connection.TRANSMITTER, false); // 每30秒发一次ActiveTest
try {
Thread.sleep(1000 * 30);
CMPPActiveTest at = (CMPPActiveTest) myConnection
.newInstance(CMPPConstants.CMPP_ACTIVE_TEST);
CMPPActiveTestResp atr = (CMPPActiveTestResp) myConnection
.sendRequest(at);
if (atr != null && atr.getCommandStatus() == 0)
logger.info("active test seq:" + atr.getSequenceNum());
} catch (BadCommandIDException e) {
e.printStackTrace();
disConnect();
} catch (SocketTimeoutException e) {
e.printStackTrace();
disConnect();
} catch (AlreadyBoundException e) {
e.printStackTrace();
disConnect();
} catch (CMPPProtocolException e) {
e.printStackTrace();
disConnect();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
disConnect();
} catch (IOException e) {
e.printStackTrace();
disConnect();
} catch (InterruptedException e) {
e.printStackTrace();
disConnect();
}
}
}
public static void main(String[] arg) {
PropertyConfigurator.configure("config/log4j.properties");
SyncTransmitterExample30 st = new SyncTransmitterExample30();
try {
st.configure("config/mtTest.properties");
st.execute();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -