From 8160ad6389970b0e9deab4bc5221807b13d4f936 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Wed, 26 Apr 2023 13:39:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B4=BE=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Myshipping.Application/Helper/MQHelper.cs | 91 +++++++++++++++++++ .../Dtos/BookingTruckSyncMessageInfo.cs | 34 +++++++ 2 files changed, 125 insertions(+) create mode 100644 Myshipping.Application/Helper/MQHelper.cs create mode 100644 Myshipping.Application/Service/BookingTruck/Dtos/BookingTruckSyncMessageInfo.cs diff --git a/Myshipping.Application/Helper/MQHelper.cs b/Myshipping.Application/Helper/MQHelper.cs new file mode 100644 index 00000000..1c5abdf0 --- /dev/null +++ b/Myshipping.Application/Helper/MQHelper.cs @@ -0,0 +1,91 @@ +using Furion.JsonSerialization; +using Furion.Logging; +using Microsoft.Extensions.Logging; +using Myshipping.Core; +using RabbitMQ.Client; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Myshipping.Application +{ + /// + /// MQ 消息队列 + /// + public static class MQHelper + { + /// + /// 发送MQ(阿里消息队列) + /// + /// 请求报文 + /// MQ Url + /// 交换名称 + /// 队列名 + /// + /// 返回结果 + public static TaskManageOrderResultDto SendMQ(string message,string mqUrl,string ExchangeName,string queueName, + long tenantId) + { + + TaskManageOrderResultDto result = new TaskManageOrderResultDto(); + + //日志 + var logger = Log.CreateLogger(nameof(MQHelper)); + + //日志参数 + logger.LogInformation("message={msg},url={url} exchange={exc} queue={que}", message, mqUrl, ExchangeName, queueName); + + try + { + ConnectionFactory factory = new ConnectionFactory(); + factory.Uri = new Uri(mqUrl); + + using (IConnection conn = factory.CreateConnection()) + { + IModel mqModel = conn.CreateModel(); + + mqModel.ExchangeDeclare(ExchangeName, ExchangeType.Direct); + + var mqQueueName = $"{queueName}.{tenantId}"; + + mqModel.QueueDeclare(mqQueueName, false, false, false, null); + mqModel.QueueBind(mqQueueName, ExchangeName, mqQueueName, null); + + logger.LogInformation("MQ绑定完成"); + + byte[] messageBodyBytes = Encoding.UTF8.GetBytes(message); + + logger.LogInformation("报文生成二进制完成 length={len}", messageBodyBytes.Length); + + IBasicProperties props = mqModel.CreateBasicProperties(); + props.DeliveryMode = 2; + + + mqModel.BasicPublish(ExchangeName, + queueName, props, + messageBodyBytes); + + logger.LogInformation("MQ报文推送完成"); + conn.Close(); + + logger.LogInformation("MQ链接关闭"); + } + + result.succ = true; + result.msg = "MQ报文推送完成"; + } + catch (Exception ex) + { + logger.LogError("MQ报文推送异常 异常={error}", ex.Message); + logger.LogError("MQ报文推送异常 StackTrace={error}", ex.StackTrace); + + result.succ = false; + result.msg = ex.GetMessage("MQ报文推送异常"); + } + + return result; + } + } +} diff --git a/Myshipping.Application/Service/BookingTruck/Dtos/BookingTruckSyncMessageInfo.cs b/Myshipping.Application/Service/BookingTruck/Dtos/BookingTruckSyncMessageInfo.cs new file mode 100644 index 00000000..a6e0c3a0 --- /dev/null +++ b/Myshipping.Application/Service/BookingTruck/Dtos/BookingTruckSyncMessageInfo.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Myshipping.Application +{ + /// + /// 订舱派车同步东胜 + /// + public class BookingTruckSyncMessageInfo + { + /// + /// Head + /// + public BookingTruckSyncMessageHeadInfo Head { get; set; } + + /// + /// Main + /// + public BookingTruckSyncMessageMainInfo Main { get; set; } + } + + public class BookingTruckSyncMessageHeadInfo: RulesEngineWebAPIHeadBase + { + + } + + public class BookingTruckSyncMessageMainInfo: BookingTruckSyncDto + { + + } +}