Skip to content

Commit

Permalink
Merge pull request #427 from K-Society/experimental
Browse files Browse the repository at this point in the history
Added Bindings to KSociety.Base.EventBusRabbitMQ
  • Loading branch information
maniglia authored Mar 1, 2024
2 parents 6e6e09d + 3c56118 commit fce04b3
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace KSociety.Base.EventBusRabbitMQ.Binding
{
public class ConnectionFactory
{
public string MqHostName { get; set; } = "localhost";
public string MqUserName { get; set; } = "KSociety";
public string MqPassword { get; set; } = "KSociety";

public ConnectionFactory()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace KSociety.Base.EventBusRabbitMQ.Binding
{
public class ExchangeDeclareParameters
{
public string BrokerName { get; set; }
public EventBus.ExchangeType ExchangeType { get; set; } = EventBus.ExchangeType.Direct;
public bool ExchangeDurable { get; set; } = false;
public bool ExchangeAutoDelete { get; set; } = true;

public ExchangeDeclareParameters()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
namespace KSociety.Base.EventBusRabbitMQ.Binding
{
using System;
using Autofac;
using EventBus;
using EventBusRabbitMQ;
using Helper;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;

/// <summary>
/// The MessageBroker module for Autofac.
/// </summary>
public class MessageBroker<
TExchangeDeclareParameters,
TQueueDeclareParameters,
TEventBusParameters,
TConnectionFactory,
TSubscriber,
TExchangeDeclareParametersClass,
TQueueDeclareParametersClass,
TEventBusParametersClass,
TSubscriberClass> : Module
where TExchangeDeclareParameters : IExchangeDeclareParameters
where TQueueDeclareParameters : IQueueDeclareParameters
where TEventBusParameters : IEventBusParameters
where TConnectionFactory : IConnectionFactory
where TSubscriber : ISubscriber
where TExchangeDeclareParametersClass : EventBus.ExchangeDeclareParameters, new()
where TQueueDeclareParametersClass : EventBus.QueueDeclareParameters, new()
where TEventBusParametersClass : EventBusParameters, new()
where TSubscriberClass : Subscriber
{
private readonly bool _debug;
private readonly int _eventBusNumber;
private readonly string _brokerName;
private readonly EventBus.ExchangeType _exchangeType;
private readonly bool _exchangeDurable;
private readonly bool _exchangeAutoDelete;
private readonly bool _queueDurable;
private readonly bool _queueExclusive;
private readonly bool _queueAutoDelete;

private readonly string _mqHostName;
private readonly string _mqUserName;
private readonly string _mqPassword;

public MessageBroker(
int eventBusNumber,
string brokerName, EventBus.ExchangeType exchangeType,
bool exchangeDurable, bool exchangeAutoDelete,
string mqHostName, string mqUserName, string mqPassword, bool debug,
bool queueDurable,
bool queueExclusive,
bool queueAutoDelete)
{
this._debug = debug;
this._eventBusNumber = eventBusNumber;
this._brokerName = brokerName;
this._exchangeType = exchangeType;
this._exchangeDurable = exchangeDurable;
this._exchangeAutoDelete = exchangeAutoDelete;

this._queueDurable = queueDurable;
this._queueExclusive = queueExclusive;
this._queueAutoDelete = queueAutoDelete;

this._mqHostName = mqHostName;
this._mqUserName = mqUserName;
this._mqPassword = mqPassword;
}

public MessageBroker(MessageBrokerOptions messageBroker, bool debug = false)
{
this._debug = debug;
this._eventBusNumber = messageBroker.EventBusNumber;
this._brokerName = messageBroker.ExchangeDeclareParameters.BrokerName;
this._exchangeType = messageBroker.ExchangeDeclareParameters.ExchangeType;
this._exchangeDurable = messageBroker.ExchangeDeclareParameters.ExchangeDurable;
this._exchangeAutoDelete = messageBroker.ExchangeDeclareParameters.ExchangeAutoDelete;

this._queueDurable = messageBroker.QueueDeclareParameters.QueueDurable;
this._queueExclusive = messageBroker.QueueDeclareParameters.QueueExclusive;
this._queueAutoDelete = messageBroker.QueueDeclareParameters.QueueAutoDelete;

this._mqHostName = messageBroker.ConnectionFactory.MqHostName;
this._mqUserName = messageBroker.ConnectionFactory.MqUserName;
this._mqPassword = messageBroker.ConnectionFactory.MqPassword;
}

protected override void Load(ContainerBuilder builder)
{
var exchangeDeclareParameters = new TExchangeDeclareParametersClass
{
BrokerName = this._brokerName,
ExchangeType = this._exchangeType.ToString().ToLower(),
ExchangeDurable = this._exchangeDurable,
ExchangeAutoDelete = this._exchangeAutoDelete
};

var queueDeclareParameters = new TQueueDeclareParametersClass
{
QueueDurable = this._queueDurable, QueueExclusive = this._queueExclusive, QueueAutoDelete = this._queueAutoDelete
};

var eventBusParameters = new TEventBusParametersClass
{
ExchangeDeclareParameters = exchangeDeclareParameters,
QueueDeclareParameters = queueDeclareParameters,
Debug = this._debug
};

var rabbitMqConnectionFactory = new RabbitMQ.Client.ConnectionFactory
{
HostName = this._mqHostName,
UserName = this._mqUserName,
Password = this._mqPassword,
AutomaticRecoveryEnabled = true,
NetworkRecoveryInterval = TimeSpan.FromSeconds(10),
RequestedHeartbeat = TimeSpan.FromSeconds(10),
ContinuationTimeout = TimeSpan.FromSeconds(120),
DispatchConsumersAsync = true
};

builder.RegisterInstance(exchangeDeclareParameters).As<TExchangeDeclareParameters>().SingleInstance();
builder.RegisterInstance(queueDeclareParameters).As<TQueueDeclareParameters>().SingleInstance();
builder.RegisterInstance(eventBusParameters).As<TEventBusParameters>().As<IEventBusParameters>().SingleInstance();
builder.RegisterInstance(rabbitMqConnectionFactory).As<TConnectionFactory>().SingleInstance();
builder.RegisterType<DefaultRabbitMqPersistentConnection>().As<IRabbitMqPersistentConnection>().UsingConstructor(typeof(TConnectionFactory), typeof(ILogger<DefaultRabbitMqPersistentConnection>))
.SingleInstance();

builder.RegisterType<TSubscriberClass>().UsingConstructor(typeof(IRabbitMqPersistentConnection), typeof(IEventBusParameters), typeof(int), typeof(ILogger<EventBusRabbitMq>)).WithParameter("eventBusNumber", this._eventBusNumber).As<TSubscriber>().SingleInstance();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace KSociety.Base.EventBusRabbitMQ.Binding
{
public class MessageBrokerOptions
{
public int EventBusNumber { get; set; }
public ConnectionFactory ConnectionFactory { get; set; }
public ExchangeDeclareParameters ExchangeDeclareParameters { get; set; }
public QueueDeclareParameters QueueDeclareParameters { get; set; }

public MessageBrokerOptions()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace KSociety.Base.EventBusRabbitMQ.Binding
{
public class QueueDeclareParameters
{
public bool QueueDurable { get; set; } = false;
public bool QueueExclusive { get; set; } = false;
public bool QueueAutoDelete { get; set; } = true;

public QueueDeclareParameters()
{

}
}
}

0 comments on commit fce04b3

Please sign in to comment.