Example #1
0
        internal void Rollback()
        {
            lock (_syncRoot)
                lock ( _dispatchedMessages )
                {
                    if (_dispatchedMessages.Count == 0)
                    {
                        return;
                    }

                    // Only increase the redelivery delay after the first redelivery..
                    var lastMd = _dispatchedMessages.First();
                    var currentRedeliveryCount = lastMd.Message.RedeliveryCounter;

                    _redeliveryDelay = RedeliveryPolicy.RedeliveryDelay(currentRedeliveryCount);

                    foreach (var dispatch in _dispatchedMessages)
                    {
                        dispatch.Message.OnMessageRollback();
                    }

                    if (RedeliveryPolicy.MaximumRedeliveries >= 0 &&
                        lastMd.Message.RedeliveryCounter > RedeliveryPolicy.MaximumRedeliveries)
                    {
                        _redeliveryDelay = 0;
                    }
                    else
                    {
                        // stop the delivery of messages.
                        _unconsumedMessages.Stop();

                        foreach (var dispatch in _dispatchedMessages)
                        {
                            _unconsumedMessages.Enqueue(dispatch, true);
                        }

                        if (_redeliveryDelay > 0 && !_unconsumedMessages.Stopped)
                        {
                            var deadline = DateTime.Now.AddMilliseconds(_redeliveryDelay);
                            ThreadPool.QueueUserWorkItem(RollbackHelper, deadline);
                        }
                        else
                        {
                            Start();
                        }
                    }

                    _deliveredCounter -= _dispatchedMessages.Count;
                    _dispatchedMessages.Clear();
                }

            // Only redispatch if there's an async _listener otherwise a synchronous
            // consumer will pull them from the local queue.
            if (_listener != null)
            {
                _session.Redispatch(_unconsumedMessages);
            }
        }
Example #2
0
        public void Executes_redelivery_policy_without_backoff_enabled_correctly()
        {
            RedeliveryPolicy policy = new RedeliveryPolicy();

            policy.InitialRedeliveryDelay = 5;

            // simulate a retry of 10 times
            Assert.IsTrue(policy.RedeliveryDelay(0) == 0, "redelivery delay not 0 is " + policy.RedeliveryDelay(0));
            Assert.IsTrue(policy.RedeliveryDelay(1) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(1));
            Assert.IsTrue(policy.RedeliveryDelay(2) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(2));
            Assert.IsTrue(policy.RedeliveryDelay(3) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(3));
            Assert.IsTrue(policy.RedeliveryDelay(4) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(4));
            Assert.IsTrue(policy.RedeliveryDelay(5) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(5));
            Assert.IsTrue(policy.RedeliveryDelay(6) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(6));
            Assert.IsTrue(policy.RedeliveryDelay(7) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(7));
            Assert.IsTrue(policy.RedeliveryDelay(8) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(8));
            Assert.IsTrue(policy.RedeliveryDelay(9) == 5, "redelivery delay not 5 is " + policy.RedeliveryDelay(9));
        }
Example #3
0
        public void Executes_redelivery_policy_with_backoff_of_3_enabled_correctly()
        {
            RedeliveryPolicy policy = new RedeliveryPolicy();

            policy.BackOffMultiplier      = 3;
            policy.InitialRedeliveryDelay = 3;
            policy.UseExponentialBackOff  = true;

            // simulate a retry of 10 times
            Assert.IsTrue(policy.RedeliveryDelay(0) == 0, "redelivery delay not 5 is " + policy.RedeliveryDelay(0));
            Assert.IsTrue(policy.RedeliveryDelay(1) == 3, "redelivery delay not 10 is " + policy.RedeliveryDelay(1));
            Assert.IsTrue(policy.RedeliveryDelay(2) == 9, "redelivery delay not 20 is " + policy.RedeliveryDelay(2));
            Assert.IsTrue(policy.RedeliveryDelay(3) == 27, "redelivery delay not 40 is " + policy.RedeliveryDelay(3));
            Assert.IsTrue(policy.RedeliveryDelay(4) == 81, "redelivery delay not 80 is " + policy.RedeliveryDelay(4));
            Assert.IsTrue(policy.RedeliveryDelay(5) == 243, "redelivery delay not 160 is " + policy.RedeliveryDelay(5));
            Assert.IsTrue(policy.RedeliveryDelay(6) == 729, "redelivery delay not 320 is " + policy.RedeliveryDelay(6));
            Assert.IsTrue(policy.RedeliveryDelay(7) == 2187, "redelivery delay not 640 is " + policy.RedeliveryDelay(7));
            Assert.IsTrue(policy.RedeliveryDelay(8) == 6561, "redelivery delay not 1280 is " + policy.RedeliveryDelay(8));
            Assert.IsTrue(policy.RedeliveryDelay(9) == 19683, "redelivery delay not 2560 is " + policy.RedeliveryDelay(9));
        }
Example #4
0
        public void Executes_redelivery_policy_with_collision_enabled_correctly()
        {
            RedeliveryPolicy policy = new RedeliveryPolicy();

            policy.BackOffMultiplier         = 2;
            policy.InitialRedeliveryDelay    = 5;
            policy.UseExponentialBackOff     = true;
            policy.UseCollisionAvoidance     = true;
            policy.CollisionAvoidancePercent = 10;

            // simulate a retry of 10 times
            int delay = policy.RedeliveryDelay(0);

            Assert.IsTrue(delay == 0, "not zero is " + policy.RedeliveryDelay(0));
            delay = policy.RedeliveryDelay(1);
            Assert.IsTrue(delay >= 4.5 && delay <= 5.5, "not delay >= 4.5 && delay <= 5.5 is " + policy.RedeliveryDelay(1));
            delay = policy.RedeliveryDelay(2);
            Assert.IsTrue(delay >= 9 && delay <= 11, "not delay >= 9 && delay <= 11 is " + policy.RedeliveryDelay(2));
            delay = policy.RedeliveryDelay(3);
            Assert.IsTrue(delay >= 18 && delay <= 22, "not delay >= 18 && delay <= 22 is " + policy.RedeliveryDelay(3));
            delay = policy.RedeliveryDelay(4);
            Assert.IsTrue(delay >= 36 && delay <= 44, "not delay >= 36 && delay <= 44 is " + policy.RedeliveryDelay(4));
            delay = policy.RedeliveryDelay(5);
            Assert.IsTrue(delay >= 72 && delay <= 88, "not delay >= 72 && delay <= 88 is " + policy.RedeliveryDelay(5));
            delay = policy.RedeliveryDelay(6);
            Assert.IsTrue(delay >= 144 && delay <= 176, "not delay >= 144 && delay <= 176 is " + policy.RedeliveryDelay(6));
            delay = policy.RedeliveryDelay(7);
            Assert.IsTrue(delay >= 288 && delay <= 352, "not delay >= 288 && delay <= 352 is " + policy.RedeliveryDelay(7));
            delay = policy.RedeliveryDelay(8);
            Assert.IsTrue(delay >= 576 && delay <= 704, "not delay >= 576 && delay <= 704 is " + policy.RedeliveryDelay(8));
            delay = policy.RedeliveryDelay(9);
            Assert.IsTrue(delay >= 1152 && delay <= 1408, "not delay >= 1152 && delay <= 1408 is " + policy.RedeliveryDelay(9));
            delay = policy.RedeliveryDelay(10);
            Assert.IsTrue(delay >= 2304 && delay <= 2816, "not delay >= 2304 && delay <= 2816 is " + policy.RedeliveryDelay(10));
        }