Ejemplo n.º 1
0
 private async Task BeforeSendResponseHandler(object sender, ResponseSentEventArgs e)
 {
     if (BeforeSendMessage != null)
     {
         await BeforeSendMessage.InvokeAsync(this, new MessageEventArgs(e.Message)).ConfigureAwait(false);
     }
 }
Ejemplo n.º 2
0
        private void ProcessEvent(Activity activity, BeforeSendMessage payload)
        {
            if (payload == null)
            {
                CollectorEventSource.Log.NullPayload("SendMessageListener.OnStartActivity");
                return;
            }

            Tracer.StartActiveSpanFromActivity(activity.OperationName, activity, SpanKind.Producer, out var span);
        }
Ejemplo n.º 3
0
        /// <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);
                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 += async(s, e) =>
                    {
                        if (MessageReceived != null)
                        {
                            await MessageReceived.InvokeAsync(this, new RawMessageEventArgs(e.Message, e.CorrelationId)).ConfigureAwait(false);
                        }
                        await Processor.ProcessAsync(e, _cTokenSource.Token).ConfigureAwait(false);
                    };
                }
                else
                {
                    QueueServer.RequestReceived += async(s, e) =>
                    {
                        if (MessageReceived != null)
                        {
                            await MessageReceived.InvokeAsync(this, new RawMessageEventArgs(e.Request, e.CorrelationId)).ConfigureAwait(false);
                        }
                        var result = await Processor.ProcessAsync(e, _cTokenSource.Token).ConfigureAwait(false);

                        if (result != ResponseMessage.NoResponse && result != null)
                        {
                            if (result is byte[] rBytes)
                            {
                                e.Response = rBytes;
                            }
                            else if (result is MultiArray <byte> mBytes)
                            {
                                e.Response = mBytes;
                            }
                            else
                            {
                                e.Response = QueueServer.SenderSerializer.Serialize(result);
                            }
                        }
                    };
                    QueueServer.BeforeSendResponse += async(s, e) =>
                    {
                        if (BeforeSendMessage != null)
                        {
                            await BeforeSendMessage.InvokeAsync(this, new RawMessageEventArgs(e.Message, e.CorrelationId)).ConfigureAwait(false);
                        }
                    };
                    QueueServer.ResponseSent += async(s, e) =>
                    {
                        if (MessageSent != null)
                        {
                            await MessageSent.InvokeAsync(this, new RawMessageEventArgs(e.Message, e.CorrelationId)).ConfigureAwait(false);
                        }
                    };
                }

                Processor.Init();
                QueueServer.StartListeners();
                Core.Log.InfoBasic("Messaging service started.");

                Core.Status.Attach(collection =>
                {
                    Core.Status.AttachChild(Processor, this);
                    Core.Status.AttachChild(QueueServer, this);
                });
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
                throw;
            }
        }
Ejemplo n.º 4
0
        /// <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;
            }
        }