Ejemplo n.º 1
0
        /// <summary>
        /// Create a tunnel with provided params
        /// </summary>
        /// <param name="hostName"></param>
        /// <param name="port"></param>
        /// <param name="virtualHost"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="watcher"></param>
        /// <returns></returns>
        public virtual ITunnel Create(string hostName, int port, string virtualHost, string username, string password, IRabbitWatcher watcher)
        {
            var connectionFactory = new ManagedConnectionFactory
            {
                HostName    = hostName,
                Port        = port,
                VirtualHost = virtualHost,
                UserName    = username,
                Password    = password
            };

            return(Create(connectionFactory, watcher));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create queues required by Burrow.RPC library
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="createExchangesAndQueues"></param>
        public void CreateQueues(string connectionString, Action <IModel> createExchangesAndQueues)
        {
            var clusterConnections = connectionString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            if (clusterConnections.Length > 1)
            {
                Global.DefaultWatcher.InfoFormat("Found multiple Connection String, will use '{0}' to setup queues", clusterConnections[0]);
            }
            ConnectionString connectionValues = clusterConnections.Length > 1
                                              ? new ConnectionString(clusterConnections[0])
                                              : new ConnectionString(connectionString);


            var connectionFactory = new ManagedConnectionFactory
            {
                HostName    = connectionValues.Host,
                Port        = connectionValues.Port,
                VirtualHost = connectionValues.VirtualHost,
                UserName    = connectionValues.UserName,
                Password    = connectionValues.Password,
            };

            using (var connection = connectionFactory.CreateConnection())
            {
                using (var model = connection.CreateModel())
                {
                    try
                    {
                        createExchangesAndQueues(model);
                    }
                    catch (OperationInterruptedException oie)
                    {
                        if (oie.ShutdownReason.ReplyText.StartsWith("PRECONDITION_FAILED - "))
                        {
                            Global.DefaultWatcher.ErrorFormat(oie.ShutdownReason.ReplyText);
                        }
                        else
                        {
                            Global.DefaultWatcher.Error(oie);
                        }
                    }
                    catch (Exception ex)
                    {
                        Global.DefaultWatcher.Error(ex);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public override ITunnel Create(string hostName, int port, string virtualHost, string username, string password, IRabbitWatcher watcher)
        {
            var rabbitWatcher     = watcher ?? _burrowResolver.Resolve <IRabbitWatcher>() ?? Global.DefaultWatcher;
            var serializer        = _burrowResolver.Resolve <ISerializer>() ?? Global.DefaultSerializer;
            var connectionFactory = new ManagedConnectionFactory
            {
                HostName    = hostName,
                Port        = port,
                VirtualHost = virtualHost,
                UserName    = username,
                Password    = password
            };
            var durableConnection = new DurableConnection(_burrowResolver.Resolve <IRetryPolicy>() ?? new DefaultRetryPolicy(),
                                                          rabbitWatcher,
                                                          connectionFactory);

            return(Create(durableConnection, serializer, rabbitWatcher));
        }
Ejemplo n.º 4
0
        public void Should_copy_all_value_from_privided_factory()
        {
            // Arrange
            var originalFactory = new ConnectionFactory
            {
                HostName    = "localhost",
                Port        = 5672,
                VirtualHost = "/",
                UserName    = "******",
                Password    = "******"
            };

            // Action
            var factory = new ManagedConnectionFactory(originalFactory);


            // Assert
            Assert.AreEqual(JsonConvert.SerializeObject(originalFactory), JsonConvert.SerializeObject(factory));
        }
Ejemplo n.º 5
0
        public override ITunnel Create(string hostName, int port, string virtualHost, string username, string password, IRabbitWatcher watcher)
        {
            if (RabbitTunnel.Factory is DependencyInjectionTunnelFactory)
            {
                return(RabbitTunnel.Factory.Create(hostName, port, virtualHost, username, password, watcher));
            }

            var rabbitWatcher     = watcher ?? Global.DefaultWatcher;
            var connectionFactory = new ManagedConnectionFactory
            {
                HostName    = hostName,
                Port        = port,
                VirtualHost = virtualHost,
                UserName    = username,
                Password    = password
            };

            var durableConnection = new DurableConnection(new DefaultRetryPolicy(), rabbitWatcher, connectionFactory);


            return(Create(durableConnection, rabbitWatcher));
        }
Ejemplo n.º 6
0
        public void Should_subscribe_to_ConnectionEstablished_event()
        {
            var connectionFactory = new ManagedConnectionFactory
            {
                HostName    = "localhost",
                Port        = 5672,
                VirtualHost = "/",
                UserName    = "******",
                Password    = "******"
            };

            using (var connection = new HaConnection(Substitute.For <IRetryPolicy>(),
                                                     Substitute.For <IRabbitWatcher>(),
                                                     new List <ManagedConnectionFactory>
            {
                connectionFactory
            }))
            {
                var fired = false;
                connection.Connected += () => { fired = true; };
                ManagedConnectionFactory.ConnectionEstablished += (a, b) => { }; //NOTE: To make it not null


                var model = Substitute.For <IModel>();
                model.IsOpen.Returns(true);
                var c = Substitute.For <IConnection>();
                c.CreateModel().Returns(model);
                c.IsOpen.Returns(true);
                c.Endpoint.Returns(connectionFactory.Endpoint);
                ManagedConnectionFactory.SharedConnections[connectionFactory.Endpoint + connectionFactory.VirtualHost] = c;

                ManagedConnectionFactory.ConnectionEstablished.Invoke(new AmqpTcpEndpoint("localhost"), "/");


                Assert.IsTrue(fired);
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// This method should only be called before close the main app. It will close all openning connections to RabbitMQ server
 /// </summary>
 public void CloseAllConnections()
 {
     ManagedConnectionFactory.CloseAllConnections();
 }