internal void TestDelayedBasicAckNackAfterChannelRecovery(TestBasicConsumer1 cons, ManualResetEventSlim latch)
        {
            string q = Model.QueueDeclare(GenerateQueueName(), false, false, false, null).QueueName;
            int    n = 30;

            Model.BasicQos(0, 1, false);
            Model.BasicConsume(q, false, cons);

            AutorecoveringConnection publishingConn = CreateAutorecoveringConnection();
            IModel publishingModel = publishingConn.CreateModel();

            for (int i = 0; i < n; i++)
            {
                publishingModel.BasicPublish("", q, null, encoding.GetBytes(""));
            }

            Wait(latch, TimeSpan.FromSeconds(20));
            Model.QueueDelete(q);
            publishingModel.Close();
            publishingConn.Close();
        }
        public void TestConsumerRecoveryOnClientNamedQueueWithOneRecovery()
        {
            string q0 = "dotnet-client.recovery.queue1";

            using (AutorecoveringConnection c = CreateAutorecoveringConnection())
            {
                IModel m  = c.CreateModel();
                string q1 = m.QueueDeclare(q0, false, false, false, null).QueueName;
                Assert.AreEqual(q0, q1);

                var cons = new EventingBasicConsumer(m);
                m.BasicConsume(q1, true, cons);
                AssertConsumerCount(m, q1, 1);

                bool queueNameChangeAfterRecoveryCalled = false;

                c.QueueNameChangeAfterRecovery += (source, ea) => { queueNameChangeAfterRecoveryCalled = true; };

                CloseAndWaitForRecovery(c);
                AssertConsumerCount(m, q1, 1);
                Assert.False(queueNameChangeAfterRecoveryCalled);

                CloseAndWaitForRecovery(c);
                AssertConsumerCount(m, q1, 1);
                Assert.False(queueNameChangeAfterRecoveryCalled);

                CloseAndWaitForRecovery(c);
                AssertConsumerCount(m, q1, 1);
                Assert.False(queueNameChangeAfterRecoveryCalled);

                var latch = new ManualResetEventSlim(false);
                cons.Received += (s, args) => latch.Set();

                m.BasicPublish("", q1, null, _encoding.GetBytes("msg"));
                Wait(latch);

                m.QueueDelete(q1);
            }
        }
        public void TestConsumerWorkServiceRecovery()
        {
            using (AutorecoveringConnection c = CreateAutorecoveringConnection())
            {
                IModel m = c.CreateModel();
                string q = m.QueueDeclare("dotnet-client.recovery.consumer_work_pool1",
                                          false, false, false, null).QueueName;
                var cons = new EventingBasicConsumer(m);
                m.BasicConsume(q, true, cons);
                AssertConsumerCount(m, q, 1);

                CloseAndWaitForRecovery(c);

                Assert.IsTrue(m.IsOpen);
                var latch = new ManualResetEventSlim(false);
                cons.Received += (s, args) => latch.Set();

                m.BasicPublish("", q, null, _encoding.GetBytes("msg"));
                Wait(latch);

                m.QueueDelete(q);
            }
        }
