Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AMQReceiver" /> class.
 /// </summary>
 /// <param name="connectionFactory">The connection factory.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destination">The destination.</param>
 public AMQReceiver(IAMQConnectionFactory connectionFactory, AMQConnectionSettings connectionSettings, AMQDestinationSettings destination) : base(connectionFactory, connectionSettings)
 {
     Mode = ConsumerMode.OnNoMessage_Timeout;
     _timeoutException = new TimeoutException();
     _destination      = destination;
     _consumer         = null;
 }
        /// <summary>
        /// Gets the connection settings.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public AMQConnectionSettings GetConnectionSettings(string name)
        {
            var attributes = GetAttributes("connectionSettings", name);

            var delayOnReconnectValue = StringHelper.FullTrim(StringHelper.IfNullOrEmptyUseDefault(attributes["delayOnReconnect"], "0"));
            var delayOnReconnect      = 0;

            if (!int.TryParse(delayOnReconnectValue, out delayOnReconnect) || delayOnReconnect < 0)
            {
                delayOnReconnect = 0;
            }

            var settings = new AMQConnectionSettings()
            {
                Name             = StringHelper.IfNullOrEmptyUseDefault(attributes["name"], string.Empty),
                Uri              = StringHelper.FullTrim(StringHelper.IfNullOrEmptyUseDefault(attributes["uri"], string.Empty)),
                Username         = StringHelper.IfNullOrEmptyUseDefault(attributes["username"], "admin"),
                Password         = StringHelper.IfNullOrEmptyUseDefault(attributes["password"], "admin"),
                AsyncSend        = StringHelper.IfNullOrEmptyUseDefault(attributes["asyncSend"], "true") == "true",
                DelayOnReconnect = delayOnReconnect,
            };

            foreach (var uri in settings.Uri.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                settings.Endpoints.Add(uri);
            }

            return(settings);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AMQReceiverSender"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public AMQReceiverSender(IAMQConnectionFactory connectionFactory, AMQConnectionSettings connectionSettings)
 {
     Disposed            = false;
     _connectionSettings = connectionSettings;
     _connectionFactory  = connectionFactory;
     _connection         = null;
     _session            = null;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Declares the connection.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="MessageException">
        /// </exception>
        public IConnection CreateConnection(AMQConnectionSettings settings)
        {
            if (settings.Endpoints.Count == 0)
            {
                throw new MessageException(MessageExceptionCode.NoAcceptableEndpoints, "There are no acceptable endpoints to connect any ActiveMQ Broker.");
            }

            var exceptionList = new List <Exception>();
            var retries       = settings.Endpoints.Count - 1;

            while (true)
            {
                try
                {
                    var connectionUri     = new Uri(settings.Endpoints[settings.ActiveEndpointIndex]);
                    var connectionFactory = new ConnectionFactory(connectionUri)
                    {
                        UserName  = settings.Username,
                        Password  = settings.Password,
                        AsyncSend = settings.AsyncSend,
                    };

                    var connection = connectionFactory.CreateConnection();
                    return(connection);
                }
                catch (NMSConnectionException ex)
                {
                    exceptionList.Add(ex);
                }
                catch (InvalidClientIDException ex)
                {
                    exceptionList.Add(ex);
                }
                catch (NMSException ex)
                {
                    exceptionList.Add(ex);
                }
                catch (UriFormatException ex)
                {
                    exceptionList.Add(ex);
                }

                retries--;
                if (retries < 0)
                {
                    var finalException = new AggregateException($"Unable to connect to any ActiveMQ Broker using the following connection uri: {settings.Uri}, for named connection settings: '{settings.Name}'.", exceptionList);
                    throw new MessageException(MessageExceptionCode.LostConnection, finalException.Message, finalException);
                }

                settings.ActiveEndpointIndex++;
                if (settings.ActiveEndpointIndex >= settings.Endpoints.Count)
                {
                    settings.ActiveEndpointIndex = 0;
                }

                Thread.Sleep(settings.DelayOnReconnect);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="connection">The connection settings.</param>
        /// <param name="destination">The destination settings.</param>
        /// <returns></returns>
        /// <exception cref="KeyNotFoundException"></exception>
        public IMessageReceiverSender Create(Type type, AMQConnectionSettings connection, AMQDestinationSettings destination)
        {
            if (!_invokers.ContainsKey(type))
            {
                throw new KeyNotFoundException($"The following type: '{type.Name}' is not supported.");
            }

            var invoker = _invokers[type];

            return(invoker(connection, destination));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AMQObjectPool" /> class.
 /// </summary>
 /// <param name="factory">The factory.</param>
 /// <param name="instanceType">Type of the instance.</param>
 /// <param name="connection">The connection.</param>
 /// <param name="destination">The destination.</param>
 /// <param name="maxGrowSize">Maximum size of the grow.</param>
 public AMQObjectPoolAsync(
     IAMQSenderReceiverFactory factory,
     Type instanceType,
     AMQConnectionSettings connection,
     AMQDestinationSettings destination,
     int maxGrowSize) : base(0, maxGrowSize)
 {
     _factory      = factory;
     _instanceType = instanceType;
     _connection   = connection;
     _destination  = destination;
 }
Ejemplo n.º 7
0
        static AMQConfigurationManager CreateConfigurationManager()
        {
            var connectionSettings = new AMQConnectionSettings
            {
                Name             = "defaultConnection",
                Username         = "******",
                Password         = "******",
                Uri              = "tcp://localhost:61616?tcpNoDelay=true,tcp://localhost:61616?tcpNoDelay=true",
                DelayOnReconnect = 1000,
            };

            var destinationSettings = new AMQDestinationSettings("MY_AMQ_TEST", "queue://MY_AMQ_TEST")
            {
                DeliveryMode = MsgDeliveryMode.Persistent,
                Durable      = true,
            };

            return(new AMQConfigurationManager(new[] { connectionSettings }, new[] { destinationSettings }));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AMQSender" /> class.
 /// </summary>
 /// <param name="connectionFactory">The connection factory.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destination">The destination.</param>
 public AMQSender(IAMQConnectionFactory connectionFactory, AMQConnectionSettings connectionSettings, AMQDestinationSettings destination) : base(connectionFactory, connectionSettings)
 {
     _destination = destination;
     _producer    = null;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AMQSenderText" /> class.
 /// </summary>
 /// <param name="connectionFactory">The connection factory.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destination">The destination.</param>
 public AMQSenderBytes(IAMQConnectionFactory connectionFactory, AMQConnectionSettings connectionSettings, AMQDestinationSettings destination) : base(connectionFactory, connectionSettings, destination)
 {
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates the bytes.
 /// </summary>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destinationSettings">The destination settings.</param>
 /// <returns></returns>
 public IAMQSenderBytes CreateBytes(AMQConnectionSettings connectionSettings, AMQDestinationSettings destinationSettings)
 {
     return(new AMQSenderBytes(_connectionFactory, connectionSettings, destinationSettings));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Creates the text.
 /// </summary>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destinationSettings">The destination settings.</param>
 /// <returns></returns>
 public IAMQSenderText CreateText(AMQConnectionSettings connectionSettings, AMQDestinationSettings destinationSettings)
 {
     return(new AMQSenderText(_connectionFactory, connectionSettings, destinationSettings));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates the specified connection settings.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="connection">The connection settings.</param>
 /// <param name="destination">The destination settings.</param>
 /// <returns></returns>
 public T Create <T>(AMQConnectionSettings connection, AMQDestinationSettings destination) where T : IMessageReceiverSender
 {
     return((T)Create(typeof(T), connection, destination));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates the specified settings.
 /// </summary>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destinationSettings">The settings.</param>
 /// <returns></returns>
 public IAMQReceiver Create(AMQConnectionSettings connectionSettings, AMQDestinationSettings destinationSettings)
 {
     return(new AMQReceiver(_connectionFactory, connectionSettings, destinationSettings));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AMQReceiverText" /> class.
 /// </summary>
 /// <param name="connectionFactory">The connection factory.</param>
 /// <param name="connectionSettings">The connection settings.</param>
 /// <param name="destination">The destination.</param>
 public AMQReceiverText(IAMQConnectionFactory connectionFactory, AMQConnectionSettings connectionSettings, AMQDestinationSettings destination) : base(connectionFactory, connectionSettings, destination)
 {
 }