Esempio n. 1
0
        protected virtual void CreatePublishChannel()
        {
            if (_dedicatedPublishingChannel == null || !_dedicatedPublishingChannel.IsOpen)
            {
                _dedicatedPublishingChannel = _connection.CreateChannel();
                _createdChannels.Add(_dedicatedPublishingChannel);

                _dedicatedPublishingChannel.BasicAcks   += OnBrokerReceivedMessage;
                _dedicatedPublishingChannel.BasicNacks  += OnBrokerRejectedMessage;
                _dedicatedPublishingChannel.BasicReturn += OnMessageIsUnrouted;
                _watcher.InfoFormat("Dedicated publishing channel established");
            }
        }
        public static RabbitTunnelWithPriorityQueuesSupport CreateTunnel(IModel channel, out IDurableConnection durableConnection, bool isChannelOpen = true)
        {
            if (channel != null)
            {
                channel.IsOpen.Returns(isChannelOpen);
            }

            var routeFinder = Substitute.For <IRouteFinder>();

            routeFinder.FindQueueName <Customer>(null).ReturnsForAnyArgs("Queue");
            durableConnection = Substitute.For <IDurableConnection>();
            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            var conn = durableConnection;

            durableConnection.When(x => x.Connect()).Do(callInfo => // Because this is a mock objectd
            {
                //Rase connected event
                conn.Connected += Raise.Event <Action>();
                conn.IsConnected.Returns(true);
            });
            durableConnection.CreateChannel().Returns(channel);

            var tunnel = new RabbitTunnelWithPriorityQueuesSupport(routeFinder, durableConnection);

            tunnel.OnOpened += () => { };
            tunnel.OnClosed += () => { };
            return(tunnel);
        }
Esempio n. 3
0
        public static RabbitTunnel CreateTunnel(IModel channel, out IDurableConnection durableConnection, bool isChannelOpen = true)
        {
            if (channel != null)
            {
                channel.IsOpen.Returns(isChannelOpen);
            }

            var routeFinder = Substitute.For<IRouteFinder>();
            routeFinder.FindQueueName<Customer>(null).ReturnsForAnyArgs("Queue");
            durableConnection = Substitute.For<IDurableConnection>();
            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            var conn = durableConnection;
            durableConnection.When(x => x.Connect()).Do(callInfo => // Because this is a mock objectd
            {
                //Rase connected event
                conn.Connected += Raise.Event<Action>();
                conn.IsConnected.Returns(true);
            });
            durableConnection.CreateChannel().Returns(channel);

            var tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);
            tunnel.OnOpened += () => { };
            tunnel.OnClosed += () => { };
            return tunnel;
        }
Esempio n. 4
0
        protected virtual void CreatePublishChannel()
        {
            if (_dedicatedPublishingChannel == null || !_dedicatedPublishingChannel.IsOpen)
            {
                _watcher.InfoFormat("Creating dedicated publishing channel");
                _dedicatedPublishingChannel = _connection.CreateChannel();

                // If still failed, it's time to throw exception
                if (_dedicatedPublishingChannel == null || !_dedicatedPublishingChannel.IsOpen)
                {
                    throw new Exception("No channel to rabbit server established.");
                }

                _createdChannels.Add(_dedicatedPublishingChannel);

                _dedicatedPublishingChannel.BasicAcks     += OnBrokerReceivedMessage;
                _dedicatedPublishingChannel.BasicNacks    += OnBrokerRejectedMessage;
                _dedicatedPublishingChannel.BasicReturn   += OnMessageIsUnrouted;
                _dedicatedPublishingChannel.ModelShutdown += (channel, reason) => _watcher.WarnFormat("Dedicated publishing channel is shutdown: {0}", reason.ReplyText);

                _watcher.InfoFormat("Dedicated publishing channel established");
            }
        }
Esempio n. 5
0
        public virtual void HandleError(BasicDeliverEventArgs deliverEventArgs, Exception exception)
        {
            try
            {
                using (var model = _durableConnection.CreateChannel())
                {
                    lock (_channelGate)
                    {
                        InitializeErrorExchangeAndQueue(model);
                    }

                    var messageBody = CreateErrorMessage(deliverEventArgs, exception);
                    var properties  = model.CreateBasicProperties();
                    properties.SetPersistent(true);
                    model.BasicPublish(_errorExchange, string.Empty, properties, messageBody);
                }
            }
            catch (ConnectFailureException)
            {
                // thrown if the broker is unreachable during initial creation.
                _watcher.ErrorFormat("ConsumerErrorHandler: cannot connect to Broker.\n" + CreateConnectionCheckMessage(_durableConnection));
            }
            catch (BrokerUnreachableException)
            {
                // thrown if the broker is unreachable during initial creation.
                _watcher.ErrorFormat("ConsumerErrorHandler: cannot connect to Broker.\n" + CreateConnectionCheckMessage(_durableConnection));
            }
            catch (OperationInterruptedException interruptedException)
            {
                // thrown if the broker connection is broken during declare or publish.
                _watcher.ErrorFormat(
                    "ConsumerErrorHandler: Broker connection was closed while attempting to publish Error message.\n" +
                    $"Message was: '{interruptedException.Message}'\n" +
                    CreateConnectionCheckMessage(_durableConnection));
            }
            catch (Exception unexpecctedException)
            {
                _watcher.ErrorFormat("ConsumerErrorHandler: Failed to publish error message\nException is:\n" + unexpecctedException);
            }
        }