Compare commits
9
Commits
699fc721f5
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8336e283e | ||
|
|
8d2d13ff3b | ||
|
|
06c92f5eeb | ||
|
|
079048dff5 | ||
|
|
28e679235c | ||
|
|
e05420b1aa | ||
|
|
1e0641643d | ||
|
|
b20c2f8db7 | ||
|
|
3e0bb2c25f |
@@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using xEventService.Models;
|
||||||
using RabbitMQ.Client;
|
|
||||||
using xEventService.Constants;
|
using xEventService.Constants;
|
||||||
|
|
||||||
namespace xEventService.Configuration
|
namespace xEventService.Configuration
|
||||||
@@ -24,5 +23,17 @@ namespace xEventService.Configuration
|
|||||||
/// Allowed Message Broker ...
|
/// Allowed Message Broker ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public XEventBroker Broker { get; set; } = XEventBroker.None;
|
public XEventBroker Broker { get; set; } = XEventBroker.None;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ZeroMQ Specific Options ...
|
||||||
|
/// Only used when Broker is XZeroMQ ...
|
||||||
|
/// </summary>
|
||||||
|
public XZeroMQOptions ZeroMQ { get; set; } = new XZeroMQOptions();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RabbitMQ Specific Options ...
|
||||||
|
/// Only used when Broker is XRabbitMQ ...
|
||||||
|
/// </summary>
|
||||||
|
public XRabbitMQOptions RabbitMQ { get; set; } = new XRabbitMQOptions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,13 +7,16 @@ namespace xEventService.Constants
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public enum XEventBroker
|
public enum XEventBroker
|
||||||
{
|
{
|
||||||
[StringValue("None")]
|
[StringValue(XEventServiceConstants.None)]
|
||||||
None,
|
None,
|
||||||
|
|
||||||
[StringValue("XKafka")]
|
[StringValue(XEventServiceConstants.XKafka)]
|
||||||
XKafka,
|
XKafka,
|
||||||
|
|
||||||
[StringValue("XRabbitMQ")]
|
[StringValue(XEventServiceConstants.XZeroMQ)]
|
||||||
XRabbitMQ
|
XZeroMQ,
|
||||||
|
|
||||||
|
[StringValue(XEventServiceConstants.XRabbitMQ)]
|
||||||
|
XRabbitMQ,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,5 +9,17 @@ namespace xEventService.Constants
|
|||||||
//
|
//
|
||||||
// Defaults ...
|
// Defaults ...
|
||||||
public const string XDefaultTopic = "XSaherelm";
|
public const string XDefaultTopic = "XSaherelm";
|
||||||
|
public const string XDefaultConsumerGroup = "XSaherElmGroup";
|
||||||
|
|
||||||
|
//
|
||||||
|
public const string None = "None";
|
||||||
|
public const string XKafka = "XKafka";
|
||||||
|
public const string PubSub = "PubSub";
|
||||||
|
public const string XZeroMQ = "XZeroMQ";
|
||||||
|
public const string PushPull = "PushPull";
|
||||||
|
public const string XRabbitMQ = "XRabbitMQ";
|
||||||
|
|
||||||
|
//
|
||||||
|
public const string XWildcard = "*";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using xExceptions.Attributes;
|
||||||
|
|
||||||
|
namespace xEventService.Constants
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Available RabbitMQ Exchange Types ...
|
||||||
|
/// </summary>
|
||||||
|
public struct XRabbitMQExchangeTypes
|
||||||
|
{
|
||||||
|
public const string None = "none";
|
||||||
|
public const string Direct = "direct";
|
||||||
|
public const string Fanout = "fanout";
|
||||||
|
public const string Topic = "topic";
|
||||||
|
public const string Headers = "headers";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RabbitMQ Exchange Type Enum ...
|
||||||
|
/// </summary>
|
||||||
|
public enum XRabbitMQExchangeType
|
||||||
|
{
|
||||||
|
[StringValue(XRabbitMQExchangeTypes.None)]
|
||||||
|
None,
|
||||||
|
|
||||||
|
[StringValue(XRabbitMQExchangeTypes.Direct)]
|
||||||
|
Direct,
|
||||||
|
|
||||||
|
[StringValue(XRabbitMQExchangeTypes.Fanout)]
|
||||||
|
Fanout,
|
||||||
|
|
||||||
|
[StringValue(XRabbitMQExchangeTypes.Topic)]
|
||||||
|
Topic,
|
||||||
|
|
||||||
|
[StringValue(XRabbitMQExchangeTypes.Headers)]
|
||||||
|
Headers,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using xExceptions.Attributes;
|
||||||
|
|
||||||
|
namespace xEventService.Constants
|
||||||
|
{
|
||||||
|
public enum XZeroMQPattern
|
||||||
|
{
|
||||||
|
[StringValue(XEventServiceConstants.None)]
|
||||||
|
None,
|
||||||
|
|
||||||
|
[StringValue(XEventServiceConstants.PubSub)]
|
||||||
|
PubSub,
|
||||||
|
|
||||||
|
[StringValue(XEventServiceConstants.PushPull)]
|
||||||
|
PushPull
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-10
@@ -1,11 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xEventService.Configuration;
|
using xExceptions.Constants;
|
||||||
using xEventService.Constants;
|
using xEventService.Constants;
|
||||||
using xEventService.Extensions;
|
using xEventService.Extensions;
|
||||||
using xExceptions.Constants;
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace xEventService.DI
|
namespace xEventService.DI
|
||||||
{
|
{
|
||||||
@@ -16,7 +16,7 @@ namespace xEventService.DI
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="source"></param>
|
/// <param name="source"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static XEventServiceConfiguration GetXEventServiceConfigurations(
|
public static XEventServiceConfiguration GetXEventServiceConfiguration(
|
||||||
this IConfiguration source
|
this IConfiguration source
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -34,14 +34,14 @@ namespace xEventService.DI
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
/// <param name="configuration"></param>
|
/// <param name="configuration"></param>
|
||||||
public static void AddXEventServiceConfigurations(
|
public static void AddXEventServiceConfiguration(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
IConfiguration configuration
|
IConfiguration configuration
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var config = configuration.GetXEventServiceConfigurations();
|
var config = configuration.GetXEventServiceConfiguration();
|
||||||
services.AddXEventServiceConfigurations(config);
|
services.AddXEventServiceConfiguration(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -49,14 +49,20 @@ namespace xEventService.DI
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
/// <param name="configuration"></param>
|
/// <param name="configuration"></param>
|
||||||
public static void AddXEventServiceConfigurations(
|
public static void AddXEventServiceConfiguration(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
XEventServiceConfiguration configuration
|
XEventServiceConfiguration configuration
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// Validate ...
|
// Validate ...
|
||||||
if (!configuration.IsValid())
|
var isValid = configuration.IsValid() &&
|
||||||
|
(configuration.Broker == XEventBroker.XKafka ||
|
||||||
|
(configuration.Broker == XEventBroker.XZeroMQ &&
|
||||||
|
configuration.ZeroMQ.IsValid()) ||
|
||||||
|
(configuration.Broker == XEventBroker.XRabbitMQ &&
|
||||||
|
configuration.RabbitMQ.IsValid()));
|
||||||
|
if (!isValid)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
Log("Registration Failed, due Invalid Configuration ...");
|
Log("Registration Failed, due Invalid Configuration ...");
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
using System.Linq;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xEventService.Configuration;
|
|
||||||
using xEventService.Constants;
|
|
||||||
using xEventService.Models;
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
|
||||||
namespace xEventService.Extensions
|
namespace xEventService.Extensions
|
||||||
{
|
{
|
||||||
@@ -44,6 +46,37 @@ namespace xEventService.Extensions
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate RabbitMQ Exchange Type ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(this XRabbitMQExchangeType source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source != XRabbitMQExchangeType.None;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate RabbitMQ Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(this XRabbitMQOptions source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Exchange.IsNullOrEmpty() &&
|
||||||
|
source.ExchangeType.IsValid();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validate an Event Message ...
|
/// Validate an Event Message ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -77,5 +110,235 @@ namespace xEventService.Extensions
|
|||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate ZeroMQ Pattern ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(this XZeroMQPattern source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source != XZeroMQPattern.None;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate ZeroMQ Options ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(this XZeroMQOptions source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
source.Pattern.IsValid();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate an Array is Wildcard passed or not ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsAllowedWildcard(this string[] source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
source is not null &&
|
||||||
|
source.Length == 1 &&
|
||||||
|
source.Any(s =>
|
||||||
|
s == XEventServiceConstants.XWildcard);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate a Collection is Valid for Kafka Consumer ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsAllowedCollection(
|
||||||
|
this string[] source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source.HasChild();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Sender ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="allowedSenders"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidSender(
|
||||||
|
this XEventMessage source,
|
||||||
|
string[] allowedSenders
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Source and Collection ...
|
||||||
|
var result =
|
||||||
|
source.IsValid() &&
|
||||||
|
allowedSenders
|
||||||
|
.IsAllowedCollection();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Sender ...
|
||||||
|
result =
|
||||||
|
allowedSenders.IsAllowedWildcard() ||
|
||||||
|
allowedSenders.Any(x =>
|
||||||
|
x.Equals(
|
||||||
|
source.Sender,
|
||||||
|
System.StringComparison.InvariantCultureIgnoreCase));
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Action ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="allowedActions"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidAction(
|
||||||
|
this XEventMessage source,
|
||||||
|
string[] allowedActions
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Source and Collection ...
|
||||||
|
var result =
|
||||||
|
source.IsValid() &&
|
||||||
|
allowedActions
|
||||||
|
.IsAllowedCollection();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Action ...
|
||||||
|
result =
|
||||||
|
allowedActions.IsAllowedWildcard() ||
|
||||||
|
allowedActions.Any(x =>
|
||||||
|
x.Equals(
|
||||||
|
source.Action,
|
||||||
|
System.StringComparison.InvariantCultureIgnoreCase));
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Completely Validate a Message, based on providing:
|
||||||
|
/// - Allowed Actions
|
||||||
|
/// - Allowed Senders
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="allowedSenders"></param>
|
||||||
|
/// <param name="allowedActions"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValid(
|
||||||
|
this XEventMessage source,
|
||||||
|
string[] allowedSenders,
|
||||||
|
string[] allowedActions
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
source.IsValid() &&
|
||||||
|
source.IsValidAction(allowedActions) &&
|
||||||
|
source.IsValidSender(allowedSenders);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse Url and Extract Host address ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string GetRabbitMQHost(this XEventServiceConfiguration source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = string.Empty;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!source.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var parts = source.Url.Split(':');
|
||||||
|
if (parts.Length != 2)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = parts[0];
|
||||||
|
if (result.IsNullOrEmpty() ||
|
||||||
|
!result.IsValidUrl()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse Url and Extract Port Number ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static int GetRabbitMQPort(this XEventServiceConfiguration source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
int result = -1;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!source.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var parts = source.Url.Split(':');
|
||||||
|
if (parts.Length != 2)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Try Parse Port as Integer ...
|
||||||
|
int.TryParse(parts[1], out result);
|
||||||
|
if (result <= 0)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace xEventService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXEventServiceProvider : IXProducerServiceBase
|
||||||
|
{ }
|
||||||
|
}
|
||||||
@@ -1,35 +1,8 @@
|
|||||||
using xEventService.Models;
|
|
||||||
|
|
||||||
namespace xEventService.Interfaces
|
namespace xEventService.Interfaces
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// a Service for Produce Kafka Messages ...
|
/// a Service for Produce Kafka Messages ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IXKafkaProducerService : IDisposable
|
public interface IXKafkaProducerService : IXProducerServiceBase
|
||||||
{
|
{ }
|
||||||
/// <summary>
|
|
||||||
/// Sending Message ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request">an Instance of <see cref="XEventRequest"/> to Provides Kafka Messaging requirement ...</param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<bool> SendMessageAsync(
|
|
||||||
XEventRequest request,
|
|
||||||
string topic = XEventServiceConstants.XDefaultTopic,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sending a Message ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="message"></param>
|
|
||||||
/// <param name="topic"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<bool> SendMessageAsync(
|
|
||||||
XEventMessage message,
|
|
||||||
string topic = XEventServiceConstants.XDefaultTopic,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using xEventService.Models;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
|
|
||||||
|
namespace xEventService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXProducerServiceBase : IDisposable
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Sending Message ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">an Instance of <see cref="XEventRequest"/> to Provides Kafka Messaging requirement ...</param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> SendMessageAsync(
|
||||||
|
XEventRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending a Message ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">an Instance of <see cref="XEventMessage"/> to Provides Kafka Messaging requirement ...</param>
|
||||||
|
/// <param name="topic">Topic for Sending Message ...</param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using xEventService.Models;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace xEventService.Interfaces
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// RabbitMQ Producer Service Implementation ...
|
||||||
|
/// </summary>
|
||||||
|
public interface IXRabbitMQProducerService : IXProducerServiceBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Sending a Message to Default Exchange ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">
|
||||||
|
/// an Instance of <see cref="XEventMessage"/> ...
|
||||||
|
/// </param>
|
||||||
|
/// <param name="exchange">
|
||||||
|
/// Target Exchange Name. if empty, uses default from configuration ...
|
||||||
|
/// </param>
|
||||||
|
/// <param name="routingKey">
|
||||||
|
/// Routing Key for message routing ...
|
||||||
|
/// </param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string exchange = null,
|
||||||
|
string routingKey = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace xEventService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXZeroMQProducerService : IXProducerServiceBase
|
||||||
|
{ }
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace xEventService.Models
|
namespace xEventService.Models
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using xEventService.Constants;
|
||||||
|
|
||||||
namespace xEventService.Models
|
namespace xEventService.Models
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
using xEventService.Constants;
|
||||||
|
|
||||||
|
namespace xEventService.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// RabbitMQ-specific connection and messaging options ...
|
||||||
|
/// </summary>
|
||||||
|
public class XRabbitMQOptions
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Connection Username ...
|
||||||
|
/// </summary>
|
||||||
|
public string Username { get; set; } = "guest";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Connection Password ...
|
||||||
|
/// </summary>
|
||||||
|
public string Password { get; set; } = "guest";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Virtual Host ...
|
||||||
|
/// </summary>
|
||||||
|
public string VirtualHost { get; set; } = "/";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Exchange Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string Exchange { get; set; } = "x.saherelm.events";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Exchange Type (fanout, direct, topic, headers) ...
|
||||||
|
/// </summary>
|
||||||
|
public XRabbitMQExchangeType ExchangeType { get; set; } = XRabbitMQExchangeType.Fanout;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Queue Name ...
|
||||||
|
/// if empty, a dynamic queue will be generated ...
|
||||||
|
/// </summary>
|
||||||
|
public string Queue { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Routing Key ...
|
||||||
|
/// </summary>
|
||||||
|
public string RoutingKey { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Exchange Durable Flag ...
|
||||||
|
/// </summary>
|
||||||
|
public bool ExchangeDurable { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Exchange AutoDelete Flag ...
|
||||||
|
/// </summary>
|
||||||
|
public bool ExchangeAutoDelete { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue Durable Flag ...
|
||||||
|
/// </summary>
|
||||||
|
public bool QueueDurable { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue Exclusive Flag ...
|
||||||
|
/// </summary>
|
||||||
|
public bool QueueExclusive { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue AutoDelete Flag ...
|
||||||
|
/// </summary>
|
||||||
|
public bool QueueAutoDelete { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enable Automatic Recovery on Connection Loss ...
|
||||||
|
/// </summary>
|
||||||
|
public bool AutomaticRecoveryEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Network Recovery Interval in Seconds ...
|
||||||
|
/// </summary>
|
||||||
|
public int NetworkRecoveryIntervalSeconds { get; set; } = 5;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prefetch Count for Consumer ...
|
||||||
|
/// </summary>
|
||||||
|
public ushort PrefetchCount { get; set; } = 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enable Manual Acknowledgement ...
|
||||||
|
/// </summary>
|
||||||
|
public bool ManualAck { get; set; } = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using xEventService.Constants;
|
||||||
|
|
||||||
|
namespace xEventService.Models
|
||||||
|
{
|
||||||
|
public class XZeroMQOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Socket Pattern (PubSub, PushPull) ...
|
||||||
|
/// </summary>
|
||||||
|
public XZeroMQPattern Pattern { get; set; } = XZeroMQPattern.PubSub;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bind or Connect ...
|
||||||
|
/// True for Server (Bind), False for Client (Connect) ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsServer { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// High Water Mark for Sending ...
|
||||||
|
/// </summary>
|
||||||
|
public int SendHighWaterMark { get; set; } = 1000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// High Water Mark for Receiving ...
|
||||||
|
/// </summary>
|
||||||
|
public int ReceiveHighWaterMark { get; set; } = 1000;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using System;
|
||||||
|
using xCommons.Providers;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Base Event Consumer Service ...
|
||||||
|
/// </summary>
|
||||||
|
public abstract class XBaseEventConsumerBackgroundService : XResumableBackgroundService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Consumed Topic Messages ...
|
||||||
|
/// </summary>
|
||||||
|
protected readonly string topic;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Allowed Senders ...
|
||||||
|
/// </summary>
|
||||||
|
protected readonly string[] allowedSenders;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Allowed Actions ...
|
||||||
|
/// </summary>
|
||||||
|
protected readonly string[] allowedActions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
protected readonly XEventServiceConfiguration configuration;
|
||||||
|
|
||||||
|
protected XBaseEventConsumerBackgroundService(
|
||||||
|
string[] allowedActions,
|
||||||
|
string[] allowedSenders,
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
ILogger<XResumableBackgroundService> logger,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic
|
||||||
|
) : base(serviceProvider, logger)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Actions ...
|
||||||
|
if (!allowedActions
|
||||||
|
.IsAllowedCollection() ||
|
||||||
|
!allowedSenders
|
||||||
|
.IsAllowedCollection() ||
|
||||||
|
!configuration.IsValid()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
this.topic = topic;
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.allowedActions = allowedActions;
|
||||||
|
this.allowedSenders = allowedSenders;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
public abstract class XBaseEventProducerService
|
||||||
|
{
|
||||||
|
protected readonly XEventServiceConfiguration configuration;
|
||||||
|
protected readonly ILogger<XBaseEventProducerService> logger;
|
||||||
|
|
||||||
|
protected XBaseEventProducerService(
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
ILogger<XBaseEventProducerService> logger
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.logger = logger;
|
||||||
|
this.configuration = configuration;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Configurations ...
|
||||||
|
if (!configuration.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Interfaces;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
public class XEventServiceProvider : IXEventServiceProvider
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private readonly XEventServiceConfiguration configuration;
|
||||||
|
private readonly IXKafkaProducerService kafkaProducerService;
|
||||||
|
private readonly IXZeroMQProducerService zeroMQProducerService;
|
||||||
|
private readonly IXRabbitMQProducerService rabbitMQProducerService;
|
||||||
|
|
||||||
|
public XEventServiceProvider(
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
IXKafkaProducerService kafkaProducerService = null,
|
||||||
|
IXZeroMQProducerService zeroMQProducerService = null,
|
||||||
|
IXRabbitMQProducerService rabbitMQProducerService = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.kafkaProducerService = kafkaProducerService;
|
||||||
|
this.zeroMQProducerService = zeroMQProducerService;
|
||||||
|
this.rabbitMQProducerService = rabbitMQProducerService;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
var isValid = configuration.IsValid() &&
|
||||||
|
configuration.Broker == XEventBroker.XKafka
|
||||||
|
? kafkaProducerService != null
|
||||||
|
: configuration.Broker == XEventBroker.XZeroMQ
|
||||||
|
? zeroMQProducerService != null
|
||||||
|
: configuration.Broker == XEventBroker.XRabbitMQ
|
||||||
|
? rabbitMQProducerService != null
|
||||||
|
: false;
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send Message to Broker Using XEventRequest instance ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await SendMessageAsync(
|
||||||
|
topic: request.Topic,
|
||||||
|
message: request.Message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send Message to Broker Using XMessage instance ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <param name="topic"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
if (!message.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = false;
|
||||||
|
switch (configuration.Broker)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
case XEventBroker.XKafka:
|
||||||
|
result = await kafkaProducerService.SendMessageAsync(
|
||||||
|
topic: topic,
|
||||||
|
message: message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case XEventBroker.XZeroMQ:
|
||||||
|
result = await zeroMQProducerService.SendMessageAsync(
|
||||||
|
topic: topic,
|
||||||
|
message: message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case XEventBroker.XRabbitMQ:
|
||||||
|
result = await rabbitMQProducerService.SendMessageAsync(
|
||||||
|
topic: topic,
|
||||||
|
message: message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose ...
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (kafkaProducerService != null)
|
||||||
|
{
|
||||||
|
kafkaProducerService.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (zeroMQProducerService != null)
|
||||||
|
{
|
||||||
|
zeroMQProducerService.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (rabbitMQProducerService != null)
|
||||||
|
{
|
||||||
|
rabbitMQProducerService.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
using System;
|
||||||
|
using Confluent.Kafka;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// an Abstract Kafka Consumer Service ...
|
||||||
|
/// </summary>
|
||||||
|
public abstract class XKafkaConsumerServiceBase : XBaseEventConsumerBackgroundService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Kafka Consumer Class ...
|
||||||
|
/// </summary>
|
||||||
|
protected readonly IConsumer<Ignore, string> consumer;
|
||||||
|
|
||||||
|
protected XKafkaConsumerServiceBase(
|
||||||
|
string[] allowedActions,
|
||||||
|
string[] allowedSenders,
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
ILogger<XKafkaConsumerServiceBase> logger,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic
|
||||||
|
) : base(
|
||||||
|
topic: topic,
|
||||||
|
logger: logger,
|
||||||
|
configuration: configuration,
|
||||||
|
allowedActions: allowedActions,
|
||||||
|
allowedSenders: allowedSenders,
|
||||||
|
serviceProvider: serviceProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validating Configuration ...
|
||||||
|
if (configuration.Broker != XEventBroker.XKafka)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Kafka Consumer Configuration ...
|
||||||
|
var config = new ConsumerConfig
|
||||||
|
{
|
||||||
|
EnableAutoCommit = false,
|
||||||
|
BootstrapServers = configuration.Url,
|
||||||
|
AutoOffsetReset = AutoOffsetReset.Earliest,
|
||||||
|
GroupId = XEventServiceConstants.XDefaultConsumerGroup,
|
||||||
|
};
|
||||||
|
consumer = new ConsumerBuilder<Ignore, string>(config).Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Action Execution for Background Services ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected override async Task ExecuteAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// For Fix Blocking Synchronous ...
|
||||||
|
await Task.Yield();
|
||||||
|
using var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Subscribe to Topic ...
|
||||||
|
consumer.Subscribe(topic);
|
||||||
|
logger.LogInformation($"Kafka Cosume Topic: {topic} ...");
|
||||||
|
|
||||||
|
//
|
||||||
|
// Consuming ...
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Consuming Topic ...
|
||||||
|
var consumeResult = consumer.Consume(cancellationToken);
|
||||||
|
if (!consumeResult.IsNullOrDefault() &&
|
||||||
|
!consumeResult.Message.IsNullOrDefault()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var message = consumeResult
|
||||||
|
.Message
|
||||||
|
.Value
|
||||||
|
.FromJSON<XEventMessage>();
|
||||||
|
if (message.IsValid(
|
||||||
|
allowedSenders: allowedSenders,
|
||||||
|
allowedActions: allowedActions)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await DoWorkAsync(
|
||||||
|
payload: message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (ConsumeException ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError($"Kafka Consumer Result Parsing Error: {ex.Message} ...");
|
||||||
|
await Task.Delay(1000, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError($"Kafka Consumer Result Parsing Error: {ex.Message} ...");
|
||||||
|
await Task.Delay(1000, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose Object ...
|
||||||
|
/// </summary>
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (consumer != null)
|
||||||
|
{
|
||||||
|
consumer.Close();
|
||||||
|
consumer.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,32 +1,140 @@
|
|||||||
|
using System;
|
||||||
|
using Confluent.Kafka;
|
||||||
|
using System.Threading;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
using xEventService.Extensions;
|
using xEventService.Extensions;
|
||||||
using xEventService.Interfaces;
|
using xEventService.Interfaces;
|
||||||
using xEventService.Configuration;
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace xEventService.Providers
|
namespace xEventService.Providers
|
||||||
{
|
{
|
||||||
public class XKafkaProducerService : IXKafkaProducerService
|
public class XKafkaProducerService : XBaseEventProducerService, IXKafkaProducerService
|
||||||
{
|
{
|
||||||
private readonly IProducer<Null, string> producer;
|
private readonly IProducer<Null, string> producer;
|
||||||
private readonly ILogger<RpkKafkaProducerService> logger;
|
|
||||||
|
|
||||||
public XKafkaProducerService(
|
public XKafkaProducerService(
|
||||||
ILoggeer<XKafkaProducerService> logger,
|
ILogger<XKafkaProducerService> logger,
|
||||||
XEventServiceConfiguration configuration
|
XEventServiceConfiguration configuration
|
||||||
)
|
) : base(configuration, logger)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
this.logger = logger;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Validate Configurations ...
|
// Validate Configurations ...
|
||||||
if (!configuration.IsValid() ||
|
if (!configuration.IsValid() ||
|
||||||
configuration.Broker != Constants.XEventBroker.XKafka
|
configuration.Broker != XEventBroker.XKafka
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
XException.InvalidConfiguration.Throw();
|
XException.InvalidConfiguration.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Kafka Producer Configuration ...
|
||||||
|
var config = new ProducerConfig
|
||||||
|
{
|
||||||
|
Acks = Acks.All,
|
||||||
|
BootstrapServers = configuration.Url,
|
||||||
|
MessageSendMaxRetries = configuration.MaxRetries,
|
||||||
|
};
|
||||||
|
producer = new ProducerBuilder<Null, string>(config).Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending Message ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">an Instance of <see cref="XEventRequest"/> to Provides Kafka Messaging requirement ...</param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
var result = request.IsValid();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = await SendMessageAsync(
|
||||||
|
topic: request.Topic,
|
||||||
|
message: request.Message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending a Message ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">an Instance of <see cref="XEventMessage"/> to Provides Kafka Messaging requirement ...</param>
|
||||||
|
/// <param name="topic">Topic for Sending Message ...</param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
var result = message.IsValid() &&
|
||||||
|
!topic.IsNullOrEmpty();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var response = await producer.ProduceAsync(
|
||||||
|
topic: topic,
|
||||||
|
message: new Message<Null, string>
|
||||||
|
{
|
||||||
|
Value = message
|
||||||
|
.ToJSON(camelCase: true)
|
||||||
|
},
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
result = !response.IsNullOrDefault();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError($"Kafka Produce Error: {ex.Message} ...");
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose Required Objects ...
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (producer != null)
|
||||||
|
{
|
||||||
|
producer.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,277 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using RabbitMQ.Client.Events;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// an Abstract Kafka Consumer Service ...
|
||||||
|
/// </summary>
|
||||||
|
public abstract class XRabbitMQConsumerServiceBase : XBaseEventConsumerBackgroundService
|
||||||
|
{
|
||||||
|
//
|
||||||
|
protected IModel channel;
|
||||||
|
protected IConnection connection;
|
||||||
|
protected AsyncEventingBasicConsumer consumer;
|
||||||
|
|
||||||
|
protected XRabbitMQConsumerServiceBase(
|
||||||
|
string[] allowedActions,
|
||||||
|
string[] allowedSenders,
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
ILogger<XRabbitMQConsumerServiceBase> logger,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic
|
||||||
|
) : base(
|
||||||
|
topic: topic,
|
||||||
|
logger: logger,
|
||||||
|
configuration: configuration,
|
||||||
|
allowedActions: allowedActions,
|
||||||
|
allowedSenders: allowedSenders,
|
||||||
|
serviceProvider: serviceProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validating Configuration ...
|
||||||
|
if (!configuration.RabbitMQ.IsValid() ||
|
||||||
|
configuration.Broker != XEventBroker.XRabbitMQ
|
||||||
|
)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
SetupConnection();
|
||||||
|
DeclareTopology(topic);
|
||||||
|
consumer = new AsyncEventingBasicConsumer(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Action Execution for Background Services ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected override async Task ExecuteAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// For Fix Blocking Synchronous ...
|
||||||
|
await Task.Yield();
|
||||||
|
using var cts = new CancellationTokenSource();
|
||||||
|
logger.LogInformation($"RabbitMQ Cosume Topic: {topic} ...");
|
||||||
|
|
||||||
|
//
|
||||||
|
// Subscribe to Topic ...
|
||||||
|
consumer.Received += async (model, ea) =>
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var body = ea.Body.ToArray();
|
||||||
|
var json = Encoding.UTF8.GetString(body);
|
||||||
|
var message = json.FromJSON<XEventMessage>();
|
||||||
|
if (!message.IsNullOrDefault() &&
|
||||||
|
message.IsValid(
|
||||||
|
allowedSenders: allowedSenders,
|
||||||
|
allowedActions: allowedActions)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await DoWorkAsync(
|
||||||
|
payload: message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (configuration.RabbitMQ.ManualAck)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
channel.BasicAck(
|
||||||
|
multiple: false,
|
||||||
|
deliveryTag: ea.DeliveryTag
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError($"RabbitMQ Cosume Error: {ex.Message} ...");
|
||||||
|
if (configuration.RabbitMQ.ManualAck)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
channel.BasicNack(
|
||||||
|
requeue: true,
|
||||||
|
multiple: false,
|
||||||
|
deliveryTag: ea.DeliveryTag
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
consumer.Shutdown += (model, ea) =>
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
channel.BasicQos(
|
||||||
|
prefetchSize: 0,
|
||||||
|
global: false,
|
||||||
|
prefetchCount: configuration.RabbitMQ.PrefetchCount
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
channel.BasicConsume(
|
||||||
|
queue: GetQueueName(),
|
||||||
|
consumer: consumer,
|
||||||
|
autoAck: !configuration.RabbitMQ.ManualAck
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
await Task.Delay(1000, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Overriding Stop Action ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (channel != null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
channel.Close();
|
||||||
|
channel.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (connection != null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
connection.Close();
|
||||||
|
connection.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await base.StopAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose Implementation ...
|
||||||
|
/// </summary>
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (channel != null)
|
||||||
|
{
|
||||||
|
channel.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (connection != null)
|
||||||
|
{
|
||||||
|
connection.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
private string GetQueueName()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = string.Empty;
|
||||||
|
if (!configuration.RabbitMQ.Queue.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
result = configuration.RabbitMQ.Queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (result.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var exchangeName = topic ?? configuration.RabbitMQ.Exchange;
|
||||||
|
var digits = Guid.NewGuid().ToString().GetDigits();
|
||||||
|
result = $"{exchangeName}[{digits}]";
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetupConnection()
|
||||||
|
{
|
||||||
|
var factory = new ConnectionFactory
|
||||||
|
{
|
||||||
|
DispatchConsumersAsync = true,
|
||||||
|
Port = configuration.GetRabbitMQPort(),
|
||||||
|
HostName = configuration.GetRabbitMQHost(),
|
||||||
|
UserName = configuration.RabbitMQ.Username,
|
||||||
|
Password = configuration.RabbitMQ.Password,
|
||||||
|
NetworkRecoveryInterval = TimeSpan.FromSeconds(
|
||||||
|
configuration.RabbitMQ.NetworkRecoveryIntervalSeconds
|
||||||
|
),
|
||||||
|
VirtualHost = configuration.RabbitMQ.VirtualHost,
|
||||||
|
AutomaticRecoveryEnabled = configuration.RabbitMQ.AutomaticRecoveryEnabled,
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
connection = factory.CreateConnection();
|
||||||
|
channel = connection.CreateModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DeclareTopology(string topic)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
channel.ExchangeDeclare(
|
||||||
|
arguments: null,
|
||||||
|
exchange: topic ?? configuration.RabbitMQ.Exchange,
|
||||||
|
durable: configuration.RabbitMQ.ExchangeDurable,
|
||||||
|
autoDelete: configuration.RabbitMQ.ExchangeAutoDelete,
|
||||||
|
type: configuration.RabbitMQ.ExchangeType.GetStringValue()
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var queueName = GetQueueName();
|
||||||
|
|
||||||
|
//
|
||||||
|
channel.QueueDeclare(
|
||||||
|
arguments: null,
|
||||||
|
queue: queueName,
|
||||||
|
durable: configuration.RabbitMQ.QueueDurable,
|
||||||
|
exclusive: configuration.RabbitMQ.QueueExclusive,
|
||||||
|
autoDelete: configuration.RabbitMQ.QueueAutoDelete
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
channel.QueueBind(
|
||||||
|
arguments: null,
|
||||||
|
queue: queueName,
|
||||||
|
exchange: topic ?? configuration.RabbitMQ.Exchange,
|
||||||
|
routingKey: configuration.RabbitMQ.RoutingKey
|
||||||
|
);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,315 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Interfaces;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// RabbitMQ Producer Service Implementation ...
|
||||||
|
/// </summary>
|
||||||
|
public class XRabbitMQProducerService : XBaseEventProducerService, IXRabbitMQProducerService
|
||||||
|
{
|
||||||
|
private IModel channel;
|
||||||
|
private IConnection connection;
|
||||||
|
private readonly object connectionLock = new();
|
||||||
|
|
||||||
|
public XRabbitMQProducerService(
|
||||||
|
ILogger<XRabbitMQProducerService> logger,
|
||||||
|
XEventServiceConfiguration configuration
|
||||||
|
) : base(configuration, logger)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Configurations ...
|
||||||
|
if (!configuration.IsValid() ||
|
||||||
|
!configuration.RabbitMQ.IsValid() ||
|
||||||
|
configuration.Broker != XEventBroker.XRabbitMQ
|
||||||
|
)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Initialize Connection ...
|
||||||
|
CreateConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending Message with Full Request Descriptor ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
var result = request.IsValid();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = await SendMessageAsync(
|
||||||
|
message: request.Message,
|
||||||
|
exchange: request.Topic,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
routingKey: configuration.RabbitMQ.RoutingKey
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending a Message to Topic ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <param name="topic"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await SendMessageAsync(
|
||||||
|
request: new XEventRequest
|
||||||
|
{
|
||||||
|
Topic = topic,
|
||||||
|
Message = message
|
||||||
|
},
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending a Message to Exchange ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <param name="exchange"></param>
|
||||||
|
/// <param name="routingKey"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string exchange = null,
|
||||||
|
string routingKey = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
var result = message.IsValid();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize Exchange ...
|
||||||
|
if (exchange.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
exchange = configuration.RabbitMQ.Exchange;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize Routing Key ...
|
||||||
|
if (routingKey.IsNull())
|
||||||
|
{
|
||||||
|
routingKey = configuration.RabbitMQ.RoutingKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Ensure Connection is Alive ...
|
||||||
|
EnsureChannel();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Serialize Message ...
|
||||||
|
var body = Encoding.UTF8.GetBytes(
|
||||||
|
message.ToJSON(camelCase: true)
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Message Properties ...
|
||||||
|
var properties = channel.CreateBasicProperties();
|
||||||
|
properties.Persistent = true;
|
||||||
|
properties.ContentType = "application/json";
|
||||||
|
properties.Timestamp = new AmqpTimestamp(
|
||||||
|
DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
);
|
||||||
|
properties.MessageId = Guid.NewGuid().ToString();
|
||||||
|
properties.Type = message.Action;
|
||||||
|
properties.AppId = message.Sender;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Publish Message ...
|
||||||
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
|
//
|
||||||
|
channel.BasicPublish(
|
||||||
|
exchange: exchange,
|
||||||
|
routingKey: routingKey,
|
||||||
|
mandatory: false,
|
||||||
|
basicProperties: properties,
|
||||||
|
body: body
|
||||||
|
);
|
||||||
|
},
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError($"RabbitMQ Produce Error: {ex.Message} ...");
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose Object ...
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (channel != null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
channel.Close();
|
||||||
|
channel.Dispose();
|
||||||
|
channel = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (connection != null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
connection.Close();
|
||||||
|
connection.Dispose();
|
||||||
|
connection = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{ }
|
||||||
|
|
||||||
|
//
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
/// <summary>
|
||||||
|
/// Create Connection to RabbitMQ Server ...
|
||||||
|
/// </summary>
|
||||||
|
private void CreateConnection()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var factory = new ConnectionFactory
|
||||||
|
{
|
||||||
|
Port = configuration.GetRabbitMQPort(),
|
||||||
|
HostName = configuration.GetRabbitMQHost(),
|
||||||
|
UserName = configuration.RabbitMQ.Username,
|
||||||
|
Password = configuration.RabbitMQ.Password,
|
||||||
|
VirtualHost = configuration.RabbitMQ.VirtualHost,
|
||||||
|
AutomaticRecoveryEnabled = configuration.RabbitMQ.AutomaticRecoveryEnabled,
|
||||||
|
NetworkRecoveryInterval = TimeSpan.FromSeconds(
|
||||||
|
configuration.RabbitMQ.NetworkRecoveryIntervalSeconds
|
||||||
|
),
|
||||||
|
DispatchConsumersAsync = true
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
connection = factory.CreateConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check Connection is Alive ...
|
||||||
|
/// </summary>
|
||||||
|
private bool IsConnected()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!connection.IsNullOrDefault() &&
|
||||||
|
connection.IsOpen &&
|
||||||
|
!channel.IsNull() &&
|
||||||
|
channel.IsOpen;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensure Channel is Open and Exchange/Queue Declared ...
|
||||||
|
/// </summary>
|
||||||
|
private void EnsureChannel()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (IsConnected())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
lock (connectionLock)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Double Check after Lock ...
|
||||||
|
if (IsConnected())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Recreate Connection if Dead ...
|
||||||
|
if (connection is null || !connection.IsOpen)
|
||||||
|
{
|
||||||
|
CreateConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create Channel ...
|
||||||
|
channel = connection.CreateModel();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Declare Exchange ...
|
||||||
|
channel.ExchangeDeclare(
|
||||||
|
arguments: null,
|
||||||
|
exchange: configuration.RabbitMQ.Exchange,
|
||||||
|
durable: configuration.RabbitMQ.ExchangeDurable,
|
||||||
|
autoDelete: configuration.RabbitMQ.ExchangeAutoDelete,
|
||||||
|
type: configuration.RabbitMQ.ExchangeType.GetStringValue()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
using NetMQ;
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using NetMQ.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// an Abstract ZeroMQ Consumer Service ...
|
||||||
|
/// </summary>
|
||||||
|
public abstract class XZeroMQConsumerServiceBase : XBaseEventConsumerBackgroundService
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private NetMQSocket socket;
|
||||||
|
|
||||||
|
protected XZeroMQConsumerServiceBase(
|
||||||
|
string[] allowedActions,
|
||||||
|
string[] allowedSenders,
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
ILogger<XZeroMQConsumerServiceBase> logger,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic
|
||||||
|
) : base(
|
||||||
|
topic: topic,
|
||||||
|
logger: logger,
|
||||||
|
configuration: configuration,
|
||||||
|
allowedActions: allowedActions,
|
||||||
|
allowedSenders: allowedSenders,
|
||||||
|
serviceProvider: serviceProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validating Configuration ...
|
||||||
|
if (!configuration.ZeroMQ.IsValid() ||
|
||||||
|
configuration.Broker != XEventBroker.XZeroMQ)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Initialize ...
|
||||||
|
InitializeSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default Action Execution for Background Services ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected override async Task ExecuteAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await Task.Yield();
|
||||||
|
logger.LogInformation($"ZeroMQ Cosume Topic: {topic} ...");
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
string receivedTopic = string.Empty;
|
||||||
|
byte[] body = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (configuration.ZeroMQ.Pattern == XZeroMQPattern.PubSub)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (socket.TryReceiveFrameString(TimeSpan.FromMilliseconds(100), out receivedTopic))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (socket.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(100), out body))
|
||||||
|
{
|
||||||
|
// Process
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogError($"ZeroMQ Cosume Error ...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (socket.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(100), out body))
|
||||||
|
{
|
||||||
|
// Process
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogError($"ZeroMQ Cosume Error ...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (body != null && body.Length > 0)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var json = Encoding.UTF8.GetString(body);
|
||||||
|
var message = json.FromJSON<XEventMessage>();
|
||||||
|
if (!message.IsNullOrDefault() &&
|
||||||
|
message.IsValid(
|
||||||
|
allowedSenders: allowedSenders,
|
||||||
|
allowedActions: allowedActions)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await DoWorkAsync(
|
||||||
|
payload: message,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await Task.Delay(10, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogError($"ZeroMQ Cosume Error: {ex.Message} ...");
|
||||||
|
await Task.Delay(1000, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (socket != null)
|
||||||
|
{
|
||||||
|
socket.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await base.StopAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose Object ...
|
||||||
|
/// </summary>
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (socket != null)
|
||||||
|
{
|
||||||
|
socket.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
private void InitializeSocket()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (configuration.ZeroMQ.Pattern == XZeroMQPattern.PubSub)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
socket = new SubscriberSocket();
|
||||||
|
socket.Options.ReceiveHighWatermark = configuration.ZeroMQ.ReceiveHighWaterMark;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Subscribe to specific topic or all
|
||||||
|
if (topic.IsNullOrEmpty() || topic == XEventServiceConstants.XWildcard)
|
||||||
|
{
|
||||||
|
((SubscriberSocket)socket).SubscribeToAnyTopic();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((SubscriberSocket)socket).Subscribe(topic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
socket = new PullSocket();
|
||||||
|
socket.Options.ReceiveHighWatermark = configuration.ZeroMQ.ReceiveHighWaterMark;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (configuration.ZeroMQ.IsServer)
|
||||||
|
{
|
||||||
|
socket.Bind(configuration.Url);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
socket.Connect(configuration.Url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,198 @@
|
|||||||
|
using NetMQ;
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using NetMQ.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xEventService.Models;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xEventService.Constants;
|
||||||
|
using xEventService.Extensions;
|
||||||
|
using xEventService.Interfaces;
|
||||||
|
using xEventService.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xEventService.Providers
|
||||||
|
{
|
||||||
|
public class XZeroMQProducerService : XBaseEventProducerService, IXZeroMQProducerService
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private NetMQSocket socket;
|
||||||
|
private readonly object socketLock = new();
|
||||||
|
|
||||||
|
public XZeroMQProducerService(
|
||||||
|
XEventServiceConfiguration configuration,
|
||||||
|
ILogger<XZeroMQProducerService> logger
|
||||||
|
) : base(configuration, logger)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Configurations ...
|
||||||
|
if (!configuration.IsValid() ||
|
||||||
|
!configuration.ZeroMQ.IsValid() ||
|
||||||
|
configuration.Broker != XEventBroker.XZeroMQ
|
||||||
|
)
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
InitializeSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending Message with Full Request Descriptor ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = request.IsValid();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = await SendMessageAsync(
|
||||||
|
message: request.Message,
|
||||||
|
topic: request.Topic,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sending a Message to Topic ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <param name="topic"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> SendMessageAsync(
|
||||||
|
XEventMessage message,
|
||||||
|
string topic = XEventServiceConstants.XDefaultTopic,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = message.IsValid();
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize Topic ...
|
||||||
|
if (topic.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
topic = XEventServiceConstants.XDefaultTopic;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var json = message.ToJSON(camelCase: true);
|
||||||
|
var body = Encoding.UTF8.GetBytes(json);
|
||||||
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
|
//
|
||||||
|
lock (socketLock)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (configuration.ZeroMQ.Pattern == XZeroMQPattern.PubSub)
|
||||||
|
{
|
||||||
|
socket.SendMoreFrame(topic).SendFrame(body);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
socket.SendFrame(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dispose Object ...
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
lock (socketLock)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (socket != null)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
socket.Close();
|
||||||
|
socket.Dispose();
|
||||||
|
socket = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
//
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize Socket ...
|
||||||
|
/// </summary>
|
||||||
|
private void InitializeSocket()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
lock (socketLock)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (configuration.ZeroMQ.Pattern == XZeroMQPattern.PubSub)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
socket = new PublisherSocket();
|
||||||
|
socket.Options.SendHighWatermark = configuration.ZeroMQ.SendHighWaterMark;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
socket = new PushSocket();
|
||||||
|
socket.Options.SendHighWatermark = configuration.ZeroMQ.SendHighWaterMark;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (configuration.ZeroMQ.IsServer)
|
||||||
|
{
|
||||||
|
socket.Bind(configuration.Url);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
socket.Connect(configuration.Url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,15 +35,16 @@
|
|||||||
|
|
||||||
<!-- Dependencies -->
|
<!-- Dependencies -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="NetMQ" Version="4.0.4.3" />
|
||||||
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
|
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
|
||||||
<PackageReference Include="Confluent.Kafka" Version="2.14.2" />
|
<PackageReference Include="Confluent.Kafka" Version="2.14.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- For XML Documentation Support -->
|
<!-- For XML Documentation Support -->
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
|
||||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
Reference in New Issue
Block a user