📄 websmsender.java
字号:
package demo.cmppdemo20 ;
import java.io.IOException ;
import java.sql.PreparedStatement ;
import java.util.LinkedList ;
import java.util.List ;
import com.huawei.smproxy.CMPPSMProxy ;
import com.huawei.smproxy.comm.cmpp.message.CMPPDeliverMessage ;
import com.huawei.smproxy.comm.cmpp.message.CMPPDeliverRepMessage ;
import com.huawei.smproxy.comm.cmpp.message.CMPPMessage ;
import com.huawei.smproxy.comm.cmpp.message.CMPPSubmitMessage ;
import com.huawei.smproxy.comm.cmpp.message.CMPPSubmitRepMessage ;
import com.huawei.smproxy.util.Args ;
import com.huawei.smproxy.util.TypeConvert ;
import java.io.UnsupportedEncodingException;
/**
* <p>Web发送短消息管理操作类,具体负责将页面提交的短消息发送到infoX</p>
*/
public class WebSMSender extends CMPPSMProxy
{
//系统配置信息
private static Args arg = Env.getConfig ().getArgs ( "CMPPConnect" ) ;
//业务类型,应与Infox对应
public static final String service_Id = Env.getConfig ().get ( "CMPPSubmitMessage/service_Id" , "WebSM" ) ;
//信息内容来源,对应于登录Infox的帐号
public static final String msg_Src = Env.getConfig ().get ( "CMPPSubmitMessage/msg_Src" , "WebSMS" ) ;
//接入号码
public static final String connectCode = Env.getConfig ().get ( "CMPPSubmitMessage/src_Terminal_Id" , "" ) ;
//用于存放所有待发送的消息,线性表中每个元素是一个CMPPMessage实例
//当有信息发送时,将生成的CMPPMessage加入到列表中,
//由后台线程负责从列表中按条取出信息并将其发送出去,然后再从列表中删除这条消息
//使用Vector是为了支持多线程同步
private static List msgs = new LinkedList () ;
private static WebSMSender instance ;
public static WebSMSender getInstance ()
{
if ( instance == null )
{
instance = new WebSMSender () ;
}
return instance ;
}
protected WebSMSender ()
{
super ( WebSMSender.arg ) ;
}
/**
* 当与InfoX的连接被中断时的处理
*/
public void OnTerminate ()
{
System.out.println ( "Connection have been breaked! " ) ;
}
/**
* 对ISMG主动下发的消息的处理。函数中只对状态报告进行处理,所有其它消息都不进行处理,且回复错误消息。
* @param msg 收到的消息。
* @return 返回的相应消息。
*/
public CMPPMessage onDeliver ( final CMPPDeliverMessage msg )
{
byte[] msgId = msg.getMsgId () ;
//状态报告
if ( msg.getRegisteredDeliver () == 1 )
{
if ( ( String.valueOf ( msg.getStat () ).equalsIgnoreCase (
"DELIVRD" ) ) )
{
System.out.println ( "\t\treceived DELIVRD message msgid=[" + msg.getMsgId () + "]" ) ;
long submitMsgId = TypeConvert.byte2long ( msg.getStatusMsgId () ) ;
PreparedStatement stat = null ;
try
{
return new CMPPDeliverRepMessage ( msgId , 0 ) ;
}
catch ( Exception ex )
{
return new CMPPDeliverRepMessage ( msgId , 9 ) ;
}
}
}
//不是状态报告
else
{
//System.out.println ( "\t\treceived non DELIVRD message msgid=[" + msg.getMsgId () + "]" ) ;
//注释:可以另外写一个类专门对上行消息进行处理,类里面包含一个handle方法。
handle(msg);
}
return new CMPPDeliverRepMessage ( msgId , 0 ) ;
}
/**
* 发送一条消息,完成真正的消息发送。
* @param msg 待发送的消息。
* @return true:发送成功。false:发送失败。
*/
public boolean send ( CMPPSubmitMessage msg )//注释:可以另一个发送类,并实现多线程发送
{
if ( msg == null )
{
return false ;
}
CMPPSubmitRepMessage reportMsg = null ;
PreparedStatement stat = null ;
try
{
reportMsg = ( CMPPSubmitRepMessage )super.send ( msg ) ;
}
catch ( IOException ex )
{
ex.printStackTrace () ;
return false ;
}
return true ;
}
public void handle(CMPPDeliverMessage msg)
{
int msgFmt = 0; //上行消息的编码方式
String msgContent = ""; //上行消息的内容
String srcAddr = ""; //消息主叫地址
String destAddr = ""; //消息被叫地址
msgFmt = msg.getMsgFmt();
srcAddr = msg.getSrcterminalId();
destAddr = msg.getDestnationId().substring(4); //假设sp号码长度是4
if(msgFmt != 8)
{
//注释:写一个消息类,继承submit消息,逻辑处理完成后,构建一个消息,调用发送方法,提交消息到网关
CMPPSubmitMessage msgmt = new CMPPSubmitMessage (
1 ,
1 ,
1 , //需要状态报告
1 ,
"websms" ,
1 ,
"13012345678" ,
0 ,
0 ,
0 ,
"websms" ,
"02" ,
"10" ,
new java.util.Date ( System.currentTimeMillis ()
+ 2 * 24 * 60 * 60 * 1000 ) ,
null ,
destAddr ,
new String[]{srcAddr} ,
"消息编码方式错误".getBytes () ,
"" );
send(msgmt);
}
else
{
try{
msgContent = new String ( msg.getMsgContent () , "utf16-be" ) ;
CMPPSubmitMessage msgmt = new CMPPSubmitMessage (
1 ,
1 ,
1 , //需要状态报告
1 ,
"websms" ,
1 ,
"13012345678" ,
0 ,
0 ,
0 ,
"websms" ,
"02" ,
"10" ,
new java.util.Date ( System.currentTimeMillis ()
+ 2 * 24 * 60 * 60 * 1000 ) ,
null ,
destAddr ,
new String[]{srcAddr} ,
msgContent.getBytes () ,
"" );
send(msgmt);
}catch (UnsupportedEncodingException e)
{}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -