53 lines
1.1 KiB
C#
53 lines
1.1 KiB
C#
using Microsoft.Extensions.ObjectPool;
|
|
using RabbitMQ.Client;
|
|
|
|
namespace BMA.EHR.Leave.Service.Services;
|
|
|
|
public class RabbitMqConnectionPoolPolicy : IPooledObjectPolicy<IModel>
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly IConnection _connection;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
public RabbitMqConnectionPoolPolicy(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
|
|
var factory = new ConnectionFactory()
|
|
{
|
|
HostName = _configuration["Rabbit:Host"],
|
|
UserName = _configuration["Rabbit:User"],
|
|
Password = _configuration["Rabbit:Password"]
|
|
};
|
|
_connection = factory.CreateConnection();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
public IModel Create()
|
|
{
|
|
return _connection.CreateModel();
|
|
}
|
|
|
|
public bool Return(IModel obj)
|
|
{
|
|
if (obj.IsOpen)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
obj.Dispose();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|