作者 朱兆平

几个测试文件。

  1 +package com.airport.core;
  2 +
  3 +import com.airport.bean.MessageBak;
  4 +import com.airport.bean.XMLHeader;
  5 +import com.airport.dao.Dao;
  6 +import com.airport.dao.impl.DaoImpl;
  7 +import com.airport.util.ConfigUtils;
  8 +import com.airport.util.SystemBean;
  9 +import com.airport.util.Utils;
  10 +import com.airport.util.parse.IMFXMLMaker;
  11 +import com.airport.util.parse.XMPParse;
  12 +import com.caac.imf.api.IMFClient;
  13 +import org.apache.commons.lang.StringUtils;
  14 +import org.apache.log4j.Logger;
  15 +
  16 +import java.util.Iterator;
  17 +import java.util.List;
  18 +import java.util.Map;
  19 +
  20 +public class CDHZSender extends Thread {
  21 + private static final Logger logger = Logger.getLogger(IMFNoDelaySender.class);
  22 + private Dao dao = (DaoImpl) SystemBean.getBean("dao");
  23 + private IMFClient client;
  24 + private String indexFileName; //记录发送数据的节点
  25 + private String sql;
  26 + private String sqlMax;
  27 +
  28 + public CDHZSender(IMFClient client, String fileName,String sql,String sqlMax) {
  29 + this.indexFileName = fileName;
  30 + this.client = client;
  31 + this.sql=sql;
  32 + this.sqlMax=sqlMax;
  33 + }
  34 +
  35 +
  36 + public void run() {
  37 + try {
  38 +
  39 + if (this.client == null) {
  40 + logger.info("IMFClient has been closed");
  41 + return;
  42 + }
  43 +
  44 + String msg = "<MSG><META><SNDR>KAKO</SNDR><RCVR/><SEQN>Z-001</SEQN><DDTM>20181008112408256</DDTM><TYPE>KAKO</TYPE><STYP>CARM</STYP></META><BODY>TEST</BODY></MSG>";
  45 + sendXml(msg);
  46 +
  47 + }catch (Exception var14) {
  48 + var14.printStackTrace();
  49 + logger.info(var14.getMessage());
  50 + }
  51 + }
  52 +
  53 + public synchronized void sendXml(String msg){
  54 + logger.info("线程:::"+Thread.currentThread().getName()+"开始发送数据 XML:" + msg);
  55 + String is_send_ok = this.client.sendMSG(msg);
  56 + logger.info("发送回执=" + is_send_ok);
  57 + }
  58 +
  59 + private static String tranlateXML(String xml) throws Exception {
  60 + String stype = XMPParse.getNodeValueFromXmlString(xml, "/MSG/META/STYP");
  61 + if (!StringUtils.isBlank(stype)) {
  62 + if (ConfigUtils.XTYPE_MAP.containsKey(stype)) {
  63 + stype = (String)ConfigUtils.XTYPE_MAP.get(stype);
  64 + } else {
  65 + logger.info("stype=" + stype + " 在对应关系里找不到这个KEY");
  66 + }
  67 + } else {
  68 + logger.info(" 在XML找不到STYPE这个节点");
  69 + stype = "";
  70 + }
  71 +
  72 + return stype;
  73 + }
  74 +}
  1 +//
  2 +// Source code recreated from a .class file by IntelliJ IDEA
  3 +// (powered by Fernflower decompiler)
  4 +//
  5 +
  6 +package com.airport.core;
  7 +
  8 +import com.airport.bean.MessageBak;
  9 +import com.airport.bean.XMLHeader;
  10 +import com.airport.dao.Dao;
  11 +import com.airport.dao.impl.DaoImpl;
  12 +import com.airport.util.ConfigUtils;
  13 +import com.airport.util.SystemBean;
  14 +import com.airport.util.Utils;
  15 +import com.airport.util.parse.IMFXMLMaker;
  16 +import com.airport.util.parse.XMPParse;
  17 +import com.caac.imf.api.IMFClient;
  18 +import org.apache.commons.lang.StringUtils;
  19 +import org.apache.log4j.Logger;
  20 +
  21 +import java.util.Iterator;
  22 +import java.util.List;
  23 +import java.util.Map;
  24 +
  25 +public class ZYFNoDelaySender extends Thread{
  26 + private static final Logger logger = Logger.getLogger(ZYFNoDelaySender.class);
  27 + private Dao dao = (DaoImpl)SystemBean.getBean("dao");
  28 + private IMFClient client;
  29 + private String indexFileName; //记录发送数据的节点
  30 + private String sql;
  31 + private String sqlMax;
  32 +
  33 + public ZYFNoDelaySender(IMFClient client, String fileName, String sql, String sqlMax) {
  34 + this.indexFileName = fileName;
  35 + this.client = client;
  36 + this.sql=sql;
  37 + this.sqlMax=sqlMax;
  38 + }
  39 +
  40 + public synchronized void run() {
  41 + try {
  42 +
  43 + if (this.client == null) {
  44 + logger.info("IMFClient has been closed");
  45 + return;
  46 + }
  47 +
  48 + while(true) {
  49 + //初始化读取
  50 + logger.info("job start,发送线程开始["+Thread.currentThread().getName()+"]");
  51 + int lastFID = Utils.readFIDIndex(this.indexFileName);
  52 + int maxFID = this.dao.getMaxFID(sqlMax);
  53 + logger.info("lastROW=" + lastFID + " maxCount=" + maxFID);
  54 + int is_ok = 0;
  55 +
  56 + if (maxFID > lastFID) {
  57 + //这里修改从FID_INDEX文件读取的FID ,改为取搜索结果的rownum,此处FID代表ROWNUM
  58 + String sqlformat = String.format(sql, lastFID, lastFID, ConfigUtils.RECORD_COUNT);
  59 + logger.info(sqlformat);
  60 + List<MessageBak> list = this.dao.getRecordByFID(lastFID,sqlformat); //查询大于[FID]350的报文
  61 + //如果两个发送者,要用到这里
  62 + Map<Integer, XMLHeader> map = ConfigUtils.XML_HEADER_MAP;
  63 + Iterator var7 = list.iterator();
  64 +
  65 + while(var7.hasNext()) {
  66 + MessageBak m = (MessageBak)var7.next();
  67 + if (is_ok != 0) {
  68 + logger.error("记录FID时发生异常,发送消息任务被迫中断");
  69 + return;
  70 + }
  71 +
  72 + if (m.getContent() != null) {
  73 + int times = 0;
  74 + //配置文件里面有几个sender 这里循环几次
  75 + for(Iterator var10 = map.keySet().iterator(); var10.hasNext(); times++) {
  76 + Integer key = (Integer)var10.next();
  77 + XMLHeader header = (XMLHeader)map.get(key);
  78 + logger.info("key=" + key + " value=" + ((XMLHeader)map.get(key)).toString());
  79 + logger.info("准备发送数据 FID:" + m.getFid() + " XML.length=:" + m.getContent().length());
  80 +
  81 +
  82 + //初始化最终发布报文内容的存储器
  83 + String sendMsg = "";
  84 + String xmlStr = m.getContent();
  85 +
  86 +
  87 +//_____________________________________给多式联运的航空报文,读取配置文件,生成新的META,并把最原始的报文封装到新报文的content里面--------------------------------------------------------------------
  88 + String sndr = header.getSndr();
  89 + String type = header.getType();
  90 + String styp = header.getStype();
  91 +
  92 + IMFXMLMaker newXml = new IMFXMLMaker(sndr,Integer.toString(m.getFid()),type,styp);
  93 + newXml.setIMFContent(m.getContent());
  94 + sendMsg = newXml.finalXmlStr();
  95 +
  96 + if (!"".equals(sendMsg)) {
  97 + sendXml(sendMsg);
  98 + is_ok = Utils.writeFIDIndex(this.indexFileName, m.getRownum_()); //发送成功写入节点文件
  99 + logger.debug("FID:" + m.getFid() + " 缓存成功");
  100 + ++times;
  101 + } else {
  102 + logger.error("查找替换节点数据出现异常,可能找不到这个节点,原始XML=" + m.getContent());
  103 + }
  104 + }
  105 +
  106 +// this.dao.update(m.getFid(), times);
  107 + } else {
  108 + logger.info("FID=" + m.getFid() + "XML is null ");
  109 + }
  110 + }
  111 + }else {
  112 + try {
  113 + Thread.sleep(100000L);
  114 + logger.info("发送线程》》》"+Thread.currentThread().getName()+"休眠");
  115 + } catch (InterruptedException var3) {
  116 + var3.printStackTrace();
  117 + }
  118 + }
  119 + }
  120 +
  121 + }catch (Exception var14) {
  122 + var14.printStackTrace();
  123 + logger.info(var14.getMessage());
  124 + }
  125 + }
  126 +
  127 + public synchronized void sendXml(String msg){
  128 + logger.info("线程:::"+Thread.currentThread().getName()+"开始发送数据 XML:" + msg);
  129 + String is_send_ok = this.client.sendMSG(msg);
  130 + logger.info("发送回执=" + is_send_ok);
  131 + }
  132 +
  133 + private static String tranlateXML(String xml) throws Exception {
  134 + String stype = XMPParse.getNodeValueFromXmlString(xml, "/MSG/META/STYP");
  135 + if (!StringUtils.isBlank(stype)) {
  136 + if (ConfigUtils.XTYPE_MAP.containsKey(stype)) {
  137 + stype = (String)ConfigUtils.XTYPE_MAP.get(stype);
  138 + } else {
  139 + logger.info("stype=" + stype + " 在对应关系里找不到这个KEY");
  140 + }
  141 + } else {
  142 + logger.info(" 在XML找不到STYPE这个节点");
  143 + stype = "";
  144 + }
  145 +
  146 + return stype;
  147 + }
  148 +}
  1 +package com.airport.util.parse;
  2 +import org.xml.sax.SAXException;
  3 +
  4 +import java.io.File;
  5 +import java.io.IOException;
  6 +import javax.xml.transform.Source;
  7 +import javax.xml.transform.stream.StreamSource;
  8 +import javax.xml.validation.Schema;
  9 +import javax.xml.validation.SchemaFactory;
  10 +import javax.xml.validation.Validator;
  11 +//根据XSD验证xml准确性
  12 +public class ValidateXML {
  13 +
  14 + public ValidateXML(){
  15 +
  16 + }
  17 +
  18 + public boolean Validatexml(String xsdpath,String xmlpath) throws SAXException,IOException{
  19 + //建立schema工厂
  20 + SchemaFactory schemaFactory=SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  21 + //建立验证文档文件对象,利用此文件对象所封装的文件进行schema验证
  22 + File schemaFile=new File(xsdpath);
  23 + //利用schema工厂,接收验证文档文件对象生成Schema对象
  24 + Schema schema=schemaFactory.newSchema(schemaFile);
  25 + //通过Schema产生针对于此Schema的验证器,利用schenaFile进行验证
  26 + Validator validator=schema.newValidator();
  27 + //得到验证的数据源
  28 + Source source=new StreamSource(xmlpath);
  29 + //开始验证,成功输出success!!!,失败输出fail
  30 + try{
  31 +
  32 + validator.validate(source);
  33 +
  34 + }catch(Exception ex){
  35 +
  36 + ex.printStackTrace();
  37 +
  38 + }
  39 + return true;
  40 + }
  41 +}
@@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
34 </bean> 34 </bean>
35 35
36 <bean id='configurationBean' class="com.airport.util.Configuration" > 36 <bean id='configurationBean' class="com.airport.util.Configuration" >
37 -<property name="location" value="file:config/config.properties"/> 37 +<property name="location" value="file:../../TEST/config.properties"/>
38 </bean> 38 </bean>
39 39
40 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 40 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">