Exemplo n.º 1
0
        /// <summary>
        /// Sends the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="action">The action.</param>
        protected void _Send(byte[] data, Action <ISenderProperties> action = null)
        {
            try
            {
                if (!IsOpen())
                {
                    Open();
                }

                try
                {
                    var props = _channel.CreateBasicProperties();
                    props.Persistent = _queueSettings.Persistent;

                    var senderProperties = new RMQSenderProperties()
                    {
                        Properties = props,
                        Exchange   = string.Empty,
                        RoutingKey = _queueSettings.QueueName,
                        Mandatory  = false,
                    };

                    action?.Invoke(senderProperties);

                    _channel.BasicPublish(
                        string.Empty,
                        _queueSettings.QueueName,
                        senderProperties.Mandatory,
                        props,
                        data);

                    return;
                }
                catch (Exception ex)
                {
                    var newException = RMQExceptionHandler.ExceptionHandler(_connection, ex);
                    if (newException != null)
                    {
                        throw newException;
                    }

                    throw;
                }
            }
            catch (MessageException ex)
            {
                switch (ex.ExceptionCode)
                {
                case MessageExceptionCode.LostConnection:
                    Close();
                    break;
                }

                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="action">The action.</param>
        protected void _Send(byte[] data, Action <ISenderProperties> action = null)
        {
            while (true)
            {
                if (!IsOpen())
                {
                    Open();
                }

                try
                {
                    var senderProperties = new RMQSenderProperties()
                    {
                        Properties = null,
                        Exchange   = _exchangeSettings.ExchangeName,
                        RoutingKey = string.Empty,
                        Mandatory  = false,
                    };

                    action?.Invoke(senderProperties);

                    _channel.BasicPublish(
                        senderProperties.Exchange,
                        senderProperties.RoutingKey,
                        senderProperties.Mandatory,
                        null,
                        data);

                    return;
                }
                catch (Exception ex)
                {
                    if ((ex is AlreadyClosedException) || (ex is IOException))
                    {
                        // retry
                        continue;
                    }

                    var newException = RMQExceptionHandler.ExceptionHandler(_connection, ex);
                    if (newException != null)
                    {
                        throw newException;
                    }

                    throw;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="action">The action.</param>
        protected void _Send(byte[] data, Action <ISenderProperties> action = null)
        {
            try
            {
                var connectionRetry = true;
                while (true)
                {
                    if (!IsOpen())
                    {
                        Open();
                    }

                    try
                    {
                        var senderProperties = new RMQSenderProperties
                        {
                            Properties = null,
                            Exchange   = _exchangeSettings.ExchangeName,
                            RoutingKey = string.Empty,
                            Mandatory  = false,
                        };

                        action?.Invoke(senderProperties);

                        _channel.BasicPublish(
                            senderProperties.Exchange,
                            senderProperties.RoutingKey,
                            senderProperties.Mandatory,
                            null,
                            data);

                        break;
                    }
                    catch (Exception ex)
                    {
                        var newException = RMQExceptionHandler.ExceptionHandler(_connection, ex);
                        if (newException != null)
                        {
                            if (newException.ExceptionCode == MessageExceptionCode.LostConnection &&
                                connectionRetry)
                            {
                                // try the reconnection cycle
                                connectionRetry = false;
                                continue;
                            }

                            throw newException;
                        }

                        throw;
                    }
                }
            }
            catch (MessageException ex)
            {
                switch (ex.ExceptionCode)
                {
                case MessageExceptionCode.LostConnection:
                    Close();
                    break;
                }

                throw;
            }
        }