public void Should_reset_once_the_message_was_received()
		{
			const int retryLimit = 5;

			var tracker = new MessageRetryTracker(retryLimit);
			const string id = "qelofjsw";

			Assert.IsFalse(tracker.IsRetryLimitExceeded(id));
			tracker.IncrementRetryCount(id);

			tracker.MessageWasReceivedSuccessfully(id);

			for (int i = 0; i < retryLimit; i++)
			{
				Assert.IsFalse(tracker.IsRetryLimitExceeded(id));
				tracker.IncrementRetryCount(id);
			}
			Assert.IsTrue(tracker.IsRetryLimitExceeded(id));
		}
Ejemplo n.º 2
0
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(_disposedMessage);
            }

            string successfulMessageId = null;

            try
            {
                Exception failedMessageException = null;

                _transport.Receive(acceptContext =>
                {
                    failedMessageException = null;

                    if (successfulMessageId != null)
                    {
                        _log.DebugFormat("Received Successfully: {0}", successfulMessageId);

                        _tracker.MessageWasReceivedSuccessfully(successfulMessageId);
                        successfulMessageId = null;
                    }

                    Exception retryException;
                    if (_tracker.IsRetryLimitExceeded(acceptContext.MessageId, out retryException))
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.ErrorFormat("Message retry limit exceeded {0}:{1}", Address,
                                             acceptContext.MessageId);
                        }

                        failedMessageException = retryException;

                        return(MoveMessageToErrorTransport);
                    }

                    Action <IReceiveContext> receive;
                    try
                    {
                        acceptContext.SetEndpoint(this);
                        _serializer.Deserialize(acceptContext);

                        receive = receiver(acceptContext);
                        if (receive == null)
                        {
                            Address.LogSkipped(acceptContext.MessageId);

                            _tracker.IncrementRetryCount(acceptContext.MessageId, null);
                            return(null);
                        }
                    }
                    catch (SerializationException sex)
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.Error("Unrecognized message " + Address + ":" + acceptContext.MessageId, sex);
                        }

                        _tracker.IncrementRetryCount(acceptContext.MessageId, sex);
                        return(MoveMessageToErrorTransport);
                    }
                    catch (Exception ex)
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.Error("An exception was thrown preparing the message consumers", ex);
                        }

                        _tracker.IncrementRetryCount(acceptContext.MessageId, ex);
                        return(null);
                    }

                    return(receiveContext =>
                    {
                        bool receivedSuccessfully;

                        try
                        {
                            receive(receiveContext);

                            //                                    _tracker.MessageWasReceivedSuccessfully(receiveContext.MessageId);
                            receivedSuccessfully = true;
                        }
                        catch (MessageNotConsumedException ex)
                        {
                            receivedSuccessfully = false;

                            _tracker.MessageWasReceivedSuccessfully(receiveContext.MessageId);
                            MoveMessageToErrorTransport(receiveContext);
                        }
                        catch (Exception ex)
                        {
                            receivedSuccessfully = false;

                            if (_log.IsErrorEnabled)
                            {
                                _log.Error("An exception was thrown by a message consumer", ex);
                            }

                            _tracker.IncrementRetryCount(receiveContext.MessageId, ex);
                            MoveMessageToErrorTransport(receiveContext);
                        }

                        if (receivedSuccessfully)
                        {
                            successfulMessageId = receiveContext.MessageId;
                        }
                    });
                }, timeout);

                if (failedMessageException != null)
                {
                    _log.DebugFormat("Throwing Original Exception: {0}", failedMessageException.GetType());

                    throw failedMessageException;
                }
            }
            catch (Exception ex)
            {
                if (successfulMessageId != null)
                {
                    _log.DebugFormat("Increment Retry Count: {0}", successfulMessageId);

                    _tracker.IncrementRetryCount(successfulMessageId, ex);
                    successfulMessageId = null;
                }
                throw;
            }
            finally
            {
                if (successfulMessageId != null)
                {
                    _log.DebugFormat("Received Successfully: {0}", successfulMessageId);

                    _tracker.MessageWasReceivedSuccessfully(successfulMessageId);
                    successfulMessageId = null;
                }
            }
        }
Ejemplo n.º 3
0
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(_disposedMessage);
            }

            _transport.Receive(acceptContext =>
            {
                if (_tracker.IsRetryLimitExceeded(acceptContext.MessageId))
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.ErrorFormat("Message retry limit exceeded {0}:{1}", Address, acceptContext.MessageId);
                    }

                    return(MoveMessageToErrorTransport);
                }

                Action <IReceiveContext> receive;
                try
                {
                    _serializer.Deserialize(acceptContext);
                    acceptContext.SetEndpoint(this);

                    receive = receiver(acceptContext);
                    if (receive == null)
                    {
                        return(null);
                    }
                }
                catch (SerializationException sex)
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.Error("Unrecognized message " + Address + ":" + acceptContext.MessageId, sex);
                    }

                    _tracker.IncrementRetryCount(acceptContext.MessageId);
                    return(MoveMessageToErrorTransport);
                }
                catch (Exception ex)
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.Error("An exception was thrown preparing the message consumers", ex);
                    }

                    _tracker.IncrementRetryCount(acceptContext.MessageId);
                    return(null);
                }

                return(receiveContext =>
                {
                    try
                    {
                        receive(receiveContext);

                        _tracker.MessageWasReceivedSuccessfully(receiveContext.MessageId);
                    }
                    catch (Exception ex)
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.Error("An exception was thrown by a message consumer", ex);
                        }

                        _tracker.IncrementRetryCount(receiveContext.MessageId);
                        MoveMessageToErrorTransport(receiveContext);
                    }
                });
            }, timeout);
        }