Example #1
0
        public override void HandleMessage(ReceivedMessageArgs messageObject)
        {
            Console.WriteLine($"{_identifier} message received");

            using (var scope = _serviceProvider.CreateScope())
            {
                Type messageType = GetMessageTypeForAction(messageObject.Action);
                var  message     = DeserializeMessage(messageObject, messageType);

                var requestsRepository = scope.ServiceProvider.GetRequiredService <RequestsRepository>();
                var repository         = scope.ServiceProvider.GetRequiredService <TRepository>();

                if (CheckIsHandleRequired(message, requestsRepository))
                {
                    try
                    {
                        HandleMessageInternal(message, messageObject.Action, repository);
                    }
                    catch (Exception)
                    {
                        requestsRepository.DeleteRequestIdAsync(message.Id).GetAwaiter().GetResult();
                        throw;
                    }
                }
                else
                {
                    Console.WriteLine($"{_identifier} message already handled: {message.Id}");
                }
            }
        }
Example #2
0
        public async Task <string> HandleMessageAsync(ReceivedMessageArgs messageObject)
        {
            var baseMessage = JsonConvert.DeserializeObject <BaseTransactionMessage>(messageObject.Message);

            if (await _requestsRepository.IsHandledOrSaveRequestAsync(baseMessage.Id, GetRequestIdInvalidationDate()))
            {
                return($"Already handled: {baseMessage.Id}");
            }

            try
            {
                switch (messageObject.Action)
                {
                case TransactionMessageActions.MoveTask_Complete:
                    return(await CompleteTransactionAsync(messageObject.Message));

                case TransactionMessageActions.MoveTask_Rollback:
                    return(await RollbackTransactionAsync(messageObject.Message));

                default:
                    return(await HandleMessageInternalAsync(messageObject.Message, messageObject.Action));
                }
            }
            catch
            {
                await _requestsRepository.DeleteRequestIdAsync(baseMessage.Id);

                throw;
            }
        }
Example #3
0
 protected virtual void PerformSafetyOnBgThread(Func <Task> action, ReceivedMessageArgs messageObject, TaskScheduler scheduler)
 {
     Task.Run(async() =>
     {
         await action();
     })
     .ContinueWith((t) =>
     {
         if (t.IsFaulted)
         {
             HandleMessageFailed(messageObject, t.Exception);
         }
     }, scheduler);
 }
        private void HandleMessage(object model, BasicDeliverEventArgs e)
        {
            try
            {
                Console.WriteLine($"Message received!");
                var    body         = e.Body.ToArray();
                var    message      = Encoding.UTF8.GetString(body);
                var    headers      = e.BasicProperties?.Headers;
                string actionHeader = GetHeaderValue(headers, _actionHeaderName);

                string topic = e.RoutingKey;

                var args = new ReceivedMessageArgs(topic, actionHeader, message);

                MessageReceived?.Invoke(args);

                _channel.BasicAck(e.DeliveryTag, false);
                Console.WriteLine($"Message acked!");
            }
            catch (Exception exc)
            {
                Console.WriteLine($"{exc.Message}\n{exc.StackTrace}");
            }
        }
Example #5
0
 protected virtual void HandleMessageFailed(ReceivedMessageArgs messageObject, Exception e)
 {
     throw e;
 }
Example #6
0
 protected abstract void OnMessageReceived(ReceivedMessageArgs messageObject);
Example #7
0
 protected virtual BaseMessage DeserializeMessage(ReceivedMessageArgs messageObject, Type type)
 {
     return(JsonConvert.DeserializeObject(messageObject.Message, type) as BaseMessage);
 }