-
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.
edited namespaces and added globalusing.
- Loading branch information
1 parent
882bbf6
commit a9b647f
Showing
9 changed files
with
137 additions
and
177 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
40 changes: 16 additions & 24 deletions
40
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,30 +1,22 @@ | ||
using Basic.RabbitMQ.Interfaces; | ||
using Basic.RabbitMQ.Options; | ||
using Basic.RabbitMQ.Services; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using RabbitMQ.Client; | ||
namespace Basic.RabbitMQ.Extensions; | ||
|
||
namespace Basic.RabbitMQ.Extensions | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static class ServiceCollectionExtensions | ||
public static void AddRabbitMQClient(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
public static void AddRabbitMQClient(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var brokerOptions = configuration.GetSection(nameof(MessageBrokerOptions)).Get<MessageBrokerOptions>(); | ||
var brokerOptions = configuration.GetSection(nameof(MessageBrokerOptions)).Get<MessageBrokerOptions>(); | ||
|
||
services.AddSingleton(_ => new ConnectionFactory | ||
{ | ||
HostName = brokerOptions.HostName, | ||
UserName = brokerOptions.Username, | ||
Password = brokerOptions.Password, | ||
VirtualHost = brokerOptions.VirtualHost, | ||
DispatchConsumersAsync = true | ||
}); | ||
services.AddSingleton(_ => new ConnectionFactory | ||
{ | ||
HostName = brokerOptions.HostName, | ||
UserName = brokerOptions.Username, | ||
Password = brokerOptions.Password, | ||
VirtualHost = brokerOptions.VirtualHost, | ||
DispatchConsumersAsync = true | ||
}); | ||
|
||
services.AddSingleton<IMessageProducer, MessageProducer>(); | ||
services.AddSingleton<IMessageConsumer, MessageConsumer>(); | ||
services.AddSingleton<RabbitMQClientService>(); | ||
} | ||
services.AddSingleton<IMessageProducer, MessageProducer>(); | ||
services.AddSingleton<IMessageConsumer, MessageConsumer>(); | ||
services.AddSingleton<RabbitMQClientService>(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Global using directives | ||
|
||
global using Basic.RabbitMQ.Interfaces; | ||
global using Basic.RabbitMQ.Options; | ||
global using Basic.RabbitMQ.Services; | ||
global using Microsoft.Extensions.Configuration; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Newtonsoft.Json; | ||
global using RabbitMQ.Client; | ||
global using RabbitMQ.Client.Events; | ||
global using System; | ||
global using System.Text; |
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,8 +1,7 @@ | ||
namespace Basic.RabbitMQ.Interfaces | ||
namespace Basic.RabbitMQ.Interfaces; | ||
|
||
public interface IMessageProducer | ||
{ | ||
public interface IMessageProducer | ||
{ | ||
void SendMessage(string queueName, string routingKey, string message); | ||
void SendMessage<T>(string queueName, string routingKey, T message); | ||
} | ||
} | ||
void SendMessage(string queueName, string routingKey, string message); | ||
void SendMessage<T>(string queueName, string routingKey, T message); | ||
} |
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,43 +1,30 @@ | ||
using Basic.RabbitMQ.Interfaces; | ||
using Basic.RabbitMQ.Services; | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Events; | ||
namespace Basic.RabbitMQ; | ||
|
||
namespace Basic.RabbitMQ | ||
public class MessageConsumer(RabbitMQClientService rabbitMqClientService) : IMessageConsumer | ||
{ | ||
public class MessageConsumer : IMessageConsumer | ||
public IModel Channel(string queueName, string routingKey) | ||
{ | ||
private readonly RabbitMQClientService _rabbitMqClientService; | ||
var channel = rabbitMqClientService.Connect(queueName); | ||
|
||
public MessageConsumer(RabbitMQClientService rabbitMqClientService) | ||
{ | ||
_rabbitMqClientService = rabbitMqClientService; | ||
} | ||
channel.QueueBind( | ||
exchange: rabbitMqClientService.BrokerOptions.ExchangeName, | ||
queue: queueName, | ||
routingKey: routingKey); | ||
|
||
public IModel Channel(string queueName, string routingKey) | ||
{ | ||
var channel = _rabbitMqClientService.Connect(queueName); | ||
channel.BasicQos(0, 10, false); | ||
|
||
channel.QueueBind( | ||
exchange: _rabbitMqClientService.BrokerOptions.ExchangeName, | ||
queue: queueName, | ||
routingKey: routingKey); | ||
|
||
channel.BasicQos(0, 10, false); | ||
|
||
return channel; | ||
} | ||
return channel; | ||
} | ||
|
||
public AsyncEventingBasicConsumer GetConsumer(IModel channel) | ||
{ | ||
var consumer = new AsyncEventingBasicConsumer(channel); | ||
public AsyncEventingBasicConsumer GetConsumer(IModel channel) | ||
{ | ||
var consumer = new AsyncEventingBasicConsumer(channel); | ||
|
||
channel.BasicConsume( | ||
queue: channel.CurrentQueue, | ||
autoAck: false, | ||
consumer: consumer); | ||
channel.BasicConsume( | ||
queue: channel.CurrentQueue, | ||
autoAck: false, | ||
consumer: consumer); | ||
|
||
return consumer; | ||
} | ||
return consumer; | ||
} | ||
} | ||
} |
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,63 +1,49 @@ | ||
using Basic.RabbitMQ.Interfaces; | ||
using Basic.RabbitMQ.Services; | ||
using Newtonsoft.Json; | ||
using RabbitMQ.Client; | ||
using System.Text; | ||
namespace Basic.RabbitMQ; | ||
|
||
namespace Basic.RabbitMQ | ||
public class MessageProducer(RabbitMQClientService rabbitMqClientService) : IMessageProducer | ||
{ | ||
public class MessageProducer : IMessageProducer | ||
public void SendMessage(string queueName, string routingKey, string message) | ||
{ | ||
private readonly RabbitMQClientService _rabbitMqClientService; | ||
var channel = rabbitMqClientService.Connect(queueName); | ||
|
||
public MessageProducer(RabbitMQClientService rabbitMqClientService) | ||
{ | ||
_rabbitMqClientService = rabbitMqClientService; | ||
} | ||
channel.QueueBind( | ||
exchange: rabbitMqClientService.BrokerOptions.ExchangeName, | ||
queue: queueName, | ||
routingKey: routingKey); | ||
|
||
public void SendMessage(string queueName, string routingKey, string message) | ||
{ | ||
var channel = _rabbitMqClientService.Connect(queueName); | ||
var json = JsonConvert.SerializeObject(message); | ||
var body = Encoding.UTF8.GetBytes(json); | ||
|
||
channel.QueueBind( | ||
exchange: _rabbitMqClientService.BrokerOptions.ExchangeName, | ||
queue: queueName, | ||
routingKey: routingKey); | ||
var properties = channel.CreateBasicProperties(); | ||
properties.Persistent = true; | ||
|
||
var json = JsonConvert.SerializeObject(message); | ||
var body = Encoding.UTF8.GetBytes(json); | ||
|
||
var properties = channel.CreateBasicProperties(); | ||
properties.Persistent = true; | ||
|
||
channel.BasicPublish( | ||
exchange: _rabbitMqClientService.BrokerOptions.ExchangeName, | ||
routingKey: routingKey, | ||
basicProperties: properties, | ||
body: body); | ||
} | ||
channel.BasicPublish( | ||
exchange: rabbitMqClientService.BrokerOptions.ExchangeName, | ||
routingKey: routingKey, | ||
basicProperties: properties, | ||
body: body); | ||
} | ||
|
||
public void SendMessage<T>(string queueName, string routingKey, T message) | ||
{ | ||
var channel = _rabbitMqClientService.Connect(queueName); | ||
public void SendMessage<T>(string queueName, string routingKey, T message) | ||
{ | ||
var channel = rabbitMqClientService.Connect(queueName); | ||
|
||
channel.QueueBind( | ||
exchange: _rabbitMqClientService.BrokerOptions.ExchangeName, | ||
queue: queueName, | ||
routingKey: routingKey); | ||
channel.QueueBind( | ||
exchange: rabbitMqClientService.BrokerOptions.ExchangeName, | ||
queue: queueName, | ||
routingKey: routingKey); | ||
|
||
var json = JsonConvert.SerializeObject(message); | ||
var body = Encoding.UTF8.GetBytes(json); | ||
var json = JsonConvert.SerializeObject(message); | ||
var body = Encoding.UTF8.GetBytes(json); | ||
|
||
var properties = channel.CreateBasicProperties(); | ||
var properties = channel.CreateBasicProperties(); | ||
|
||
properties.Persistent = true; | ||
properties.Persistent = true; | ||
|
||
channel.BasicPublish( | ||
exchange: _rabbitMqClientService.BrokerOptions.ExchangeName, | ||
routingKey: routingKey, | ||
basicProperties: properties, | ||
body: body); | ||
} | ||
channel.BasicPublish( | ||
exchange: rabbitMqClientService.BrokerOptions.ExchangeName, | ||
routingKey: routingKey, | ||
basicProperties: properties, | ||
body: body); | ||
} | ||
} | ||
} |
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,11 +1,10 @@ | ||
namespace Basic.RabbitMQ.Options | ||
namespace Basic.RabbitMQ.Options; | ||
|
||
public class MessageBrokerOptions | ||
{ | ||
public class MessageBrokerOptions | ||
{ | ||
public string HostName { get; set; } | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
public string ExchangeName { get; set; } | ||
public string VirtualHost { get; set; } | ||
} | ||
} | ||
public string HostName { get; set; } | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
public string ExchangeName { get; set; } | ||
public string VirtualHost { get; set; } | ||
} |
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,57 +1,45 @@ | ||
using Basic.RabbitMQ.Options; | ||
using Microsoft.Extensions.Configuration; | ||
using RabbitMQ.Client; | ||
using System; | ||
namespace Basic.RabbitMQ.Services; | ||
|
||
namespace Basic.RabbitMQ.Services | ||
public class RabbitMQClientService(IConfiguration configuration, ConnectionFactory connectionFactory) | ||
: IDisposable | ||
{ | ||
public class RabbitMQClientService : IDisposable | ||
{ | ||
public readonly MessageBrokerOptions BrokerOptions; | ||
|
||
private readonly ConnectionFactory _connectionFactory; | ||
private IConnection _connection; | ||
private IModel _channel; | ||
public readonly MessageBrokerOptions BrokerOptions = configuration.GetSection(nameof(MessageBrokerOptions)).Get<MessageBrokerOptions>(); | ||
|
||
public RabbitMQClientService(IConfiguration configuration, ConnectionFactory connectionFactory) | ||
{ | ||
_connectionFactory = connectionFactory; | ||
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; | ||
public IModel Connect(string queueName) | ||
{ | ||
if (_channel is { IsOpen: true } && _channel.CurrentQueue == queueName) | ||
return _channel; | ||
|
||
if (_connection is not { IsOpen: true }) | ||
_connection = _connectionFactory.CreateConnection(); | ||
if (_connection is not { IsOpen: true }) | ||
_connection = connectionFactory.CreateConnection(); | ||
|
||
_channel = _connection.CreateModel(); | ||
_channel = _connection.CreateModel(); | ||
|
||
_channel.ExchangeDeclare( | ||
exchange: BrokerOptions.ExchangeName, | ||
type: "direct", | ||
durable: true, | ||
autoDelete: false); | ||
_channel.ExchangeDeclare( | ||
exchange: BrokerOptions.ExchangeName, | ||
type: "direct", | ||
durable: true, | ||
autoDelete: false); | ||
|
||
_channel.QueueDeclare( | ||
queue: queueName, | ||
durable: true, | ||
exclusive: false, | ||
autoDelete: false, | ||
arguments: null); | ||
_channel.QueueDeclare( | ||
queue: queueName, | ||
durable: true, | ||
exclusive: false, | ||
autoDelete: false, | ||
arguments: null); | ||
|
||
return _channel; | ||
} | ||
return _channel; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_channel?.Close(); | ||
_channel?.Dispose(); | ||
public void Dispose() | ||
{ | ||
_channel?.Close(); | ||
_channel?.Dispose(); | ||
|
||
_connection?.Close(); | ||
_connection?.Dispose(); | ||
} | ||
_connection?.Close(); | ||
_connection?.Dispose(); | ||
} | ||
} | ||
} |