53 lines
2.1 KiB
C#
Raw Normal View History

2024-10-25 19:11:43 +08:00
using JiShe.CollectBus.Common.Extensions.DependencyInjections;
using MassTransit;
using Microsoft.Extensions.Configuration;
namespace JiShe.CollectBus.RabbitMQ.Senders
{
2024-10-26 22:27:19 +08:00
public class MqSender : IMqSender, ISingletonDependency
2024-10-25 19:11:43 +08:00
{
private readonly ISendEndpointProvider _sendEndpointProvider;
private readonly IConfiguration _configuration;
public MqSender(ISendEndpointProvider sendEndpointProvider, IConfiguration configuration)
{
_sendEndpointProvider = sendEndpointProvider;
_configuration = configuration;
}
public async Task SendToPushAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
{
2024-10-26 22:27:19 +08:00
var queueKey = _configuration["MQ:Queue:Push"];
await SendAsync(queueKey, message, cancellationToken);
2024-10-25 19:11:43 +08:00
}
public async Task SendToPushAsync(object message, CancellationToken cancellationToken = default)
{
2024-10-26 22:27:19 +08:00
var queueKey = _configuration["MQ:Queue:Push"];
await SendAsync(queueKey, message, cancellationToken);
2024-10-25 19:11:43 +08:00
}
public async Task SendToReportAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
{
2024-10-26 22:27:19 +08:00
var queueKey = _configuration["MQ:Queue:Report"];
//await SendAsync(queueName, message, cancellationToken);
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{queueKey}"));
await endpoint.Send(message,typeof(T),cancellationToken);
2024-10-25 19:11:43 +08:00
}
public async Task SendToReportAsync(object message, CancellationToken cancellationToken = default)
{
2024-10-26 22:27:19 +08:00
var queueKey = _configuration["MQ:Queue:Report"];
await SendAsync(queueKey, message, cancellationToken);
2024-10-25 19:11:43 +08:00
}
public async Task SendAsync(string queueKey,object message, CancellationToken cancellationToken = default)
{
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{queueKey}"));
await endpoint.Send(message, cancellationToken);
}
}
}