示例#1
0
        private async Task <TMessageResult> Execute <TMessage, TMessageResult>(TMessage message, bool isNeedResult = true) where TMessageResult : MessageResult, new() where TMessage : BaseMessage
        {
            try
            {
                if (message == null)
                {
                    throw new ArgumentNullException("The message object is null!");
                }
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"接收到消息{message.GetType()},请求数据:{_jsonSerialzer.Serialize(message)}");
                }

                _contextManager.Create();
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"创建新Context成功,ContextId: {_currentContextProvider.Current.Id}");
                }
                var result = await ExecuteMessage <TMessage, TMessageResult>(message);

                _currentContextProvider.Current.Commit();
                _currentContextProvider.Current.Dispose();
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"Context执行结束,ContextId: {_currentContextProvider.Current.Id}");
                }
                CurrentRepositoryContextProvider.ReleaseCurrentContext();

                return(result);
            }
            catch (Exception ex)
            {
                if (_currentContextProvider.Current != null)
                {
                    _currentContextProvider.Current.Dispose();
                    CurrentRepositoryContextProvider.ReleaseCurrentContext();
                }

                var isEat = EnsureException.EatException(ref ex);
                if (isEat)
                {
                    _logger.Warn($"执行消息{message.GetType().Name}时业务失败,原因:{ex.Message}, data:{{message}}", null, new object[] { message });
                    return(new TMessageResult()
                    {
                        Code = ResultCode.BussinessError, Message = ex.Message
                    });
                }
                else
                {
                    _logger.Fatal($"系统在执行{typeof(TMessage)}消息的时候出现异常!原因:{ex.GetExceptionMessage()}, data: {{message}}", ex, new object[] { message });
                    return(new TMessageResult()
                    {
                        Code = ResultCode.Exception, Message = "服务异常"
                    });
                }
            }
        }
 protected Task RunWithNewContext(Func <Task> action)
 {
     return(Task.Run(async() =>
     {
         var context = _repositoryContextManager.Create();
         await action();
         _currentRepositoryContextProvider.Current.Commit();
         _currentRepositoryContextProvider.Current.Dispose();
         CurrentRepositoryContextProvider.ReleaseCurrentContext();
     }));
 }
