init
This commit is contained in:
163
Kreta.MessageBroker/Client/MessageClient.cs
Normal file
163
Kreta.MessageBroker/Client/MessageClient.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kreta.Core.MessageBroker;
|
||||
using Kreta.Core.MessageBroker.Client.Implementations;
|
||||
using Kreta.Core.MessageBroker.Contract;
|
||||
using Kreta.Core.MessageBroker.Logging;
|
||||
using Kreta.Core.MessageBroker.Logging.Serilog;
|
||||
using Kreta.MessageBroker.ClientFactory;
|
||||
using Kreta.MessageBroker.Configuration;
|
||||
using Kreta.MessageBroker.TraceLog;
|
||||
using log4net;
|
||||
|
||||
namespace Kreta.MessageBroker.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Message client
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Message object</typeparam>
|
||||
abstract class MessageClient<T> : IMessageClient<T>
|
||||
where T : Message
|
||||
{
|
||||
#region [Properties]
|
||||
|
||||
private IntegratedMessageClient<T> IntegratedMessageClient { get; }
|
||||
|
||||
private string MessageSignatureKey { get; }
|
||||
|
||||
private int QueueSize { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region [Public Methods]
|
||||
|
||||
/// <summary>
|
||||
/// Üzenet küldése
|
||||
/// </summary>
|
||||
/// <param name="message">Üzenet</param>
|
||||
/// <remarks>A metódus az üzenetet várakozási sorba helyezi és azonal visszatér.
|
||||
/// Ha a várakozási sor megtellik, az üzenetet eldobja</remarks>
|
||||
public async Task PostAsync(T message)
|
||||
{
|
||||
message.CreateSignature(this.MessageSignatureKey);
|
||||
|
||||
await this.IntegratedMessageClient.PostAsync(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Üzenet küldése
|
||||
/// </summary>
|
||||
/// <param name="message">Üzenet</param>
|
||||
public void Post(T message)
|
||||
{
|
||||
if (this.QueueSize == 0)
|
||||
{
|
||||
Task.Run(async () => await PostAsync(message));
|
||||
}
|
||||
else
|
||||
{
|
||||
PostAsync(message).Wait();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Üzenet tömb küldése
|
||||
/// </summary>
|
||||
/// <param name="messages">Üzenetek</param>
|
||||
/// <remarks>A metódus addig nem tér vissza amig el nem küldtük az összes üzenetet</remarks>
|
||||
public async Task PostAsync(T[] messages)
|
||||
{
|
||||
foreach (var message in messages)
|
||||
{
|
||||
message.CreateSignature(this.MessageSignatureKey);
|
||||
}
|
||||
|
||||
await this.IntegratedMessageClient.PostAsync(messages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Üzenet tömb küldése
|
||||
/// </summary>
|
||||
/// <param name="messages">Üzenetek</param>
|
||||
/// <remarks>A metódus addig nem tér vissza amig el nem küldtük az összes üzenetet</remarks>
|
||||
public void Post(T[] messages)
|
||||
{
|
||||
PostAsync(messages).Wait();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region [Constructor(s)]
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="jsonMessageClientFactory">Json message client factory</param>
|
||||
/// <param name="messageClientName">Message client name</param>
|
||||
/// <param name="configuration">Configuration</param>
|
||||
/// <param name="loggerName">Logger name</param>
|
||||
protected MessageClient(string messageClientName, IJsonMessageClientFactory<T> jsonMessageClientFactory, IMessageBrokerConfiguration configuration, ITraceMessageClient traceMessageClient, ISerilogTraceLogger serilogTraceLogger, string loggerName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(messageClientName))
|
||||
{
|
||||
throw new ArgumentException($"{nameof(messageClientName)} cannot be null or whitespace");
|
||||
}
|
||||
|
||||
if (jsonMessageClientFactory == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(jsonMessageClientFactory));
|
||||
}
|
||||
|
||||
if (traceMessageClient == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(traceMessageClient));
|
||||
}
|
||||
|
||||
if (configuration == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configuration));
|
||||
}
|
||||
|
||||
var clientConfiguration = configuration.Clients.Cast<MessageClientConfigurationElement>().FirstOrDefault(c => c.Name.Equals(messageClientName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (clientConfiguration == null)
|
||||
{
|
||||
throw new ArgumentException($"Client configuration was not found for client {messageClientName}");
|
||||
}
|
||||
|
||||
this.QueueSize = clientConfiguration.QueueSize;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(clientConfiguration.MessageSignatureKey))
|
||||
{
|
||||
throw new ArgumentException($"{nameof(clientConfiguration.MessageSignatureKey)} cannot be null or whitespace");
|
||||
}
|
||||
|
||||
this.MessageSignatureKey = clientConfiguration.MessageSignatureKey;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(loggerName))
|
||||
{
|
||||
throw new ArgumentException($"{nameof(loggerName)} cannot be null or whitespace");
|
||||
}
|
||||
|
||||
IKeyValueLogger keyValueLogger = null;
|
||||
|
||||
if (clientConfiguration.LoggerType == LoggerType.Log4Net)
|
||||
{
|
||||
keyValueLogger = new Log4NetKeyValueLogger(LogManager.GetLogger(loggerName));
|
||||
}
|
||||
else if (clientConfiguration.LoggerType == LoggerType.Serilog)
|
||||
{
|
||||
keyValueLogger = new SerilogKeyValueLogger(clientConfiguration.SerilogLogger, serilogTraceLogger);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException($"Not supported logger type {clientConfiguration.LoggerType}");
|
||||
}
|
||||
|
||||
this.IntegratedMessageClient = new IntegratedMessageClient<T>(clientConfiguration, jsonMessageClientFactory, keyValueLogger, traceMessageClient);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
using Kreta.Core.MessageBroker.Contract.MobileNotification;
|
||||
using Kreta.MessageBroker.ClientFactory;
|
||||
using Kreta.MessageBroker.Configuration;
|
||||
using Kreta.MessageBroker.TraceLog;
|
||||
|
||||
namespace Kreta.MessageBroker.Client.MobileNotification
|
||||
{
|
||||
/// <summary>
|
||||
/// Mobile notification client
|
||||
/// </summary>
|
||||
class MobileNotificationMessageClient : MessageClient<MobileNotificationMessage>
|
||||
{
|
||||
#region [Constructor(s)]
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="jsonMessageClientFactory">Json message client factory of mobile notification message</param>
|
||||
/// <param name="configuration">Configuration</param>
|
||||
public MobileNotificationMessageClient(
|
||||
IJsonMessageClientFactory<MobileNotificationMessage> jsonMessageClientFactory,
|
||||
IMessageBrokerConfiguration configuration,
|
||||
ITraceMessageClient traceMessageClient,
|
||||
ISerilogTraceLogger serilogTraceLogger) : base(Constants.MobileNotificationMessageClient.Name,
|
||||
jsonMessageClientFactory,
|
||||
configuration,
|
||||
traceMessageClient,
|
||||
serilogTraceLogger,
|
||||
Constants.MobileNotificationMessageClient.LoggerName)
|
||||
{ }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,130 @@
|
||||
using System.Threading.Tasks;
|
||||
using Kreta.Core.MessageBroker.Contract.MobileNotification;
|
||||
using Kreta.Core.MessageBroker.Contract.MobileNotification.Enum;
|
||||
|
||||
namespace Kreta.MessageBroker.Client.MobileNotification
|
||||
{
|
||||
/// <summary>
|
||||
/// Mobile notification message helper
|
||||
/// </summary>
|
||||
public class MobileNotificationMessageHelper
|
||||
{
|
||||
#region [Public Methods]
|
||||
|
||||
/// <summary>
|
||||
/// Create
|
||||
/// </summary>
|
||||
/// <param name="instituteCode">Institute code</param>
|
||||
/// <param name="instituteUserId">Institute user id</param>
|
||||
/// <param name="itemId">Item id</param>
|
||||
/// <param name="message">Message</param>
|
||||
/// <param name="notificationType">Notification type</param>
|
||||
public static MobileNotificationMessage CreateMessage(string instituteCode, int instituteUserId, MobileNotificationMessageType notificationType, int itemId, string message)
|
||||
{
|
||||
return new MobileNotificationMessage(instituteCode, instituteUserId, MobileNotificationMessageRole.Student | MobileNotificationMessageRole.Tutelary, notificationType, MobileNotificationMessageSource.Kreta, MobileNotificationMessageDestination.RoleGroup, itemId, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create
|
||||
/// </summary>
|
||||
/// <param name="instituteCode">Institute code</param>
|
||||
/// <param name="instituteUserId">Institute user id</param>
|
||||
/// <param name="notificationType">Notification type</param>
|
||||
/// <param name="itemId">Item id</param>
|
||||
/// <param name="message">Message</param>
|
||||
/// <param name="title">Need for message address</param>
|
||||
/// <param name="data">Can hold any data</param>
|
||||
public static MobileNotificationMessage CreateMessage(string instituteCode, int instituteUserId, MobileNotificationMessageType notificationType, int itemId, string message, string title = "", string data = "")
|
||||
{
|
||||
return new MobileNotificationMessage(instituteCode, instituteUserId, MobileNotificationMessageRole.Student | MobileNotificationMessageRole.Tutelary, notificationType, MobileNotificationMessageSource.Kreta, MobileNotificationMessageDestination.RoleGroup, itemId, message, title, data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send message asynchronously
|
||||
/// </summary>
|
||||
/// <param name="instituteCode">Institute code</param>
|
||||
/// <param name="instituteUserId">Institute user id</param>
|
||||
/// <param name="itemId">Item id</param>
|
||||
/// <param name="message">Message</param>
|
||||
/// <param name="notificationType">Notification type</param>
|
||||
public static async Task PostStudentNotificationAsync(string instituteCode, int instituteUserId, MobileNotificationMessageType notificationType, int itemId, string message)
|
||||
{
|
||||
await DependencyContainer.Instance.GetInstance<MobileNotificationMessageClient>().PostAsync(new MobileNotificationMessage(instituteCode, instituteUserId, MobileNotificationMessageRole.Student | MobileNotificationMessageRole.Tutelary, notificationType, MobileNotificationMessageSource.Kreta, MobileNotificationMessageDestination.RoleGroup, itemId, message));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bulk send messages asynchronously
|
||||
/// </summary>
|
||||
/// <param name="messages">Üzenetek</param>
|
||||
public static async Task PostStudentNotificationAsync(MobileNotificationMessage[] messages)
|
||||
{
|
||||
await DependencyContainer.Instance.GetInstance<MobileNotificationMessageClient>().PostAsync(messages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send message
|
||||
/// </summary>
|
||||
/// <param name="instituteCode">Institute code</param>
|
||||
/// <param name="instituteUserId">Institute user id</param>
|
||||
/// <param name="itemId">Item id</param>
|
||||
/// <param name="message">Message</param>
|
||||
/// <param name="notificationType">Notification type</param>
|
||||
/// <remarks>We wrap calls into a task to avoid callbacks to current http syncronization context (because hosting thread may be destructed)</remarks>
|
||||
public static void PostStudentNotification(string instituteCode, int instituteUserId, MobileNotificationMessageType notificationType, int itemId, string message)
|
||||
{
|
||||
DependencyContainer.Instance.GetInstance<MobileNotificationMessageClient>().Post(new MobileNotificationMessage(instituteCode, instituteUserId, MobileNotificationMessageRole.Student | MobileNotificationMessageRole.Tutelary, notificationType, MobileNotificationMessageSource.Kreta, MobileNotificationMessageDestination.RoleGroup, itemId, message));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send message
|
||||
/// </summary>
|
||||
/// <param name="instituteCode">Institute code</param>
|
||||
/// <param name="instituteUserId">Institute user id</param>
|
||||
/// <param name="itemId">Item id</param>
|
||||
/// <param name="message">Message</param>
|
||||
/// <param name="notificationType">Notification type
|
||||
/// <list type="bullet">
|
||||
/// <item>
|
||||
/// <term>2</term>
|
||||
/// <description>Evaluation</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>4</term>
|
||||
/// <description>Absence</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>8</term>
|
||||
/// <description>Note</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>16</term>
|
||||
/// <description>Message</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>32</term>
|
||||
/// <description>Homework</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>64</term>
|
||||
/// <description>Exam</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </param>
|
||||
/// <remarks>We wrap calls into a task to avoid callbacks to current http syncronization context (because hosting thread may be destructed)</remarks>
|
||||
public static void PostStudentTestNotification(string instituteCode, int instituteUserId, int notificationType, int itemId, string message)
|
||||
{
|
||||
DependencyContainer.Instance.GetInstance<MobileNotificationMessageClient>().Post(new MobileNotificationMessage(instituteCode, instituteUserId, MobileNotificationMessageRole.Student | MobileNotificationMessageRole.Tutelary, (MobileNotificationMessageType)notificationType, MobileNotificationMessageSource.Kreta, MobileNotificationMessageDestination.RoleGroup, itemId, message));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bulk send messages
|
||||
/// </summary>
|
||||
/// <param name="messages">Üzenetek</param>
|
||||
public static void PostStudentNotification(MobileNotificationMessage[] messages)
|
||||
{
|
||||
DependencyContainer.Instance.GetInstance<MobileNotificationMessageClient>().Post(messages);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user