Beispiel #1
0
 public void OnStart(string[] args)
 {
     try
     {
         Core.Log.InfoBasic("Starting messaging service");
         _cTokenSource = new CancellationTokenSource();
         OnInit(args);
         Status      = new MessagingServiceStatus(this);
         QueueServer = GetQueueServer();
         if (QueueServer is null)
         {
             throw new Exception("The queue server is null, please check the configuration file and ensure the Types assemblies are on the assembly folder.");
         }
         Processor = GetMessageProcessorAsync(QueueServer);
         if (Processor is null)
         {
             throw new Exception("The message processor is null, please check your GetMessageProcessor method implementation.");
         }
         if (QueueServer.ResponseServer)
         {
             QueueServer.ResponseReceived += ResponseReceivedHandler;
         }
         else
         {
             QueueServer.RequestReceived    += RequestReceivedHandler;
             QueueServer.BeforeSendResponse += BeforeSendResponseHandler;
             QueueServer.ResponseSent       += ResponseSentHandler;
         }
         Processor.Init();
         QueueServer.StartListeners();
         Core.Log.InfoBasic("Messaging service started.");
     }
     catch (Exception ex)
     {
         Core.Log.Write(ex);
         throw;
     }
 }
        /// <inheritdoc />
        /// <summary>
        /// On Service Start method
        /// </summary>
        /// <param name="args">Start arguments</param>
        public void OnStart(string[] args)
        {
            try
            {
                Core.Log.InfoBasic("Starting messaging service");
                _cTokenSource = new CancellationTokenSource();
                OnInit(args);
                Status      = new MessagingServiceStatus(this);
                QueueServer = GetQueueServer();
                if (QueueServer == null)
                {
                    throw new Exception("The queue server is null, please check the configuration file and ensure the Types assemblies are on the assembly folder.");
                }
                Processor = GetMessageProcessorAsync(QueueServer);
                if (Processor == null)
                {
                    throw new Exception("The message processor is null, please check your GetMessageProcessor method implementation.");
                }
                if (QueueServer.ResponseServer)
                {
                    QueueServer.ResponseReceived += async(s, e) =>
                    {
                        if (MessageReceived != null)
                        {
                            await MessageReceived.InvokeAsync(this, new MessageEventArgs(e.Message)).ConfigureAwait(false);
                        }
                        if (e.Message?.Body == null)
                        {
                            return;
                        }

                        ReceivedMessagesCache.TryAdd(e.Message.Body, e.Message);
                        Status.IncrementCurrentMessagesBeingProcessed();
                        var sw = Stopwatch.StartNew();
                        try
                        {
                            await Processor.ProcessAsync(e.Message.Body, _cTokenSource.Token).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Core.Log.Write(ex);
                            Status.IncrementTotalExceptions();
                        }
                        sw.Stop();
                        Status.ReportProcessingTime(sw.Elapsed.TotalMilliseconds);
                        Status.DecrementCurrentMessagesBeingProcessed();
                        Status.IncrementTotalMessagesProccesed();
                        ReceivedMessagesCache.TryRemove(e.Message.Body, out object _);
                    };
                }
                else
                {
                    QueueServer.RequestReceived += async(s, e) =>
                    {
                        if (MessageReceived != null)
                        {
                            await MessageReceived.InvokeAsync(this, new MessageEventArgs(e.Request)).ConfigureAwait(false);
                        }
                        if (e.Request?.Body == null)
                        {
                            return;
                        }

                        ReceivedMessagesCache.TryAdd(e.Request.Body, e.Request);
                        object result = null;
                        Status.IncrementCurrentMessagesBeingProcessed();
                        var sw = Stopwatch.StartNew();
                        try
                        {
                            result = await Processor.ProcessAsync(e.Request.Body, e.ProcessResponseTimeoutCancellationToken).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Core.Log.Write(ex);
                            Status.IncrementTotalExceptions();
                        }
                        sw.Stop();
                        Status.ReportProcessingTime(sw.Elapsed.TotalMilliseconds);
                        Status.DecrementCurrentMessagesBeingProcessed();
                        Status.IncrementTotalMessagesProccesed();
                        e.Response.Body = result;
                        ReceivedMessagesCache.TryRemove(e.Request.Body, out object _);
                    };
                    QueueServer.BeforeSendResponse += async(s, e) =>
                    {
                        if (BeforeSendMessage != null)
                        {
                            await BeforeSendMessage.InvokeAsync(this, new MessageEventArgs(e.Message)).ConfigureAwait(false);
                        }
                    };
                    QueueServer.ResponseSent += async(s, e) =>
                    {
                        if (MessageSent != null)
                        {
                            await MessageSent.InvokeAsync(this, new MessageEventArgs(e.Message)).ConfigureAwait(false);
                        }
                    };
                }

                Processor.Init();
                QueueServer.StartListeners();
                Core.Log.InfoBasic("Messaging service started.");
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
                throw;
            }
        }