Beispiel #1
0
        protected void Publish(string exchange, string routingKey, ITopology topology, MessageProperties properties,
                               byte[] messageBody, Action <IPublishConfiguration> configure)
        {
            if (disposed)
            {
                throw new Exception("PublishChannel is already disposed");
            }
            if (!_rabbitMessageBus.PersistentConnection.IsConnected)
            {
                throw new Exception("Publish failed. No rabbit server connected.");
            }

            try
            {
                var configuration = new AdvancedPublishConfiguration();
                if (configure != null)
                {
                    configure(configuration);
                }

                if (_publisherConfirms != null)
                {
                    //if (configuration.SuccessCallback == null || configuration.FailureCallback == null)
                    //{
                    //    throw new Exception("When pulisher confirms are on, you must supply success and failure callbacks in the publish configuration");
                    //}

                    _publisherConfirms.RegisterCallbacks(_channel,
                                                         configuration.SuccessCallback != null ? configuration.SuccessCallback : () => { },
                                                         configuration.FailureCallback != null ? configuration.FailureCallback : () => { });
                }

                var defaultProperties = _channel.CreateBasicProperties();
                properties.CopyTo(defaultProperties);

                if (topology != null)
                {
                    topology.Visit(new TopologyBuilder(_channel));
                }

                _channel.BasicPublish(
                    exchange,          // exchange
                    routingKey,        // routingKey
                    defaultProperties, // basicProperties
                    messageBody);      // body

                _rabbitMessageBus.Logger.Debug(string.Format("Published to exchange: '{0}', routing key: '{1}', correlationId: '{2}'",
                                                             exchange, routingKey, defaultProperties.CorrelationId));
            }
            catch (OperationInterruptedException exception)
            {
                throw new Exception(string.Format("Publish Failed: '{0}'", exception.Message));
            }
            catch (IOException exception)
            {
                throw new Exception(string.Format("Publish Failed: '{0}'", exception.Message));
            }
        }
        public void Should_copy_to_rabbit_client_properties()
        {
            const string replyTo = "reply to";

            var properties = new MessageProperties { ReplyTo = replyTo };
            var destinationProperties = new BasicProperties();

            properties.CopyTo(destinationProperties);

            destinationProperties.ReplyTo.ShouldEqual(replyTo);
            destinationProperties.IsReplyToPresent().ShouldBeTrue();
            destinationProperties.IsMessageIdPresent().ShouldBeFalse();
        }
        private static void RetryMessage(ConsumerExecutionContext context, IModel model, MessageProperties properties, int flrRetry)
        {
            // Ensure and update the retrycount
            properties.Headers.Remove("easynetq.retry.count");
            properties.Headers.Add("easynetq.retry.count", flrRetry);

            // Copy all properties to BasicProperties for the Rabbit
            var basicProperties = model.CreateBasicProperties();

            properties.CopyTo(basicProperties);

            // Resend the message
            model.BasicPublish(context.Info.Exchange, context.Info.RoutingKey, basicProperties, context.Body);
        }
Beispiel #4
0
        public void Should_copy_to_rabbit_client_properties()
        {
            const string replyTo = "reply to";

            var properties = new MessageProperties {
                ReplyTo = replyTo
            };
            var destinationProperties = new BasicProperties();

            properties.CopyTo(destinationProperties);

            destinationProperties.ReplyTo.ShouldEqual(replyTo);
            destinationProperties.IsReplyToPresent().ShouldBeTrue();
            destinationProperties.IsMessageIdPresent().ShouldBeFalse();
        }
        private void SendMessageToErrorQueue(ConsumerExecutionContext context, Exception exception, IModel model, MessageProperties properties)
        {
            var errorExchange = DeclareErrorExchangeQueueStructure(model, context);

            _logger.InfoWrite(
                $"(DEAD LETTERED) Second Level Retry max reached for message of type [{properties.Type}], message is sent to dead letter queue: [{errorExchange}].");

            var messageBody = CreateDefaultErrorMessage(context, exception.InnerException);

            var errorProperties = model.CreateBasicProperties();

            properties.CopyTo(errorProperties);
            errorProperties.Persistent = true;
            errorProperties.Type       = _typeNameSerializer.Serialize(typeof(RabbitErrorMessage));

            model.BasicPublish(errorExchange, context.Info.RoutingKey, errorProperties, messageBody);
        }