You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB
C#

using EntrustSettle.Common;
using EntrustSettle.IServices;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using System;
using System.Text;
namespace EntrustSettle.Services
{
public class QueueService : IQueueService
{
private readonly ILogger<QueueService> logger;
public QueueService(ILogger<QueueService> logger)
{
this.logger = logger;
}
public void Push(string opType, string data)
{
var mqUrl = AppSettings.app("RabbitMQ", "ConnectionString");
try
{
const string MqActionExchangeName = "amq.direct";
const string MqActionQueueName = "feedback.output";
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri(mqUrl);
using (IConnection conn = factory.CreateConnection())
using (IModel mqModel = conn.CreateModel())
{
mqModel.ExchangeDeclare(MqActionExchangeName, ExchangeType.Direct, true);
//var queueName = $"{MqActionQueueName}.{model.TenantId}";
var queueName = $"{MqActionQueueName}";
mqModel.QueueDeclare(queueName, false, false, false, null);
mqModel.QueueBind(queueName, MqActionExchangeName, queueName, null);
IBasicProperties props = mqModel.CreateBasicProperties();
props.DeliveryMode = 2;
byte[] messageBodyBytes = Encoding.UTF8.GetBytes(SharpZipLib.Compress(data));
mqModel.BasicPublish(MqActionExchangeName, queueName, props, messageBodyBytes);
conn.Close();
logger.LogInformation($"{opType},已发送数据到消息队列,数据内容:【{data}】");
}
}
catch (Exception)
{
throw;
}
}
}
}