Esempio n. 1
0
        } // AmqpClient

        #region ConnectionHandlerProgram

        private Exception MakeNewConnection(ref RmqCl.IConnection connection, ref RmqCl.IModel channel, bool reconnecting)
        {
            // This method attempts to make a new connection. Returns true if success, otherwise false.

            var connRequest = ConnectionRequestObj;

            var factory = new RmqCl.ConnectionFactory()
            {
                HostName = connRequest.Host,
                UserName = connRequest.Username,
                Password = connRequest.Password
            };

            // Secure connection?
            if (connRequest.Secure)
            {
                factory.Ssl.Enabled    = true;
                factory.Ssl.ServerName = connRequest.Host;
                factory.Ssl.Version    = System.Security.Authentication.SslProtocols.Tls12;
            }

            try
            {
                // Create connection and channel
                connection = factory.CreateConnection();
                channel    = connection.CreateModel();

                // Declare the exchange
                channel.ExchangeDeclare(exchange: connRequest.Exchange, type: "topic", autoDelete: false, durable: true, arguments: null);

                // Create a queue to receive messages
                var queueName = channel.QueueDeclare(queue: "",        // Use a generated queue name
                                                     durable: false,   // The queue does not survive a broker restart
                                                     exclusive: true,  // The queue is only for this application, and it will be deleted on app exit
                                                     autoDelete: true, // The queue is deleted when no one is bound to it
                                                     arguments: null
                                                     ).QueueName;

                // Bind the queue to the topic pattern
                channel.QueueBind(queue: queueName, exchange: connRequest.Exchange, routingKey: connRequest.TopicPattern, arguments: null);

                // Set up a consumer for messages
                m_messageConsumer           = new RmqCl.Events.EventingBasicConsumer(channel);
                m_messageConsumer.Received += MessageReceived;
                channel.BasicConsume(queue: queueName, noAck: true, consumerTag: "", noLocal: false, exclusive: false, arguments: new Dictionary <string, object> {
                }, consumer: m_messageConsumer);

                // Sign up for the shutdown event
                channel.ModelShutdown += ModelShutdown; // This event will fire if the connection is lost

                return(null);
            }
            catch (Exception e)
            {
                // Clean the connection
                DestroyConnection(ref connection, ref channel);

                return(e);
            }
        } // MakeNewConnection
        private void InitializeInputExchange(IModel channel)
        {
            channel.ExchangeDeclare(
                exchange: $"{_serviceName}.Input.E.Direct.{Env}",
                type: "direct");

            channel.ExchangeBind(
                destination: $"{_serviceName}.Input.E.Direct.{Env}",
                source: $"RabbitMqPocMessageHub.E.Fanout.{Env}",
                routingKey: "");
        }
Esempio n. 3
0
        private void DeclareExchanges(RC.IModel channel, params IExchange[] exchanges)
        {
            foreach (var exchange in exchanges)
            {
                _logger?.LogDebug("Declaring exchange '{exchange}'", exchange.ExchangeName);

                if (!IsDeclaringDefaultExchange(exchange))
                {
                    try
                    {
                        if (exchange.IsDelayed)
                        {
                            var arguments = exchange.Arguments;
                            if (arguments == null)
                            {
                                arguments = new Dictionary <string, object>();
                            }
                            else
                            {
                                arguments = new Dictionary <string, object>(arguments);
                            }

                            arguments["x-delayed-type"] = exchange.Type;

                            // TODO: exchange.IsInternal .. appears .NET client doesn't expose
                            channel.ExchangeDeclare(exchange.ExchangeName, DELAYED_MESSAGE_EXCHANGE, exchange.IsDurable, exchange.IsAutoDelete, arguments);
                        }
                        else
                        {
                            // TODO: exchange.IsInternal .. appears .NET client doesn't expose
                            channel.ExchangeDeclare(exchange.ExchangeName, exchange.Type, exchange.IsDurable, exchange.IsAutoDelete, exchange.Arguments);
                        }
                    }
                    catch (Exception e)
                    {
                        LogOrRethrowDeclarationException(exchange, "exchange", e);
                    }
                }
            }
        }
Esempio n. 4
0
        public RabbitMQWrapper(RabbitMQ.Client.IConnection connection)
        {
            _rmqConnection = connection;
            _rmqModel      = _rmqConnection.CreateModel();

            _rmqModel.ExchangeDeclare(_exchangeName, ExchangeType.Topic);

            _serializerSettings = new JsonSerializerSettings
            {
                ContractResolver  = new CamelCasePropertyNamesContractResolver(),
                NullValueHandling = NullValueHandling.Ignore
            };
        }
Esempio n. 5
0
        public void ReviceMessage(string queueName, string someone, Action <string> eventHandel)
        {
            EventingBasicConsumer consumer = new EventingBasicConsumer(_chanel);

            _chanel.ExchangeDeclare(exchangeName, exchangeType, false, false, null);
            //_chanel.ExchangeDeclare(exchangeName, exchangeType);
            _chanel.QueueDeclare(queueName, false, false, false, null);//持久化  排他性 自动删除
            _chanel.QueueBind(queueName, exchangeName, routingKey, null);
            //_chanel.QueueBind(queueName, exchangeName, routingKey + someone);//交换机与
            consumer.Received += (ch, ea) =>
            {
                var message = Encoding.UTF8.GetString(ea.Body);
                eventHandel?.Invoke(message);
                _chanel.BasicAck(ea.DeliveryTag, false);//确认该消息已被消费
            };
            _chanel.BasicConsume(queueName, false, consumer);
            //_chanel.BasicConsume(queueName, false, consumer); //启动消费者

            // Console.WriteLine("客户端2已启动");
        }
 private void InitializeRabbitMqPocMessageHub(IModel channel)
 {
     channel.ExchangeDeclare(
         exchange: $"RabbitMqPocMessageHub.E.Fanout.{Env}",
         type: "fanout");
 }
 private void InitializeDeadLetterExchange(IModel channel)
 {
     channel.ExchangeDeclare(
         exchange: $"{_serviceName}.Dlx.E.Fanout.{Env}",
         type: "fanout");
 }