2024-10-28 16:23:39 +08:00
|
|
|
|
using MassTransit;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Extensions.DependencyInjections;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.RabbitMQ.Senders
|
|
|
|
|
|
{
|
|
|
|
|
|
public class NSender:INSender,ISingletonDependency
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ISendEndpointProvider _sendEndpointProvider;
|
|
|
|
|
|
private readonly string _issuedKey;
|
|
|
|
|
|
private readonly string _receivedKey;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public NSender(ISendEndpointProvider sendEndpointProvider, IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
_sendEndpointProvider = sendEndpointProvider;
|
|
|
|
|
|
_issuedKey = configuration["MQ:Queue:Issued"]!;
|
|
|
|
|
|
_receivedKey = configuration["MQ:Queue:Received"]!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToIssuedAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync(_issuedKey, message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToIssuedAsync(object message, CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync(_issuedKey, message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToReceivedAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync(_receivedKey, message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToReceivedAsync(object message, CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync(_receivedKey, message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-29 16:28:14 +08:00
|
|
|
|
public async Task SendToReceivedLoginAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync($"{_receivedKey}_Login", message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToReceivedLoginAsync(object message, CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync($"{_receivedKey}_Login", message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToReceivedHeartbeatAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync($"{_receivedKey}_Heartbeat", message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendToReceivedHeartbeatAsync(object message, CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendAsync($"{_receivedKey}_Heartbeat", message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-28 16:23:39 +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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|