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