示例#1
0
        public void TestCopyHeaders()
        {
            IEnumerable <string> xDeathHeaderNames = typeof(RabbitMqXDeathHeaders)
                                                     .GetFields(BindingFlags.Public | BindingFlags.Static)
                                                     .Where(fi => (fi.FieldType.IsAssignableFrom(typeof(string))))
                                                     .Select(x => (string)x.GetValue(null));

            Dictionary <string, object> source = xDeathHeaderNames.ToDictionary <string, string, object>(item => item, item => "test");

            source.Add("other", "garbage data");
            var target = new Dictionary <string, object>();

            Assert.DoesNotThrow(() => RabbitMqXDeathHeaders.CopyHeaders(source, target));

            source.Remove("other");
            Assert.True(source.Count == target.Count && !source.Except(target).Any());
        }
示例#2
0
        private void RepublishMessage(BasicDeliverEventArgs deliverArgs)
        {
            var exchangeForRepublish = (string)deliverArgs.BasicProperties.Headers[RabbitMqXDeathHeaders.XFirstDeathExchangeKey];

            try
            {
                _model.ExchangeDeclarePassive(exchangeForRepublish);
            }
            catch (OperationInterruptedException e)
            {
                // If an exchange doesn't exist, then we are forced to Fatal the host
                throw new ApplicationException("Exchange for republishing did not exist (\"" + exchangeForRepublish + "\")", e);
            }

            _props.Timestamp = new AmqpTimestamp(MessageHeader.UnixTimeNow());

            // Clear the existing headers so we can update them and re-add
            ClearMessageHeaders(_props.Headers);

            // Update the new message props with the existing data from the deliverArgs
            var existingHeader = new MessageHeader(deliverArgs.BasicProperties.Headers);

            new MessageHeader(existingHeader).Populate(_props.Headers);
            RabbitMqXDeathHeaders.CopyHeaders(deliverArgs.BasicProperties.Headers, _props.Headers);

            _model.BasicPublish(exchangeForRepublish, deliverArgs.RoutingKey, true, _props, deliverArgs.Body);

            try
            {
                _model.WaitForConfirmsOrDie(_confirmTimeout);
            }
            catch (Exception e)
            {
                // If we can't republish a certain message, send it to graveyard
                _logger.Error(e, "WaitForConfirmsOrDie Died");
                _deadLetterStore.SendToGraveyard(existingHeader.MessageGuid, "Couldn't republish", e);
                return;
            }

            _deadLetterStore.NotifyMessageRepublished(existingHeader.MessageGuid);
            ++TotalRepublished;
        }