Compare commits

...
2 Commits
Author SHA1 Message Date
saherelm e05420b1aa add supports for ZeroMQ, XRabbitMQ, XKafka ... 2026-08-19 14:52:56 +03:30
saherelm 1e0641643d last works on Event Service ... 2026-08-19 12:34:14 +03:30
20 changed files with 1069 additions and 291 deletions
+7 -3
View File
@@ -1,5 +1,3 @@
using System.Collections.Generic;
using RabbitMQ.Client;
using xEventService.Constants;
using xEventService.Models;
@@ -25,7 +23,13 @@ namespace xEventService.Configuration
/// Allowed Message Broker ...
/// </summary>
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 ...
+7 -4
View File
@@ -7,13 +7,16 @@ namespace xEventService.Constants
/// </summary>
public enum XEventBroker
{
[StringValue("None")]
[StringValue(XEventServiceConstants.None)]
None,
[StringValue("XKafka")]
[StringValue(XEventServiceConstants.XKafka)]
XKafka,
[StringValue("XRabbitMQ")]
XRabbitMQ
[StringValue(XEventServiceConstants.XZeroMQ)]
XZeroMQ,
[StringValue(XEventServiceConstants.XRabbitMQ)]
XRabbitMQ,
}
}
+11 -3
View File
@@ -8,10 +8,18 @@ namespace xEventService.Constants
//
// Defaults ...
public const string XDefaultTopic = "XSaherelm";
public const string XDefaultConsumerGroup = "XSaherElmGroup";
public const string XDefaultTopic = "XSaherelm";
public const string XDefaultConsumerGroup = "XSaherElmGroup";
//
public const string XWildcard = "*";
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 = "*";
}
}
+16
View File
@@ -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
}
}
+129 -4
View File
@@ -71,7 +71,7 @@ namespace xEventService.Extensions
var result =
!source.IsNullOrDefault() &&
!source.Exchange.IsNullOrEmpty() &&
!source.ExchangeType.IsValid();
source.ExchangeType.IsValid();
//
return result;
@@ -110,7 +110,37 @@ namespace xEventService.Extensions
//
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>
@@ -122,9 +152,9 @@ namespace xEventService.Extensions
var result =
source is not null &&
source.Length == 1 &&
source.Any(s =>
source.Any(s =>
s == XEventServiceConstants.XWildcard);
//
return result;
}
@@ -145,6 +175,101 @@ namespace xEventService.Extensions
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>
+2 -32
View File
@@ -1,38 +1,8 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using xEventService.Constants;
using xEventService.Models;
namespace xEventService.Interfaces
{
/// <summary>
/// a Service for Produce Kafka Messages ...
/// </summary>
public interface IXKafkaProducerService : 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
);
}
public interface IXKafkaProducerService : IXProducerServiceBase
{ }
}
+35
View File
@@ -0,0 +1,35 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using xEventService.Constants;
using xEventService.Models;
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
);
}
}
+2 -17
View File
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using xEventService.Models;
@@ -8,22 +7,8 @@ namespace xEventService.Interfaces
/// <summary>
/// RabbitMQ Producer Service Implementation ...
/// </summary>
public interface IXRabbitMQProducerService : IDisposable
public interface IXRabbitMQProducerService : IXProducerServiceBase
{
/// <summary>
/// Sending Message with Full Request Descriptor ...
/// </summary>
/// <param name="request">
/// an Instance of <see cref="XEventRequest"/>.
/// Topic is used as Exchange Name ...
/// </param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<bool> SendMessageAsync(
XEventRequest request,
CancellationToken cancellationToken = default
);
/// <summary>
/// Sending a Message to Default Exchange ...
/// </summary>
@@ -43,6 +28,6 @@ namespace xEventService.Interfaces
string exchange = null,
string routingKey = null,
CancellationToken cancellationToken = default
);
);
}
}
+5
View File
@@ -0,0 +1,5 @@
namespace xEventService.Interfaces
{
public interface IXZeroMQProducerService : IXProducerServiceBase
{ }
}
-1
View File
@@ -1,4 +1,3 @@
using RabbitMQ.Client;
using xEventService.Constants;
namespace xEventService.Models
+28
View File
@@ -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 Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xCommons.Providers;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xExceptions.Constants;
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;
}
}
}
+31
View File
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Extensions;
using xExceptions.Constants;
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();
}
}
}
}
+46 -155
View File
@@ -2,12 +2,12 @@ using System;
using System.Threading;
using System.Threading.Tasks;
using Confluent.Kafka;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xEventService.Models;
using xExceptions.Constants;
namespace xEventService.Providers
@@ -15,67 +15,35 @@ namespace xEventService.Providers
/// <summary>
/// an Abstract Kafka Consumer Service ...
/// </summary>
public abstract class XKafkaConsumerServiceBase : BackgroundService
public abstract class XKafkaConsumerServiceBase : XBaseEventConsumerBackgroundService
{
/// <summary>
/// Consumed Topic Messages ...
/// </summary>
protected readonly string topic;
/// <summary>
/// Check is Paused ...
/// </summary>
private bool paused = false;
/// <summary>
/// Allowed Senders ...
/// </summary>
public readonly string[] allowedSenders;
/// <summary>
/// Allowed Actions ...
/// </summary>
public readonly string[] allowedActions;
/// <summary>
/// Kafka Consumer Class ...
/// </summary>
protected readonly IConsumer<Ignore, string> consumer;
protected readonly XEventServiceConfiguration configuration;
protected readonly ILogger<XKafkaConsumerServiceBase> logger;
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
)
{
//
this.topic = topic;
this.logger = logger;
this.configuration = configuration;
//
// Validate Actions ...
if (!allowedActions
.IsAllowedCollection())
// Validating Configuration ...
if (configuration.Broker != XEventBroker.XKafka)
{
XException.InvalidArgs.Throw();
XException.InvalidConfiguration.Throw();
}
this.allowedActions = [.. allowedActions];
//
// Validate Senders ...
if (!allowedSenders
.IsAllowedCollection())
{
XException.InvalidArgs.Throw();
}
this.allowedSenders = [.. allowedSenders];
//
// Prepare Kafka Consumer Configuration ...
@@ -89,45 +57,6 @@ namespace xEventService.Providers
consumer = new ConsumerBuilder<Ignore, string>(config).Build();
}
/// <summary>
/// Pause Consuming ...
/// </summary>
/// <returns></returns>
public bool Pause()
{
//
var result = !paused;
if (!result)
{
return result;
}
//
paused = true;
result = paused;
return result;
}
/// <summary>
/// Resume Consuming ...
/// </summary>
/// <returns></returns>
public bool Resume()
{
//
var result = paused;
if (!result)
{
return result;
}
//
paused = false;
result = !paused;
return result;
}
/// <summary>
/// Default Action Execution for Background Services ...
/// </summary>
@@ -142,103 +71,65 @@ namespace xEventService.Providers
await Task.Yield();
using var cts = new CancellationTokenSource();
//
// Subscribe to Topic ...
consumer.Subscribe(topic);
//
// Consuming ...
while (!cancellationToken.IsCancellationRequested)
{
try
{
//
// Consuming Topic ...
await consumer.Consume(async (consumeResult, cancellationToken) =>
var consumeResult = consumer.Consume(cancellationToken);
if (!consumeResult.IsNullOrDefault() &&
!consumeResult.Message.IsNullOrDefault()
)
{
// Check Pausing ...
if (!paused)
//
var message = consumeResult
.Message
.Value
.FromJSON<XEventMessage>();
if (message.IsValid(
allowedSenders: allowedSenders,
allowedActions: allowedActions)
)
{
// Logging Message Recieved ...
if (enableLogging)
{
// Encrypt Message ...
var expirationOffset = DateTimeOffset.UtcNow
.AddMinutes(encryptionLifetimeInMinute);
var encryptedMessage = cryptographyService.Encrypt(
text: consumeResult?.Message.Value ?? string.Empty,
expirationOffset: expirationOffset
);
logger
.LogInformation(
"Consume Recieved Message from Kafka Topic: {Topic}, Offset: {Offset}, Partition: {Partition}, Message: {Message}",
topic,
consumeResult?.Offset,
consumeResult?.Partition,
encryptedMessage
);
}
// Produce Message Model ...
var model = consumeResult
.FillConsumeResult()
?? throw RpkKafkaExceptionHelper
.GetInvalidKafkaConsumeResultException();
// Check Sender and Actions Owning ...
var isOwned = model.IsOwned(
allowedActions,
allowedSenders
//
await DoWorkAsync(
payload: consumeResult.Message,
cancellationToken: cancellationToken
);
if (isOwned)
{
// Notify Extended Classes for Consume Message ...
await ConsumeAsync(
model,
cancellationToken
);
}
// Commit Offset after Consuming ...
consumer?.Commit(consumeResult);
}
},
cts.Token);
}
catch (ConsumeException ex)
{
// Log Exception ...
if (enableLogging)
{
logger.LogError(
ex,
"Kafka Message Consume Failed: {Error}",
ex.Error.Reason
);
}
}
catch (ConsumeException)
{
await Task.Delay(1000, cancellationToken);
}
catch (Exception ex)
catch (Exception)
{
// Log Exception ...
if (enableLogging)
{
logger.LogError(
ex,
"Exception: {Exc}",
ex.Message
);
}
await Task.Delay(1000, cancellationToken);
}
}
}
}
/// <summary>
/// Dispose Implementation ...
/// Dispose Object ...
/// </summary>
public override void Dispose()
{
//
consumer.Close();
consumer.Dispose();
if (consumer != null)
{
consumer.Close();
consumer.Dispose();
}
//
GC.SuppressFinalize(this);
base.Dispose();
}
+9 -10
View File
@@ -13,21 +13,15 @@ using System;
namespace xEventService.Providers
{
public class XKafkaProducerService : IXKafkaProducerService
public class XKafkaProducerService : XBaseEventProducerService, IXKafkaProducerService
{
private readonly IProducer<Null, string> producer;
private readonly ILogger<XKafkaProducerService> logger;
private readonly XEventServiceConfiguration configuration;
public XKafkaProducerService(
ILogger<XKafkaProducerService> logger,
XEventServiceConfiguration configuration
)
) : base(configuration, logger)
{
//
this.logger = logger;
this.configuration = configuration;
//
// Validate Configurations ...
if (!configuration.IsValid() ||
@@ -115,7 +109,7 @@ namespace xEventService.Providers
);
//
result = true;
result = !response.IsNullOrDefault();
}
catch
{
@@ -132,7 +126,12 @@ namespace xEventService.Providers
public void Dispose()
{
//
producer.Dispose();
if (producer != null)
{
producer.Dispose();
}
//
GC.SuppressFinalize(this);
}
}
+236 -22
View File
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xEventService.Models;
using xExceptions.Constants;
namespace xEventService.Providers
@@ -15,36 +17,248 @@ namespace xEventService.Providers
/// <summary>
/// an Abstract Kafka Consumer Service ...
/// </summary>
public class XRabbitMQConsumerServiceBase : BackgroundService
public abstract class XRabbitMQConsumerServiceBase : XBaseEventConsumerBackgroundService
{
protected readonly IModel channel;
protected readonly IConnection connection;
protected readonly XEventServiceConfiguration configuration;
protected readonly ILogger<XRabbitMQConsumerServiceBase> logger;
//
protected IModel channel;
protected IConnection connection;
protected AsyncEventingBasicConsumer consumer;
public XRabbitMQConsumerServiceBase(
protected XRabbitMQConsumerServiceBase(
string[] allowedActions,
string[] allowedSenders,
IServiceProvider serviceProvider,
XEventServiceConfiguration configuration,
ILogger<XRabbitMQConsumerServiceBase> logger
ILogger<XRabbitMQConsumerServiceBase> logger,
string topic = XEventServiceConstants.XDefaultTopic
) : base(
topic: topic,
logger: logger,
configuration: configuration,
allowedActions: allowedActions,
allowedSenders: allowedSenders,
serviceProvider: serviceProvider
)
{
//
this.logger = logger;
this.configuration = configuration;
//
// Validate ...
if (!configuration.IsValid() ||
!configuration.RabbitMQ.IsValid() ||
configuration.Broker != Constants.XEventBroker.XRabbitMQ
// Validating Configuration ...
if (!configuration.RabbitMQ.IsValid() ||
configuration.Broker != XEventBroker.XRabbitMQ
)
{
XException.InvalidConfiguration.Throw();
}
//
SetupConnection();
DeclareTopology();
consumer = new AsyncEventingBasicConsumer(channel);
}
public abstract Task HandleMessageAsync(
XEventMessage message,
/// <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.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
{
//
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)
{
//
channel.Close();
channel.Dispose();
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 digits = Guid.NewGuid().ToString().GetDigits();
result = $"{configuration.RabbitMQ.Exchange}[{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()
{
//
channel.ExchangeDeclare(
arguments: null,
exchange: 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: configuration.RabbitMQ.Exchange,
routingKey: configuration.RabbitMQ.RoutingKey
);
}
#endregion
}
}
+53 -38
View File
@@ -17,23 +17,17 @@ namespace xEventService.Providers
/// <summary>
/// RabbitMQ Producer Service Implementation ...
/// </summary>
public class XRabbitMQProducerService : IXRabbitMQProducerService
public class XRabbitMQProducerService : XBaseEventProducerService, IXRabbitMQProducerService
{
private IModel channel;
private IConnection connection;
private readonly object connectionLock = new();
private readonly ILogger<XRabbitMQProducerService> logger;
private readonly XEventServiceConfiguration configuration;
public XRabbitMQProducerService(
ILogger<XRabbitMQProducerService> logger,
XEventServiceConfiguration configuration
)
) : base(configuration, logger)
{
//
this.logger = logger;
this.configuration = configuration;
//
// Validate Configurations ...
if (!configuration.IsValid() ||
@@ -52,6 +46,9 @@ namespace xEventService.Providers
/// <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
@@ -77,9 +74,41 @@ namespace xEventService.Providers
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,
@@ -114,7 +143,7 @@ namespace xEventService.Providers
{
//
// Ensure Connection is Alive ...
await EnsureChannelAsync(cancellationToken);
EnsureChannelAsync();
//
// Serialize Message ...
@@ -170,14 +199,22 @@ namespace xEventService.Providers
try
{
//
channel.Close();
channel.Dispose();
channel = null;
if (channel != null)
{
//
channel.Close();
channel.Dispose();
channel = null;
}
//
connection.Close();
connection.Dispose();
connection = null;
if (connection != null)
{
//
connection.Close();
connection.Dispose();
connection = null;
}
}
catch
{ }
@@ -228,29 +265,10 @@ namespace xEventService.Providers
return result;
}
/// <summary>
/// Ensure Connection Established ...
/// </summary>
public async Task<bool> EnsureConnectionAsync()
{
//
try
{
await EnsureChannelAsync();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Ensure Channel is Open and Exchange/Queue Declared ...
/// </summary>
private async Task EnsureChannelAsync(
CancellationToken cancellationToken = default
)
private void EnsureChannelAsync()
{
//
if (IsConnected())
@@ -289,9 +307,6 @@ namespace xEventService.Providers
type: configuration.RabbitMQ.ExchangeType.GetStringValue()
);
}
//
await Task.CompletedTask;
}
#endregion
}
+187
View File
@@ -0,0 +1,187 @@
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NetMQ;
using NetMQ.Sockets;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xEventService.Models;
using xExceptions.Constants;
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.Broker != XEventBroker.XKafka)
{
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();
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
{
if (socket.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(100), out body))
{
// Process
}
}
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)
{
await Task.Delay(1000, cancellationToken);
}
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
//
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
}
}
+198
View File
@@ -0,0 +1,198 @@
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NetMQ;
using NetMQ.Sockets;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xEventService.Interfaces;
using xEventService.Models;
using xExceptions.Constants;
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
}
}
+2 -2
View File
@@ -20,8 +20,7 @@
<!-- Icon Handling -->
<ItemGroup>
<None Include="../../Documents/Resources/Images/favicon.png" Link="icon.png" Pack="true"
PackagePath="\icon.png" />
<None Include="../../Documents/Resources/Images/favicon.png" Link="icon.png" Pack="true" PackagePath="\icon.png" />
</ItemGroup>
<!-- Local Modules -->
@@ -35,6 +34,7 @@
<!-- Dependencies -->
<ItemGroup>
<PackageReference Include="NetMQ" Version="4.0.4.3" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
<PackageReference Include="Confluent.Kafka" Version="2.14.2" />
</ItemGroup>