正在显示
3 个修改的文件
包含
34 行增加
和
26 行删除
| @@ -54,25 +54,26 @@ public class RabbitController { | @@ -54,25 +54,26 @@ public class RabbitController { | ||
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | @PostMapping("/product/old") | 56 | @PostMapping("/product/old") |
| 57 | - public ResultJson productOld(String xmlStr) throws DocumentException | 57 | + public ResultJson productOld(String xmlStr) |
| 58 | { | 58 | { |
| 59 | - // 1、解析 xml 文件,获取数据 | ||
| 60 | - XmlData xmlData = xmlUtils.parsingMessage(xmlStr); | ||
| 61 | - | ||
| 62 | - // 2、校验格式、数据 | ||
| 63 | - ResultJson resultJson = xmlUtils.checkFormatAndData(xmlData); | ||
| 64 | - if (!"200".equals(resultJson.getCode())) { | ||
| 65 | - return resultJson; | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - // 3、通过格式校验之后,进行配置校验 | ||
| 69 | - Boolean binding = userMessageBindingService.validateXmlBinding(xmlData); | ||
| 70 | - if (!binding) { | ||
| 71 | - return ResultJson.error(CustomExceptionType.BINDING_ERROR); | 59 | + try { |
| 60 | + // 1、解析 xml 文件,获取数据 | ||
| 61 | + XmlData xmlData = xmlUtils.parsingMessage(xmlStr); | ||
| 62 | + String SNDR = xmlData.getSender(); | ||
| 63 | + String RCVR = xmlData.getQueueName(); | ||
| 64 | + Date DDTM = xmlData.getSendDateTime() == null ? new Date() : xmlData.getSendDateTime(); | ||
| 65 | + String TYPE = xmlData.getExchangeName(); | ||
| 66 | + String STYP = xmlData.getRoutingKeyName(); | ||
| 67 | + String TOKN = xmlData.getToken(); | ||
| 68 | + String SEQN = xmlData.getSequence(); | ||
| 69 | + String VSHT = xmlData.getVirtualHostName(); | ||
| 70 | + String SERV = xmlData.getServerName(); | ||
| 71 | + String content = xmlData.getSendContent(); | ||
| 72 | + ResultJson result = productNew(SNDR, RCVR, DDTM, TYPE, STYP, TOKN, SEQN, VSHT, SERV, content); | ||
| 73 | + return result; | ||
| 74 | + } catch (DocumentException e) { | ||
| 75 | + return ResultJson.error(CustomExceptionType.CLIENT_EXCEPTION); | ||
| 72 | } | 76 | } |
| 73 | - | ||
| 74 | - // 4、发送消息,并讲消息存放到数据库中 | ||
| 75 | - return ResultJson.success("发送成功"); | ||
| 76 | } | 77 | } |
| 77 | 78 | ||
| 78 | @PostMapping("/product/new") | 79 | @PostMapping("/product/new") |
| @@ -116,7 +117,7 @@ public class RabbitController { | @@ -116,7 +117,7 @@ public class RabbitController { | ||
| 116 | // 4、mq发送消息,数据库中保存消息并保存至ES | 117 | // 4、mq发送消息,数据库中保存消息并保存至ES |
| 117 | return sendAndSave(sentData); | 118 | return sendAndSave(sentData); |
| 118 | } catch (IOException | TimeoutException | InterruptedException e) { | 119 | } catch (IOException | TimeoutException | InterruptedException e) { |
| 119 | - return ResultJson.error(CustomExceptionType.SERVER_EXCEPTION); | 120 | + return ResultJson.error(CustomExceptionType.SERVER_EXCEPTION); |
| 120 | } | 121 | } |
| 121 | } | 122 | } |
| 122 | 123 |
| @@ -9,6 +9,7 @@ package com.sunyo.wlpt.message.bus.service.exception; | @@ -9,6 +9,7 @@ package com.sunyo.wlpt.message.bus.service.exception; | ||
| 9 | public enum CustomExceptionType { | 9 | public enum CustomExceptionType { |
| 10 | MESSAGE_SUCCESS("10200", "消息发送成功"), | 10 | MESSAGE_SUCCESS("10200", "消息发送成功"), |
| 11 | SERVER_EXCEPTION("10500", "服务器异常,发送消息失败!"), | 11 | SERVER_EXCEPTION("10500", "服务器异常,发送消息失败!"), |
| 12 | + CLIENT_EXCEPTION("10400", "报文格式错误,请检查报文格式!"), | ||
| 12 | 13 | ||
| 13 | BINDING_ERROR("10501", "配置信息,未进行绑定!"), | 14 | BINDING_ERROR("10501", "配置信息,未进行绑定!"), |
| 14 | SENDER_ERROR("10401", "报文格式错误,发送者不能为空!"), | 15 | SENDER_ERROR("10401", "报文格式错误,发送者不能为空!"), |
| @@ -8,12 +8,15 @@ import io.netty.util.internal.StringUtil; | @@ -8,12 +8,15 @@ import io.netty.util.internal.StringUtil; | ||
| 8 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
| 9 | import org.dom4j.Document; | 9 | import org.dom4j.Document; |
| 10 | import org.dom4j.DocumentException; | 10 | import org.dom4j.DocumentException; |
| 11 | +import org.dom4j.DocumentHelper; | ||
| 11 | import org.dom4j.Element; | 12 | import org.dom4j.Element; |
| 12 | -import org.dom4j.io.SAXReader; | ||
| 13 | import org.springframework.beans.factory.annotation.Value; | 13 | import org.springframework.beans.factory.annotation.Value; |
| 14 | import org.springframework.stereotype.Component; | 14 | import org.springframework.stereotype.Component; |
| 15 | 15 | ||
| 16 | import javax.annotation.Resource; | 16 | import javax.annotation.Resource; |
| 17 | +import java.text.ParsePosition; | ||
| 18 | +import java.text.SimpleDateFormat; | ||
| 19 | +import java.util.Date; | ||
| 17 | import java.util.List; | 20 | import java.util.List; |
| 18 | 21 | ||
| 19 | /** | 22 | /** |
| @@ -50,20 +53,23 @@ public class XmlUtils { | @@ -50,20 +53,23 @@ public class XmlUtils { | ||
| 50 | */ | 53 | */ |
| 51 | public XmlData parsingMessage(String xmlStr) throws DocumentException | 54 | public XmlData parsingMessage(String xmlStr) throws DocumentException |
| 52 | { | 55 | { |
| 53 | - // 设置文件名 | ||
| 54 | - String title = "test.xml"; | ||
| 55 | - String filePath = dir + "/" + title; | ||
| 56 | - | ||
| 57 | - SAXReader reader = new SAXReader(); | ||
| 58 | - Document document = reader.read(filePath); | 56 | + // 将String类型的xml字符串转成xml |
| 57 | + Document document = DocumentHelper.parseText(xmlStr); | ||
| 59 | Element msg = document.getRootElement(); | 58 | Element msg = document.getRootElement(); |
| 60 | Element meta = msg.element("META"); | 59 | Element meta = msg.element("META"); |
| 60 | + String body = msg.elementText("BODY"); | ||
| 61 | + | ||
| 62 | + String ddtm = meta.elementText("DDTM"); | ||
| 63 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); | ||
| 64 | + ParsePosition pos = new ParsePosition(0); | ||
| 65 | + Date sendDateTime = formatter.parse(ddtm, pos); | ||
| 61 | 66 | ||
| 62 | XmlData xmlData = XmlData.builder() | 67 | XmlData xmlData = XmlData.builder() |
| 63 | - .sendContent(xmlStr) | 68 | + .sendContent(msg.elementText("BODY")) |
| 64 | .sender(meta.elementText("SNDR")) | 69 | .sender(meta.elementText("SNDR")) |
| 65 | .queueName(meta.elementText("RCVR")) | 70 | .queueName(meta.elementText("RCVR")) |
| 66 | .sendTime(meta.elementText("DDTM")) | 71 | .sendTime(meta.elementText("DDTM")) |
| 72 | + .sendDateTime(sendDateTime) | ||
| 67 | .exchangeName(meta.elementText("TYPE")) | 73 | .exchangeName(meta.elementText("TYPE")) |
| 68 | .routingKeyName(meta.elementText("STYP")) | 74 | .routingKeyName(meta.elementText("STYP")) |
| 69 | .sequence(meta.elementText("SEQN")) | 75 | .sequence(meta.elementText("SEQN")) |
-
请 注册 或 登录 后发表评论