当用户手机编辑短信发送订购或者退订信息时,MISC会发送同步消息给我们的系统。下面就是处理的过程:
1、MISC 向 SP 发送订购关系同步请求 SyncOrderRelationReq:
<?xml version=\"1.0\" encoding=\"utf-8\"?><SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"> <SOAP-ENV:Header> <TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\">00110318384464</TransactionID> </SOAP-ENV:Header> <SOAP-ENV:Body> <SyncOrderRelationReq xmlns=\"http://www.monternet.com/dsmp/schemas/\"> <Version>1.5.0</Version> <MsgType>SyncOrderRelationReq</MsgType><Send_Address><DeviceType>0</DeviceType> <DeviceID>0011</DeviceID></Send_Address><Dest_Address><DeviceType>400</DeviceType><DeviceID>0</DeviceID></Dest_Address><FeeUser_ID><UserIDType>1</UserIDType> <MSISDN>13985046628</MSISDN><PseudoCode></PseudoCode></FeeUser_ID> <DestUser_ID><UserIDType>1</UserIDType><MSISDN>13985046628</MSISDN> <PseudoCode></PseudoCode></DestUser_ID><LinkID>SP</LinkID><ActionID>1</ActionID> <ActionReasonID>1</ActionReasonID><SPID>924403</SPID><SPServiceID>业务代码</SPServiceID><AccessMode>3</AccessMode> <FeatureStr>MTA2NjIxNDQgREE=</FeatureStr> </SyncOrderRelationReq> </SOAP-ENV:Body> </SOAP-ENV:Envelope>2、接收MISC同步请求的Servlet(Servlet的地址一定要跟申请的受理地址对应起来):
/** * 订购关系同步消息接口 * @throws IOException */ @SuppressWarnings("static-access") public void SyncOrderRelationReq(HttpServletRequest request,HttpServletResponse response) throws IOException{ String resultXml = ""; boolean resultStr = true; String XMLData = null; StringBuffer tempStringBuffer = new StringBuffer(); String tempString = null; BufferedReader reader = request.getReader();//接受MISC的订购关系同步请求包SyncOrderRelationReq while ((tempString = reader.readLine()) != null){ tempStringBuffer.append(tempString);//读取订购关系同步请求包SyncOrderRelationReq内容 } XMLData = tempStringBuffer.toString();//转换订购关系同步请求包SyncOrderRelationReq内容为STRING,存储到XMLDAT System.out.println("++++++++++++++++++++++++++++++++++++"); System.out.println("接收到的同步消息数据"); System.out.println(XMLData); System.out.println("++++++++++++++++++++++++++++++++++++"); ProvisionBean p = null; if(XMLData == null || XMLData.length() < 10 ){ resultStr = false; }else{ p = new AnalysisXML().getXmlResultt(XMLData); if(p==null){ resultStr = false; }else{ ////处理同步消息 } } PrintWriter out = response.getWriter(); if(resultStr){ //返回成功 resultXml = new AnalysisXML().getResultXml(p.getTransactionID(), p.getVersion(), "0"); }else{ //返回失败 resultXml = new AnalysisXML().getResultXml(p.getTransactionID(), p.getVersion(), "1"); } response.setContentType( "text/xml" ) ; out.print(resultXml); out.flush(); out.close(); }存放消息的实体类:
package com.bx.core.entity; import java.io.Serializable; /** * 订购同步消息bean * @author admin_Hzw * */ public class ProvisionBean implements Serializable { private Long id; //主键 private String msisdnu; //计费用户标识 private String MSISDN_D; //使用用户标识 private String TransactionID; //消息编号 private String MsgType; //消息类型 private String Version;//该接口消息的版本号,本次所有的接口消息的版本都为“1.5.0” //-------------------- private String LinkID; //临时订购关系的事务ID /* * 服务状态管理动作代码,具体值如下: 1: 开通服务; 2: 停止服务; 3: 激活服务; 4: 暂停服务; 5: 免费试用; 8: 第三方确认 */ private String ActionID; /* * 产生服务状态管理动作原因的代码,具体值如下: 1:用户发起行为 2:系统发起行为 3:扣费失败导致的服务取消 4:其他 */ private String ActionReasonID; private String SPID; //SP的企业代码 private String SPServiceID; //SP中该服务的服务代码 private String FeatureStr; //服务订购参数在短信MO流程中,FeatureStr中传送的是AccessNo+短信内容。 private String platUserId;//平台侧用户id private String createtime; //创建时间 private String status;//状态0为订购成功,1为未成功 private String Pno; //产品编号 public String getMsisdnu() { return msisdnu; } public void setMsisdnu(String msisdnu) { this.msisdnu = msisdnu; } public String getMSISDN_D() { return MSISDN_D; } public void setMSISDN_D(String mSISDND) { MSISDN_D = mSISDND; } public String getTransactionID() { return TransactionID; } public void setTransactionID(String transactionID) { TransactionID = transactionID; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public String getVersion() { return Version; } public void setVersion(String version) { Version = version; } public String getLinkID() { return LinkID; } public void setLinkID(String linkID) { LinkID = linkID; } public String getActionID() { return ActionID; } public void setActionID(String actionID) { ActionID = actionID; } public String getActionReasonID() { return ActionReasonID; } public void setActionReasonID(String actionReasonID) { ActionReasonID = actionReasonID; } public String getSPID() { return SPID; } public void setSPID(String sPID) { SPID = sPID; } public String getSPServiceID() { return SPServiceID; } public void setSPServiceID(String sPServiceID) { SPServiceID = sPServiceID; } public String getFeatureStr() { return FeatureStr; } public void setFeatureStr(String featureStr) { FeatureStr = featureStr; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPlatUserId() { return platUserId; } public void setPlatUserId(String platUserId) { this.platUserId = platUserId; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getPno() { return Pno; } public void setPno(String pno) { Pno = pno; } }处理同步消息的工具类:
package com.china.boxun.util; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.util.Iterator; import java.util.List; import java.util.Random; import org.jdom.Element; import org.jdom.xpath.XPath; import com.bx.core.entity.ProvisionBean; public class AnalysisXML { /** * 解析返回的XML信息 * @param XMLData */ public static ProvisionBean getXmlResultt(String XMLData) { ProvisionBean p = new ProvisionBean(); File f = null; try{ org.jdom.input.SAXBuilder sb=new org.jdom.input.SAXBuilder(); org.jdom.Document doc=null; File ffFile = new File("C://temp"); if(!ffFile.exists()){ ffFile.mkdir(); } f = new File("C://temp//temp"+System.currentTimeMillis()+new Random().nextInt(100000)+".xml"); if(f.exists()){ f.delete(); } f.createNewFile(); RandomAccessFile ra = new RandomAccessFile(f,"rw"); ra.writeBytes(XMLData); ra.close(); doc=sb.build(f);//读取PROVISION.XML文件,SB和DOC的使用方法不是很明白; //以下是读取PROVISION.XML的信息,解释XML文件的各部分定义,和读取数值到变量里。 org.jdom.Element root=doc.getRootElement(); //读取header信息; Element header=(Element)XPath.selectSingleNode(root,"/SOAP-ENV:Envelope/SOAP-ENV:Header"); List ns=header.getChildren(); for(Iterator i=ns.iterator();i.hasNext();){ Object n=i.next(); if(n instanceof Element){ Element e=(Element)n; if(e.getName().equals("TransactionID")){ p.setTransactionID(e.getTextNormalize()); } } } //读取body信息; Element SyncOrderRelationReq = null ; Element body=(Element)XPath.selectSingleNode(root,"/SOAP-ENV:Envelope/SOAP-ENV:Body"); ns=body.getChildren(); for(Iterator i=ns.iterator();i.hasNext();){ Object n=i.next(); if(n instanceof Element){ Element e=(Element)n; if(e.getName().equals("SyncOrderRelationReq")){ SyncOrderRelationReq = e; } } } //取得数据包的所有孩子。 if(SyncOrderRelationReq!=null){ ns=SyncOrderRelationReq.getChildren(); for(Iterator i=ns.iterator();i.hasNext();){ Object n=i.next(); if(n instanceof Element){ Element e=(Element)n; //------------------------------------ if(e.getName().equals("FeeUser_ID")){ ns = e.getChildren(); for(Iterator j=ns.iterator();j.hasNext();){ Object k=j.next(); if(k instanceof Element){ Element eU=(Element)k; if(eU.getName().equals("MSISDN")){ p.setMsisdnu(eU.getText()); } } } } if(e.getName().equals("DestUser_ID")){ ns = e.getChildren(); for(Iterator j=ns.iterator();j.hasNext();){ Object k=j.next(); if(k instanceof Element){ Element eU=(Element)k; if(eU.getName().equals("MSISDN")){ p.setMSISDN_D(eU.getText()); } } } } //------------------------------------ if(e.getName().equals("Version")){ p.setVersion(e.getTextNormalize()); } if(e.getName().equals("MsgType")){ p.setMsgType(e.getTextNormalize()); } if(e.getName().equals("LinkID")){ p.setLinkID(e.getTextNormalize()); } if(e.getName().equals("ActionID")){ p.setActionID(e.getTextNormalize()); } if(e.getName().equals("ActionReasonID")){ p.setActionReasonID(e.getTextNormalize()); } if(e.getName().equals("SPID")){ p.setSPID(e.getTextNormalize()); } if(e.getName().equals("SPServiceID")){ p.setSPServiceID(e.getTextNormalize()); } if(e.getName().equals("FeatureStr")){ p.setFeatureStr(e.getTextNormalize()); } } } } }catch(Exception e){ e.printStackTrace(); System.out.println("同步MISC请求,解析出错!"); }finally{ if(f!=null){ f.delete(); } } return p; } /** * 模拟获得请求的XML * @return */ public static String getXml(){ return "<?xml version=\"1.0\" encoding=\"utf-8\"?><SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Header> <TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\">00110318384464</TransactionID></SOAP-ENV:Header><SOAP-ENV:Body><SyncOrderRelationReq xmlns=\"http://www.monternet.com/dsmp/schemas/\"><Version>1.5.0</Version>"+ "<MsgType>SyncOrderRelationReq</MsgType><Send_Address><DeviceType>0</DeviceType>"+ "<DeviceID>0011</DeviceID></Send_Address><Dest_Address><DeviceType>400</DeviceType><DeviceID>0</DeviceID></Dest_Address><FeeUser_ID><UserIDType>1</UserIDType>"+ "<MSISDN>13985046628</MSISDN><PseudoCode></PseudoCode></FeeUser_ID>"+ "<DestUser_ID><UserIDType>1</UserIDType><MSISDN>13985046628</MSISDN>"+ "<PseudoCode></PseudoCode></DestUser_ID><LinkID>SP</LinkID><ActionID>1</ActionID>"+ "<ActionReasonID>1</ActionReasonID><SPID>924403</SPID><SPServiceID>业务代码</SPServiceID><AccessMode>3</AccessMode>"+ "<FeatureStr>MTA2NjIxNDQgREE=</FeatureStr></SyncOrderRelationReq>"+ "</SOAP-ENV:Body></SOAP-ENV:Envelope>"; } /** * 响应XML * @param TransactionID 消息编号 * @param Version 版本号 * @param hRet 0 为成功,1为失败 * @return */ public static String getResultXml(String TransactionID,String Version,String hRet){ return "<SOAP-ENV:Envelope "+ "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "+ "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" "+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:dsmp=\"http://www.monternet.com/dsmp/schemas/\">"+ "<SOAP-ENV:Header>"+ "<dsmp:TransactionID xmlns:dsmp=\"http://www.monternet.com/dsmp/schemas/\">"+ TransactionID+ "</dsmp:TransactionID>"+ "</SOAP-ENV:Header>"+ "<SOAP-ENV:Body>"+ "<dsmp:SyncOrderRelationResp xmlns:dsmp=\"http://www.monternet.com/dsmp/schemas/\">"+ "<MsgType>SyncOrderRelationResp</MsgType>"+ "<Version>"+Version+"</Version>"+ "<hRet>"+hRet+"</hRet>"+ "</dsmp:SyncOrderRelationResp>"+ "</SOAP-ENV:Body>"+ "</SOAP-ENV:Envelope>"; } }模拟测试:
package com.china.boxun.hzw; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import com.china.boxun.util.AnalysisXML; public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String r = AnalysisXML.getXml(); String path ="http://localhost:8080/axis/services/bxserver"; java.net.URL url = new java.net.URL(path); // 打开连接 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 设置是否向connection输出,因为这个是post请求,参数要放在 // http正文内,因此需要设为true connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); // Post 请求不能使用缓存 connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "text/xml"); connection.connect(); DataOutputStream out = new DataOutputStream(connection .getOutputStream()); out.writeBytes(r); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; System.out.println("============================="); System.out.println("Contents of post request"); System.out.println("============================="); while ((line = reader.readLine()) != null) { System.out.println(line); } System.out.println("============================="); System.out.println("Contents of post request ends"); System.out.println("============================="); reader.close(); connection.disconnect(); } }