Example #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));
            }
        }
Example #2
0
        public virtual void Publish(IExchange exchange, string routingKey, MessageProperties properties, byte[] messageBody, Action <IAdvancedPublishConfiguration> configure)
        {
            Preconditions.CheckNotNull(exchange, "exchange");
            Preconditions.CheckNotNull(routingKey, "routingKey");
            Preconditions.CheckNotNull(properties, "properties");
            Preconditions.CheckNotNull(messageBody, "messageBody");
            Preconditions.CheckNotNull(configure, "configure");

            if (disposed)
            {
                throw new EasyNetQException("PublishChannel is already disposed");
            }
            if (!advancedBus.Connection.IsConnected)
            {
                throw new EasyNetQException("Publish failed. No rabbit server connected.");
            }

            try
            {
                var configuration = new AdvancedPublishConfiguration();
                configure(configuration);

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

                    publisherConfirms.RegisterCallbacks(channel, configuration.SuccessCallback, configuration.FailureCallback);
                }

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

                exchange.Visit(new TopologyBuilder(channel));

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

                advancedBus.Logger.DebugWrite("Published to exchange: '{0}', routing key: '{1}', correlationId: '{2}'",
                                              exchange.Name, routingKey, defaultProperties.CorrelationId);
            }
            catch (OperationInterruptedException exception)
            {
                throw new EasyNetQException("Publish Failed: '{0}'", exception.Message);
            }
            catch (System.IO.IOException exception)
            {
                throw new EasyNetQException("Publish Failed: '{0}'", exception.Message);
            }
        }