Ejemplo n.º 1
0
        /// <summary>
        /// Creates the map.
        /// </summary>
        /// <returns></returns>
        public IMapMessage CreateMapMessage()
        {
            while (true)
            {
                Open();

                try
                {
                    return(_session.CreateMapMessage());
                }
                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;
                }
            }
        }
Ejemplo n.º 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)
        {
            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;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Receives the message.
        /// </summary>
        /// <param name="msecTimeout">The msec timeout.</param>
        /// <returns></returns>
        public IMessage ReceiveMessage(int msecTimeout = Timeout.Infinite)
        {
            try
            {
                Open();

                if (_consumer == null)
                {
                    var destination = SessionUtil.GetDestination(_session, _destination.Path);
                    if (_destination.Durable && destination.IsTopic)
                    {
                        _consumer = _session.CreateDurableConsumer((ITopic)destination, _destination.SubscriberId, _destination.Selector, false);
                    }
                    else
                    {
                        _consumer = _session.CreateConsumer(destination, _destination.Selector, false);
                    }
                }

                IMessage message;
                if (msecTimeout != Timeout.Infinite)
                {
                    message = _consumer.Receive(TimeSpan.FromMilliseconds(msecTimeout));
                }
                else
                {
                    message = _consumer.Receive();
                }

                if (message == null && Mode == ConsumerMode.OnNoMessage_Timeout)
                {
                    throw _timeoutException;
                }

                return(message);
            }
            catch (TimeoutException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Close();

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

                throw;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Clears the cache buffer.
        /// </summary>
        public void ClearCacheBuffer()
        {
            if (_session != null)
            {
                try
                {
                    _session.Recover();
                }
                catch (Exception ex)
                {
                    Close();

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

                    throw;
                }
            }
        }
Ejemplo n.º 5
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;
            }
        }