Пример #1
0
        /// <summary>
        /// Sends the specified data.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="createMessage">The create message.</param>
        public void Send(Action <ISenderProperties> action, Func <ISession, IMessage> createMessage)
        {
            while (true)
            {
                Open();

                try
                {
                    if (_producer == null)
                    {
                        var destination = SessionUtil.GetDestination(_session, _destination.Path);
                        _producer = _session.CreateProducer(destination);
                    }

                    var senderProperties = new AMQSenderProperties()
                    {
                        Properties = new NameValueCollection()
                    };

                    action?.Invoke(senderProperties);

                    var request = createMessage(_session);
                    request.NMSCorrelationID = senderProperties.CorrelationId;
                    request.NMSTimeToLive    = senderProperties.TTL;

                    foreach (var key in senderProperties.Properties.AllKeys)
                    {
                        request.Properties[key] = senderProperties.Properties[key];
                    }

                    _producer.Send(request);

                    return;
                }
                catch (Exception ex)
                {
                    Close();

                    if ((ex is NMSConnectionException) || (ex is IOException))
                    {
                        // retry
                        continue;
                    }

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

                    throw;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Sends the specified data.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="createMessage">The create message.</param>
        public void Send(Action <ISenderProperties> action, Func <ISession, IMessage> createMessage)
        {
            try
            {
                var connectionRetry = true;
                while (true)
                {
                    Open();

                    try
                    {
                        if (_producer == null)
                        {
                            var destination = SessionUtil.GetDestination(_session, _destination.Path);
                            _producer = _session.CreateProducer(destination);
                        }

                        var senderProperties = new AMQSenderProperties()
                        {
                            Properties = new NameValueCollection()
                        };

                        action?.Invoke(senderProperties);

                        var request = createMessage(_session);
                        request.NMSCorrelationID = senderProperties.CorrelationId;
                        request.NMSTimeToLive    = senderProperties.TTL;

                        foreach (var key in senderProperties.Properties.AllKeys)
                        {
                            request.Properties[key] = senderProperties.Properties[key];
                        }

                        _producer.Send(request);

                        return;
                    }
                    catch (Exception ex)
                    {
                        Close();

                        var newException = AMQExceptionHandler.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;
            }
        }