-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from byerlikaya/refactor-and-improvements
Refactor and improvements
- Loading branch information
Showing
10 changed files
with
101 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 50 additions & 10 deletions
60
src/Basic.RabbitMQ/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,62 @@ | ||
namespace Basic.RabbitMQ.Extensions; | ||
|
||
// ReSharper disable once UnusedType.Global | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void AddRabbitMQClient(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var brokerOptions = configuration.GetSection(nameof(MessageBrokerOptions)).Get<MessageBrokerOptions>(); | ||
// ReSharper disable once UnusedMember.Global | ||
public static void AddRabbitMqClient( | ||
this IServiceCollection services, | ||
IConfiguration configuration, | ||
ServiceLifetime messageConsumerServiceLifetime = ServiceLifetime.Singleton) => | ||
CreateServices(services, configuration.GetSection(nameof(MessageBrokerOptions)).Get<MessageBrokerOptions>(), messageConsumerServiceLifetime); | ||
|
||
// ReSharper disable once UnusedMember.Global | ||
public static void AddRabbitMqClient( | ||
this IServiceCollection services, | ||
MessageBrokerOptions messageBrokerOptions, | ||
ServiceLifetime messageConsumerServiceLifetime = ServiceLifetime.Singleton) => | ||
CreateServices(services, messageBrokerOptions, messageConsumerServiceLifetime); | ||
|
||
private static void CreateServices( | ||
IServiceCollection services, | ||
MessageBrokerOptions messageBrokerOptions, | ||
ServiceLifetime messageConsumerServiceLifetime) | ||
{ | ||
services.AddSingleton(_ => new ConnectionFactory | ||
{ | ||
HostName = brokerOptions.HostName, | ||
UserName = brokerOptions.Username, | ||
Password = brokerOptions.Password, | ||
VirtualHost = brokerOptions.VirtualHost, | ||
DispatchConsumersAsync = true | ||
HostName = messageBrokerOptions.HostName, | ||
UserName = messageBrokerOptions.Username, | ||
Password = messageBrokerOptions.Password, | ||
VirtualHost = messageBrokerOptions.VirtualHost, | ||
DispatchConsumersAsync = true, | ||
AutomaticRecoveryEnabled = true, | ||
NetworkRecoveryInterval = TimeSpan.FromSeconds(30) | ||
}); | ||
|
||
services.AddSingleton<RabbitMQClientService>(); | ||
services.AddSingleton<IMessageProducer, MessageProducer>(); | ||
services.AddSingleton<RabbitMqClientService>(); | ||
services.AddSingleton<IMessageConsumer, MessageConsumer>(); | ||
|
||
CreateConsumerService(services, messageConsumerServiceLifetime); | ||
} | ||
|
||
private static void CreateConsumerService( | ||
IServiceCollection services, | ||
ServiceLifetime messageConsumerServiceLifetime) | ||
{ | ||
switch (messageConsumerServiceLifetime) | ||
{ | ||
case ServiceLifetime.Singleton: | ||
services.AddSingleton<IMessageProducer, MessageProducer>(); | ||
break; | ||
case ServiceLifetime.Scoped: | ||
services.AddScoped<IMessageProducer, MessageProducer>(); | ||
break; | ||
case ServiceLifetime.Transient: | ||
services.AddTransient<IMessageProducer, MessageProducer>(); | ||
break; | ||
default: | ||
services.AddSingleton<IMessageProducer, MessageProducer>(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,37 @@ | ||
namespace Basic.RabbitMQ.Services; | ||
|
||
// ReSharper disable once InconsistentNaming | ||
public class RabbitMQClientService(IConfiguration configuration, ConnectionFactory connectionFactory) : IDisposable | ||
public class RabbitMqClientService(IConfiguration configuration, ConnectionFactory connectionFactory) | ||
{ | ||
public readonly MessageBrokerOptions BrokerOptions = configuration.GetSection(nameof(MessageBrokerOptions)).Get<MessageBrokerOptions>(); | ||
|
||
private IConnection _connection; | ||
private IModel _channel; | ||
|
||
public IModel Connect(string queueName) | ||
{ | ||
if (_channel is { IsOpen: true } && _channel.CurrentQueue == queueName) | ||
return _channel; | ||
|
||
if (_connection is not { IsOpen: true }) | ||
_connection = connectionFactory.CreateConnection(); | ||
return CreateChannel(_connection, queueName); | ||
} | ||
|
||
_channel = _connection.CreateModel(); | ||
public IModel Connect( | ||
IConnection connection, | ||
string queueName) => CreateChannel(connection, queueName); | ||
|
||
private IModel CreateChannel(IConnection connection, string queueName) | ||
{ | ||
var channel = connection.CreateModel(); | ||
|
||
_channel.ExchangeDeclare( | ||
channel.ExchangeDeclare( | ||
exchange: BrokerOptions.ExchangeName, | ||
type: "direct", | ||
durable: true, | ||
autoDelete: false); | ||
type: ExchangeType.Direct); | ||
|
||
_channel.QueueDeclare( | ||
channel.QueueDeclare( | ||
queue: queueName, | ||
durable: true, | ||
durable: false, | ||
exclusive: false, | ||
autoDelete: false, | ||
arguments: null); | ||
|
||
return _channel; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_channel?.Close(); | ||
_channel?.Dispose(); | ||
|
||
_connection?.Close(); | ||
_connection?.Dispose(); | ||
return channel; | ||
} | ||
} |