8 Commits
1.0.1 ... 1.0.5

4 changed files with 49 additions and 23 deletions

View File

@@ -3,29 +3,36 @@ using RabbitMQ.Client;
namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ;
public class RabbitMQEventQueue : EventQueue
public class RabbitMQEventQueue : EventQueue<RabbitMQEventQueue>
{
public string Hostname { get; set; } = null!;
public int Port { get; set; }
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public string ExchangeName { get; set; } = null!;
public string QueuePrefix { get; set; } = null!;
public override void Setup(IServiceCollection services)
protected override void Setup(IServiceCollection services, IDictionary<string, string> connectionParameters)
{
if (!connectionParameters.TryGetValue("Hostname", out string hostname))
{
hostname = "localhost";
}
if (!connectionParameters.TryGetValue("Port", out string port))
{
port = "5672";
}
string username = connectionParameters["Username"];
string password = connectionParameters["Password"];
string exchangeName = connectionParameters["ExchangeName"];
string queuePrefix = connectionParameters["QueuePrefix"];
ConnectionFactory factory = new ConnectionFactory
{
HostName = Hostname,
Port = Port,
UserName = Username,
Password = Password,
HostName = hostname,
Port = int.Parse(port),
UserName = username,
Password = password,
};
Task<IConnection> createConnectionTask = factory.CreateConnectionAsync();
createConnectionTask.Wait();
services.AddSingleton(createConnectionTask.Result);
services.AddSingleton<IEventQueuePublisher, RabbitMQEventQueuePublisher>(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService<IConnection>(), ExchangeName));
services.AddSingleton<IEventQueueSubscriber, RabbitMQEventQueueSubscriber>(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService<IConnection>(), ExchangeName, QueuePrefix));
services.AddSingleton<IEventQueuePublisher, RabbitMQEventQueuePublisher>(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService<IConnection>(), exchangeName));
services.AddSingleton<IEventQueueSubscriber, RabbitMQEventQueueSubscriber>(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService<IConnection>(), exchangeName, queuePrefix));
}
}

View File

@@ -0,0 +1,21 @@
namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ;
public class RabbitMQEventQueueBuilder : EventQueueBuilder<RabbitMQEventQueue>
{
public string Hostname { get; set; } = "localhost";
public int Port { get; set; } = 5672;
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public string ExchangeName { get; set; } = null!;
public string QueuePrefix { get; set; } = null!;
public override IDictionary<string, string> GetConnectionParameters() => new Dictionary<string, string>()
{
{ nameof(Hostname), Hostname },
{ nameof(Port), Port.ToString() },
{ nameof(Username), Username },
{ nameof(Password), Password },
{ nameof(ExchangeName), ExchangeName },
{ nameof(QueuePrefix), QueuePrefix }
};
}

View File

@@ -14,21 +14,19 @@ public class RabbitMQEventQueuePublisher : IEventQueuePublisher
_exchangeName = exchangeName;
}
public async Task PublishAsync<T>(T eventData) where T : class
public async Task PublishAsync(string data, Type dataType)
{
string routingKey = typeof(T).FullName!;
BasicProperties properties = new BasicProperties
{
ContentType = "application/json",
DeliveryMode = DeliveryModes.Persistent,
Type = typeof(T).FullName,
Type = dataType.FullName,
};
ReadOnlyMemory<byte> body = JsonSerializer.SerializeToUtf8Bytes(eventData);
ReadOnlyMemory<byte> body = JsonSerializer.SerializeToUtf8Bytes(data);
await using (IChannel channel = await _connection.CreateChannelAsync())
{
await channel.ExchangeDeclareAsync(_exchangeName, ExchangeType.Direct);
await channel.BasicPublishAsync(_exchangeName, routingKey, true, properties, body);
await channel.BasicPublishAsync(_exchangeName, dataType.FullName!, true, properties, body);
}
}
}

View File

@@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" />
<PackageReference Include="timetabledesigner.backend.events" Version="1.1.1" />
<PackageReference Include="timetabledesigner.backend.events" Version="1.1.4" />
</ItemGroup>
<ItemGroup>