Пример #1
0
        public bool SendMsgToMachines(KeyValueMessage message, CloudToMachineType machineType)
        {
            if (!isConnectedToServer)
            {
                throw new InvalidOperationException("Please call InitConfigAndConnect medthod first!");
            }
            try
            {
                var _noQueueChannel = _connectToRabbitMqService.GetNoQueuedModel();
                _noQueueChannel.ExchangeDeclare(RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_NOQUEUE, "fanout");
                var msgStr = JsonConvert.SerializeObject(message);
                var body   = MessagePackSerializer.Serialize(msgStr);

                _noQueueChannel.BasicPublish(exchange: RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_NOQUEUE,
                                             routingKey: "",
                                             basicProperties: null,
                                             body: body);

                MessageLogUtil.Log($"Send no queue message to cloud: {JsonConvert.SerializeObject(message)}");
                return(true);
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("SendMsgToMachine", e);
                ErrorAction?.Invoke(e, "SendMsgToMachine");
                return(false);
            }
        }
Пример #2
0
        public bool SendQueuedMsgToCloud(KeyValueMessage message)
        {
            if (!isConnectedToServer)
            {
                throw new InvalidOperationException("Please call InitConfigAndConnect medthod first!");
            }
            try
            {
                var _queuedChannel = _connectToRabbitMqService.GetQueuedModel();
                _queuedChannel.QueueDeclare(queue: RabbitMqConstants.CLIENT_TO_SERVER_QUEUE,
                                            durable: true,
                                            exclusive: false,
                                            autoDelete: false,
                                            arguments: null);

                var body = MessagePackSerializer.Serialize(message);

                var properties = _queuedChannel.CreateBasicProperties();
                properties.Persistent = true;

                _queuedChannel.BasicPublish(exchange: "",
                                            routingKey: RabbitMqConstants.CLIENT_TO_SERVER_QUEUE,
                                            basicProperties: properties,
                                            body: body);
                MessageLogUtil.Log($"Send message to cloud: {JsonConvert.SerializeObject(message)}");
                return(true);
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("SendQueuedMsgToCloud", e);
                ErrorAction?.Invoke(e, "SendQueuedMsgToCloud");
                return(false);
            }
        }
        private void CheckConnectTimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (_connection == null || !_connection.IsOpen)
            {
                MessageLogUtil.Log("RabbitMQ connection closed, try to reopen...");
                if (_queuedChannel != null && _queuedChannel.IsOpen)
                {
                    _queuedChannel.Close();
                }

                Connect(_hostName, _userName, _pwd);
            }
        }
Пример #4
0
        public bool SendQueuedMsgToMachines(KeyValueMessage message, CloudToMachineType machineType)
        {
            if (!isConnectedToServer)
            {
                throw new InvalidOperationException("Please call InitConfigAndConnect medthod first!");
            }
            try
            {
                var _queuedChannel = _connectToRabbitMqService.GetQueuedModel();
                _queuedChannel.ExchangeDeclare(exchange: RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_QUEUED, type: "direct");
                var msgStr = JsonConvert.SerializeObject(message);
                var body   = MessagePackSerializer.Serialize(msgStr);

                var properties = _queuedChannel.CreateBasicProperties();
                properties.Persistent = true;

                switch (machineType)
                {
                case CloudToMachineType.AllMachines:
                    _queuedChannel.BasicPublish(exchange: RabbitMqConstants.EXCHANGE_CLOUD_TO_MACHINE_QUEUED,
                                                routingKey: RabbitMqConstants.ROUTING_KEY_MACHINES,
                                                basicProperties: properties,
                                                body: body);
                    break;

                case CloudToMachineType.CurrentTenant:
                    break;

                case CloudToMachineType.ToMachineId:
                    _queuedChannel.BasicPublish(exchange: "",
                                                routingKey: message.MachineId.ToString(),
                                                basicProperties: properties,
                                                body: body);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(machineType), machineType, null);
                }

                MessageLogUtil.Log($"Send message type {machineType.ToString()} to machines: {JsonConvert.SerializeObject(message)}");
                return(true);
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("SendQueuedMsgToMachines", e);
                ErrorAction?.Invoke(e, "SendQueuedMsgToMachines");
                return(false);
            }
        }
        public void Connect(string hostName, string userName, string pwd, string clientName = "KonbiCloud")
        {
            try
            {
                _hostName = hostName;
                _userName = userName;
                _pwd      = pwd;

                MessageLogUtil.Log($"Connecting to RabbitMQ {hostName}");
                var factory = new ConnectionFactory()
                {
                    HostName = hostName, UserName = userName, Password = pwd
                };

                _connection     = factory.CreateConnection(clientName);
                _queuedChannel  = _connection.CreateModel();
                _noQueueChannel = _connection.CreateModel();
            }
            catch (Exception e)
            {
                MessageLogUtil.Error("InitRabbitMqConnection", e);
                ErrorAction?.Invoke(e);
            }
        }