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));
		}
        public void IsLimitExceeded_return_false_untill_specified_limit_reached()
        {
            const int retryLimit = 5;

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

            for (int i = 0; i < retryLimit; i++)
            {
                Assert.IsFalse(tracker.IsRetryLimitExceeded(id));
                tracker.IncrementRetryCount(id);
            }
            Assert.IsTrue(tracker.IsRetryLimitExceeded(id));
        }
        public void Should_return_false_until_the_limit_is_exceeded()
        {
            const int retryLimit = 5;

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

            Exception ex;

            for (int i = 0; i < retryLimit; i++)
            {
                Assert.IsFalse(tracker.IsRetryLimitExceeded(id, out ex));
                tracker.IncrementRetryCount(id, ex);
            }
            Assert.IsTrue(tracker.IsRetryLimitExceeded(id, out ex));
        }
示例#4
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;
                }
            }
        }
示例#5
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);
        }