-
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 #427 from K-Society/experimental
Added Bindings to KSociety.Base.EventBusRabbitMQ
- Loading branch information
Showing
5 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/01/KSocietyBaseEventBus/KSociety.Base.EventBusRabbitMQ/Binding/ConnectionFactory.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 |
---|---|---|
@@ -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() | ||
{ | ||
|
||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../KSocietyBaseEventBus/KSociety.Base.EventBusRabbitMQ/Binding/ExchangeDeclareParameters.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 |
---|---|---|
@@ -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() | ||
{ | ||
|
||
} | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/01/KSocietyBaseEventBus/KSociety.Base.EventBusRabbitMQ/Binding/MessageBroker.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 |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/01/KSocietyBaseEventBus/KSociety.Base.EventBusRabbitMQ/Binding/MessageBrokerOptions.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 |
---|---|---|
@@ -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() | ||
{ | ||
|
||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/01/KSocietyBaseEventBus/KSociety.Base.EventBusRabbitMQ/Binding/QueueDeclareParameters.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 |
---|---|---|
@@ -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() | ||
{ | ||
|
||
} | ||
} | ||
} |