Exemple #1
0
        private void OnConnectionShutdown(object sender, ShutdownEventArgs shutdownEventArgs)
        {
            try
            {
                // Check if shutdown code is known in our managed shut down code .
                // If yes check if they no more connected connector to really shut down the connection
                // Reminder all connectors in the same process share the connection on Rabbit mq
                // Connection object is thread safe.
                if (CloseCodeReasonDictionary.ContainsKey(shutdownEventArgs.ReplyCode) &&
                    RegisteredConnectors.Count == 0)
                {
                    return;
                }

                _logger.LogWarning($"Disconnect for bad reason id: {shutdownEventArgs.ReplyCode} - Text: {shutdownEventArgs.ReplyText} - Initiator: {shutdownEventArgs.Initiator}");
                _connection = null;
                _factory    = null;
                RegisteredConnectors.Clear();

                // If the shutdown reason is unknown (ei: send message connector try
                // to send a message on a missing exchange) we recreate the shared connection
                CreateConnection(_configuration);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
            }
        }
Exemple #2
0
        public IModel CreateChannel(IMessageConnector connector)
        {
            IModel channel = _connection.CreateModel();

            channel.ModelShutdown += (sender, args) =>
            {
                _logger.LogInformation($"Channel shutdown for connector Id: {connector.Id} - type {connector.GetType().Name} - reason id: {args.ReplyCode} - Text: {args.ReplyText} - Initiator: {args.Initiator}");
                RegisteredConnectors.TryRemove(connector.Id, out _);
            };

            RegisterConnector(connector, channel);

            return(channel);
        }
Exemple #3
0
        internal IModel GetAssociatedChannel(IMessageConnector connector)
        {
            IModel channel = null;

            if (IsConnect && RegisteredConnectors.TryGetValue(connector.Id, out channel))
            {
                if (channel == null || channel.IsOpen)
                {
                    RegisteredConnectors.TryRemove(connector.Id, out _);
                }
            }

            return(channel);
        }
Exemple #4
0
        private void CreateConnection(BrokerConfiguration configuration)
        {
            try
            {
                if (!IsConnect)
                {
                    if (_factory == null)
                    {
                        _factory = new ConnectionFactory
                        {
                            HostName    = configuration.HostName,
                            Port        = configuration.Port,
                            UserName    = configuration.UserName,
                            Password    = configuration.Password,
                            VirtualHost = configuration.Vhost,
                        };

#if DEBUG
                        // Force to use Ipv4 instead of trying ipv6 and try catch exception in his code.
                        _factory.SocketFactory = family => ConnectionFactory.DefaultSocketFactory(AddressFamily.InterNetwork);
#endif
                    }

                    RegisteredConnectors.Clear();

                    _connection = _factory.CreateConnection($"{Environment.MachineName}:{Environment.UserName}");
                    _logger.LogInformation($"Connected on broker {_factory.HostName} - vHost: {_factory.VirtualHost}");

                    using (IModel channel = _connection.CreateModel())
                    {
                        CreateDeadLetter(channel);
                    }

                    _connection.ConnectionShutdown += OnConnectionShutdown;
                    _configuration = configuration;
                }
                if (IsConnect)
                {
                    OnConnect?.Invoke(this, EventArgs.Empty);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Connection failed.");
            }
        }