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.

57 lines
1.8 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;
}
8 months ago
public void Push(string opType, string compId, string data)
{
var mqUrl = AppSettings.app("RabbitMQ", "ConnectionString");
try
{
8 months ago
string exchangeName = "amq.direct";
string queueName = "feedback.output." + compId;
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri(mqUrl);
using (IConnection conn = factory.CreateConnection())
using (IModel mqModel = conn.CreateModel())
{
8 months ago
mqModel.ExchangeDeclare(exchangeName, ExchangeType.Direct, true);
mqModel.QueueDeclare(queueName, false, false, false, null);
8 months ago
mqModel.QueueBind(queueName, exchangeName, queueName, null);
IBasicProperties props = mqModel.CreateBasicProperties();
props.DeliveryMode = 2;
byte[] messageBodyBytes = Encoding.UTF8.GetBytes(SharpZipLib.Compress(data));
8 months ago
mqModel.BasicPublish(exchangeName, queueName, props, messageBodyBytes);
conn.Close();
7 months ago
logger.LogInformation($"{opType},已发送数据到消息队列,队列名称:【{queueName}】,数据内容:{Environment.NewLine}{data}");
}
}
catch (Exception)
{
throw;
}
}
}
}