add supports for ZeroMQ, XRabbitMQ, XKafka ...

This commit is contained in:
2026-08-19 14:52:56 +03:30
parent 1e0641643d
commit e05420b1aa
11 changed files with 532 additions and 66 deletions
+7 -1
View File
@@ -23,7 +23,13 @@ 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> /// <summary>
/// RabbitMQ Specific Options ... /// RabbitMQ Specific Options ...
/// Only used when Broker is XRabbitMQ ... /// Only used when Broker is XRabbitMQ ...
+1 -2
View File
@@ -1,4 +1,3 @@
using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using xEventService.Models; using xEventService.Models;
@@ -29,6 +28,6 @@ namespace xEventService.Interfaces
string exchange = null, string exchange = null,
string routingKey = null, string routingKey = null,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );
} }
} }
+1 -3
View File
@@ -1,7 +1,5 @@
namespace xEventService.Interfaces namespace xEventService.Interfaces
{ {
public interface IXZeroMQProducerService : IXProducerServiceBase public interface IXZeroMQProducerService : IXProducerServiceBase
{ { }
}
} }
@@ -22,12 +22,12 @@ namespace xEventService.Providers
/// <summary> /// <summary>
/// Allowed Senders ... /// Allowed Senders ...
/// </summary> /// </summary>
public readonly string[] allowedSenders; protected readonly string[] allowedSenders;
/// <summary> /// <summary>
/// Allowed Actions ... /// Allowed Actions ...
/// </summary> /// </summary>
public readonly string[] allowedActions; protected readonly string[] allowedActions;
/// <summary> /// <summary>
/// Configuration ... /// Configuration ...
@@ -41,10 +41,7 @@ namespace xEventService.Providers
XEventServiceConfiguration configuration, XEventServiceConfiguration configuration,
ILogger<XResumableBackgroundService> logger, ILogger<XResumableBackgroundService> logger,
string topic = XEventServiceConstants.XDefaultTopic string topic = XEventServiceConstants.XDefaultTopic
) : base( ) : base(serviceProvider, logger)
logger: logger,
serviceProvider: serviceProvider
)
{ {
// //
// Validate Actions ... // Validate Actions ...
+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();
}
}
}
}
+25 -7
View File
@@ -6,6 +6,8 @@ using Microsoft.Extensions.Logging;
using xCommons.Extensions; using xCommons.Extensions;
using xEventService.Configuration; using xEventService.Configuration;
using xEventService.Constants; using xEventService.Constants;
using xEventService.Extensions;
using xEventService.Models;
using xExceptions.Constants; using xExceptions.Constants;
namespace xEventService.Providers namespace xEventService.Providers
@@ -87,10 +89,21 @@ namespace xEventService.Providers
) )
{ {
// //
await DoWorkAsync( var message = consumeResult
payload: consumeResult.Message, .Message
cancellationToken: cancellationToken .Value
); .FromJSON<XEventMessage>();
if (message.IsValid(
allowedSenders: allowedSenders,
allowedActions: allowedActions)
)
{
//
await DoWorkAsync(
payload: consumeResult.Message,
cancellationToken: cancellationToken
);
}
} }
} }
catch (ConsumeException) catch (ConsumeException)
@@ -105,13 +118,18 @@ namespace xEventService.Providers
} }
/// <summary> /// <summary>
/// Dispose Implementation ... /// Dispose Object ...
/// </summary> /// </summary>
public override void Dispose() public override void Dispose()
{ {
// //
consumer.Close(); if (consumer != null)
consumer.Dispose(); {
consumer.Close();
consumer.Dispose();
}
//
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
base.Dispose(); base.Dispose();
} }
+8 -9
View File
@@ -13,21 +13,15 @@ using System;
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<XKafkaProducerService> logger;
private readonly XEventServiceConfiguration configuration;
public XKafkaProducerService( public XKafkaProducerService(
ILogger<XKafkaProducerService> logger, ILogger<XKafkaProducerService> logger,
XEventServiceConfiguration configuration XEventServiceConfiguration configuration
) ) : base(configuration, logger)
{ {
//
this.logger = logger;
this.configuration = configuration;
// //
// Validate Configurations ... // Validate Configurations ...
if (!configuration.IsValid() || if (!configuration.IsValid() ||
@@ -132,7 +126,12 @@ namespace xEventService.Providers
public void Dispose() public void Dispose()
{ {
// //
producer.Dispose(); if (producer != null)
{
producer.Dispose();
}
//
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
} }
+18 -3
View File
@@ -19,6 +19,7 @@ namespace xEventService.Providers
/// </summary> /// </summary>
public abstract class XRabbitMQConsumerServiceBase : XBaseEventConsumerBackgroundService public abstract class XRabbitMQConsumerServiceBase : XBaseEventConsumerBackgroundService
{ {
//
protected IModel channel; protected IModel channel;
protected IConnection connection; protected IConnection connection;
protected AsyncEventingBasicConsumer consumer; protected AsyncEventingBasicConsumer consumer;
@@ -79,7 +80,11 @@ namespace xEventService.Providers
var body = ea.Body.ToArray(); var body = ea.Body.ToArray();
var json = Encoding.UTF8.GetString(body); var json = Encoding.UTF8.GetString(body);
var message = json.FromJSON<XEventMessage>(); var message = json.FromJSON<XEventMessage>();
if (!message.IsNullOrDefault()) if (!message.IsNullOrDefault() &&
message.IsValid(
allowedSenders: allowedSenders,
allowedActions: allowedActions)
)
{ {
// //
await DoWorkAsync( await DoWorkAsync(
@@ -163,8 +168,18 @@ namespace xEventService.Providers
public override void Dispose() public override void Dispose()
{ {
// //
channel.Dispose(); if (channel != null)
connection.Dispose(); {
channel.Dispose();
}
//
if (connection != null)
{
connection.Dispose();
}
//
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
base.Dispose(); base.Dispose();
} }
+53 -35
View File
@@ -17,23 +17,17 @@ namespace xEventService.Providers
/// <summary> /// <summary>
/// RabbitMQ Producer Service Implementation ... /// RabbitMQ Producer Service Implementation ...
/// </summary> /// </summary>
public class XRabbitMQProducerService : IXRabbitMQProducerService public class XRabbitMQProducerService : XBaseEventProducerService, IXRabbitMQProducerService
{ {
private IModel channel; private IModel channel;
private IConnection connection; private IConnection connection;
private readonly object connectionLock = new(); private readonly object connectionLock = new();
private readonly ILogger<XRabbitMQProducerService> logger;
private readonly XEventServiceConfiguration configuration;
public XRabbitMQProducerService( public XRabbitMQProducerService(
ILogger<XRabbitMQProducerService> logger, ILogger<XRabbitMQProducerService> logger,
XEventServiceConfiguration configuration XEventServiceConfiguration configuration
) ) : base(configuration, logger)
{ {
//
this.logger = logger;
this.configuration = configuration;
// //
// Validate Configurations ... // Validate Configurations ...
if (!configuration.IsValid() || if (!configuration.IsValid() ||
@@ -52,6 +46,9 @@ namespace xEventService.Providers
/// <summary> /// <summary>
/// Sending Message with Full Request Descriptor ... /// Sending Message with Full Request Descriptor ...
/// </summary> /// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> SendMessageAsync( public async Task<bool> SendMessageAsync(
XEventRequest request, XEventRequest request,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
@@ -77,9 +74,41 @@ namespace xEventService.Providers
return result; 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> /// <summary>
/// Sending a Message to Exchange ... /// Sending a Message to Exchange ...
/// </summary> /// </summary>
/// <param name="message"></param>
/// <param name="exchange"></param>
/// <param name="routingKey"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> SendMessageAsync( public async Task<bool> SendMessageAsync(
XEventMessage message, XEventMessage message,
string exchange = null, string exchange = null,
@@ -114,7 +143,7 @@ namespace xEventService.Providers
{ {
// //
// Ensure Connection is Alive ... // Ensure Connection is Alive ...
await EnsureChannelAsync(cancellationToken); EnsureChannelAsync();
// //
// Serialize Message ... // Serialize Message ...
@@ -170,14 +199,22 @@ namespace xEventService.Providers
try try
{ {
// //
channel.Close(); if (channel != null)
channel.Dispose(); {
channel = null; //
channel.Close();
channel.Dispose();
channel = null;
}
// //
connection.Close(); if (connection != null)
connection.Dispose(); {
connection = null; //
connection.Close();
connection.Dispose();
connection = null;
}
} }
catch catch
{ } { }
@@ -228,29 +265,10 @@ namespace xEventService.Providers
return result; return result;
} }
/// <summary>
/// Ensure Connection Established ...
/// </summary>
private async Task<bool> EnsureConnectionAsync()
{
//
try
{
await EnsureChannelAsync();
return true;
}
catch
{
return false;
}
}
/// <summary> /// <summary>
/// Ensure Channel is Open and Exchange/Queue Declared ... /// Ensure Channel is Open and Exchange/Queue Declared ...
/// </summary> /// </summary>
private async Task EnsureChannelAsync( private void EnsureChannelAsync()
CancellationToken cancellationToken = default
)
{ {
// //
if (IsConnected()) if (IsConnected())
+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
}
}