Exemple #4
0
        /// <summary>
        /// Create a connection using a list of hostnames. The first reachable
        /// hostname will be used initially. Subsequent hostname picks are determined
        /// by the <see cref="IHostnameSelector" /> configured.
        /// </summary>
        /// <param name="hostnames">
        /// List of hostnames to use for the initial
        /// connection and recovery.
        /// </param>
        /// <returns>Open connection</returns>
        /// <exception cref="BrokerUnreachableException">
        /// When no hostname was reachable.
        /// </exception>
        public IConnection CreateConnection(IList <string> hostnames)
        {
            IConnection conn;

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    var autorecoveringConnection = new AutorecoveringConnection(this);
                    autorecoveringConnection.Init(hostnames);
                    conn = autorecoveringConnection;
                }
                else
                {
                    IProtocol protocol = Protocols.DefaultProtocol;
                    conn = protocol.CreateConnection(this, false, CreateFrameHandler());
                }
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }
            return(conn);
        }
        public void TestBasicConnectionRecoveryStopsAfterManualClose()
        {
            Assert.True(_conn.IsOpen);
            AutorecoveringConnection c = CreateAutorecoveringConnection();
            var latch = new AutoResetEvent(false);

            c.ConnectionRecoveryError += (o, args) => latch.Set();

            try
            {
                StopRabbitMQ();
                latch.WaitOne(30000); // we got the failed reconnection event.
                bool triedRecoveryAfterClose = false;
                c.Close();
                Thread.Sleep(5000);
                c.ConnectionRecoveryError += (o, args) => triedRecoveryAfterClose = true;
                Thread.Sleep(10000);
                Assert.False(triedRecoveryAfterClose);
            }
            finally
            {
                StartRabbitMQ();
            }
        }
        /// <summary>
        /// Create a connection using an IEndpointResolver.
        /// </summary>
        /// <param name="endpointResolver">
        /// The endpointResolver that returns the endpoints to use for the connection attempt.
        /// </param>
        /// <param name="clientProvidedName">
        /// Application-specific connection name, will be displayed in the management UI
        /// if RabbitMQ server supports it. This value doesn't have to be unique and cannot
        /// be used as a connection identifier, e.g. in HTTP API requests.
        /// This value is supposed to be human-readable.
        /// </param>
        /// <returns>Open connection</returns>
        /// <exception cref="BrokerUnreachableException">
        /// When no hostname was reachable.
        /// </exception>
        public IConnection CreateConnection(IEndpointResolver endpointResolver, string clientProvidedName)
        {
            IConnection conn;

            try
            {
                if (AutomaticRecoveryEnabled)
                {
                    var autorecoveringConnection = new AutorecoveringConnection(this, clientProvidedName);
                    autorecoveringConnection.Init(endpointResolver);
                    conn = autorecoveringConnection;
                }
                else
                {
                    conn = ((ProtocolBase)Protocols.AMQP_0_9_1).CreateConnection(this, endpointResolver.SelectOne(CreateFrameHandler), clientProvidedName);
                }
            }
            catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }

            return(conn);
        }
        ///<summary>Create a connection to the specified endpoint.</summary>
        public virtual IConnection CreateConnection()
        {
            IConnection conn = null;

            try
            {
                if (this.AutomaticRecoveryEnabled)
                {
                    AutorecoveringConnection ac = new AutorecoveringConnection(this);
                    ac.init();
                    conn = ac;
                }
                else
                {
                    IProtocol p = Protocols.DefaultProtocol;
                    conn = p.CreateConnection(this, false, this.CreateFrameHandler());
                }
            } catch (Exception e)
            {
                throw new BrokerUnreachableException(e);
            }

            return(conn);
        }
 internal void AssertRecordedQueues(AutorecoveringConnection c, int n)
 {
     Assert.AreEqual(n, c.RecordedQueuesCount);
 }
 public DisposableConnection(AutorecoveringConnection c)
 {
     Connection = c;
 }
 internal void WaitForRecovery(AutorecoveringConnection conn)
 {
     Wait(PrepareForRecovery(conn));
 }
Exemple #11
0
 public AutorecoveringModel(AutorecoveringConnection conn, RecoveryAwareModel _delegate)
 {
     m_connection = conn;
     m_delegate   = _delegate;
 }
 internal void AssertRecordedExchanges(AutorecoveringConnection c, int n)
 {
     Assert.Equal(n, c.RecordedExchangesCount);
 }
Exemple #13
0
 public void OnReconnected(AutorecoveringConnection conn)
 {
     Console.WriteLine("OnConnectionSuccess : 重新连接");
 }
Exemple #14
0
 public void OnError(AutorecoveringConnection conn, Exception e)
 {
     Console.WriteLine("OnError : " + e.Message);
 }
 public void OnReconnected(AutorecoveringConnection conn)
 {
 }
 public void OnError(AutorecoveringConnection conn, Exception e)
 {
 }
 protected void AssertRecordedExchanges(AutorecoveringConnection c, int n)
 {
     Assert.AreEqual(n, c.RecordedExchanges.Count);
 }
Exemple #18
0
 protected void WaitForRecovery(AutorecoveringConnection conn)
 {
     Wait(PrepareForRecovery(conn));
 }