コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        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);
                }
            }
        }