示例#3
0
 /// <summary>
 /// Please use this method with caution!
 /// </summary>
 protected virtual void FlushToPersient()
 {
     lock (_sync)
     {
         var content    = _currentRepositoryContextProvider.Current;
         var contextKey = content.Id;
         using (content)
         {
             content.Commit();
         }
         _repositoryContextManager.Create();
         CurrentRepositoryContextProvider.ReplaceCurrentContextKey(contextKey);
     }
 }
 protected void FlushToPersistence()
 {
     lock (_sync)
     {
         var content    = _currentRepositoryContextProvider.Current;
         var contextKey = content.Id;
         if (content == null || content.IsDisposed)
         {
             throw new ObjectDisposedException($"对象已被释放!ID: {contextKey}");
         }
         using (content)
         {
             content.Commit();
         }
         _repositoryContextManager.Create();
         CurrentRepositoryContextProvider.ReplaceCurrentContextKey(contextKey);
     }
 }
        public async Task Handle(RabbitConnection connection, object model, BasicDeliverEventArgs e)
        {
            TMessage message;

            try
            {
                message = Deserialize(e.Body);
                if (message == null)
                {
                    _logger.Warn($"队列:{Queue},无法序列化消息,已跳过该条消息!消息数据:{GetErrorByteBody(e.Body)}");
                    connection.AckMessage(e.DeliveryTag);
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"队列:{Queue},无法序列化消息,已跳过该条消息!", ex);
                connection.AckMessage(e.DeliveryTag);
                return;
            }

            try
            {
                _logger.Info($"队列:{Queue},接收到MQ消息:{_jsonSerialzer.Serialize(message)}");
                _repositoryContextManager.Create();
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"创建新Context成功,ContextId: {_currentRepositoryContextProvider.Current.Id}");
                }
                await Handle(message);

                _currentRepositoryContextProvider.Current.Commit();
                _currentRepositoryContextProvider.Current.Dispose();
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"Context执行结束,ContextId: {_currentRepositoryContextProvider.Current.Id}");
                }
                CurrentRepositoryContextProvider.ReleaseCurrentContext();
                connection.AckMessage(e.DeliveryTag);
            }
            catch (Exception ex)
            {
                if (_currentRepositoryContextProvider.Current != null)
                {
                    _currentRepositoryContextProvider.Current.Dispose();
                    CurrentRepositoryContextProvider.ReleaseCurrentContext();
                }

                var isEat = EnsureException.EatException(ref ex);
                if (isEat)
                {
                    _logger.Warn($"队列:{Queue} 处理消息失败,消息体:{_jsonSerialzer.Serialize(message)},原因:{ex.Message},trace:{ex.GetCloseTrace()}");
                    connection.AckMessage(e.DeliveryTag);
                }
                else
                {
                    _logger.Fatal($"系统在执行{typeof(TMessage)}消息的时候出现异常!原因:{ex.GetExceptionMessage()}", ex);
                    Thread.Sleep(new Random().Next(1000, 30000));
                    connection.RejectMessage(e.DeliveryTag);
                }
            }
        }
        public void Handle(RabbitConnection connection, object model, BasicDeliverEventArgs e)
        {
            System.Threading.Thread.Sleep(50);
            TMessage message;

            try
            {
                message = Deserialize(e.Body);
                if (message == null)
                {
                    _logger.Warn($"队列:{Queue},无法序列化消息,已跳过该条消息!消息数据:{GetErrorByteBody(e.Body)}");
                    connection.AckMessage(e.DeliveryTag);
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"队列:{Queue},无法序列化消息,已跳过该条消息!", ex);
                connection.AckMessage(e.DeliveryTag);
                return;
            }

            try
            {
                _logger.Info($"队列:{Queue},接收到MQ消息:{_jsonSerialzer.Serialize(message)}");
                _repositoryContextManager.Create();
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"创建新Context成功,ContextId: {_currentRepositoryContextProvider.Current.Id}");
                }
                Handle(message);
                _currentRepositoryContextProvider.Current.Commit();
                _currentRepositoryContextProvider.Current.Dispose();
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"Context执行结束,ContextId: {_currentRepositoryContextProvider.Current.Id}");
                }
                CurrentRepositoryContextProvider.ReleaseCurrentContext();
                connection.AckMessage(e.DeliveryTag);
            }
            catch (EnsureException ex)
            {
                _logger.Warn($"队列:{Queue},消息{message.GetType().Name}处理失败,消息体:{_jsonSerialzer.Serialize(message)},原因:{ex.Message}", ex);
                connection.AckMessage(e.DeliveryTag);

                if (_currentRepositoryContextProvider.Current != null)
                {
                    _currentRepositoryContextProvider.Current.Dispose();
                    CurrentRepositoryContextProvider.ReleaseCurrentContext();
                }
            }
            catch (Exception ex)
            {
                var ensureException = ex;
                while (true)
                {
                    if (ensureException == null)
                    {
                        break;
                    }
                    if (ensureException is EnsureException)
                    {
                        break;
                    }
                    ensureException = ensureException.InnerException;
                }
                if (ensureException is EnsureException)
                {
                    _logger.Warn($"执行消息{message.GetType().Name}时业务失败,原因:{ensureException.Message}");
                    connection.AckMessage(e.DeliveryTag);

                    if (_currentRepositoryContextProvider.Current != null)
                    {
                        _currentRepositoryContextProvider.Current.Dispose();
                        CurrentRepositoryContextProvider.ReleaseCurrentContext();
                    }
                }
                else
                {
                    _logger.Fatal($"系统在执行{typeof(TMessage)}消息的时候出现异常!原因:{ex.GetExceptionMessage()}", ex);
                    if (_currentRepositoryContextProvider.Current != null)
                    {
                        _currentRepositoryContextProvider.Current.Dispose();
                        CurrentRepositoryContextProvider.ReleaseCurrentContext();
                    }
                    Thread.Sleep(new Random().Next(1000, 30000));
                    connection.RejectMessage(e.DeliveryTag);
                }
            }
        }