2024-10-28 16:23:39 +08:00
|
|
|
|
using MassTransit;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-11-13 17:50:52 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-11-13 00:30:24 +08:00
|
|
|
|
using Volo.Abp.DependencyInjection;
|
2024-10-28 16:23:39 +08:00
|
|
|
|
|
2024-11-13 17:50:52 +08:00
|
|
|
|
namespace JiShe.CollectBus.MQ.Sender
|
2024-10-28 16:23:39 +08:00
|
|
|
|
{
|
2024-11-13 17:50:52 +08:00
|
|
|
|
public class NSender : INSender, ISingletonDependency
|
2024-10-28 16:23:39 +08:00
|
|
|
|
{
|
2024-11-13 17:50:52 +08:00
|
|
|
|
private readonly ServiceProvider _serviceProvider;
|
2024-10-28 16:23:39 +08:00
|
|
|
|
private readonly string _issuedKey;
|
|
|
|
|
|
private readonly string _receivedKey;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-13 17:50:52 +08:00
|
|
|
|
public NSender(IConfiguration configuration, ServiceProvider serviceProvider)
|
2024-10-28 16:23:39 +08:00
|
|
|
|
{
|
2024-11-13 17:50:52 +08:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2024-10-28 16:23:39 +08:00
|
|
|
|
_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)
|
|
|
|
|
|
{
|
2024-11-13 17:50:52 +08:00
|
|
|
|
using var scope = _serviceProvider.CreateScope();
|
|
|
|
|
|
var scopedService = scope.ServiceProvider.GetRequiredService<ISendEndpointProvider>();
|
|
|
|
|
|
var endpoint = await scopedService.GetSendEndpoint(new Uri($"queue:{queueKey}"));
|
2024-10-28 16:23:39 +08:00
|
|
|
|
await endpoint.Send(message, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|