示例#1
0
文件: MQ.cs 项目: ewin66/SAE
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="message"></param>
        /// <returns></returns>
        public IMQ Publish <TMessage>(TMessage message)
        {
            this._log.Debug("Start Publish");
            //获得exchang名称
            var name = NameUtils.Get(message);

            this._log.Debug($"Exchange Name:{name}");

            if (!_channels.ContainsKey(name))
            {
                lock (_lock)
                {
                    if (!_channels.ContainsKey(name))
                    {
                        this.AddChannel <TMessage>(name);
                    }
                }
            }
            //获得channel
            var channel = _channels[name];

            var propertie = channel.CreateBasicProperties();

            //设置消息为持久化
            propertie.DeliveryMode = 2;

            var json = this._jsonConvertor.Serialize(message);

            //序列主体
            var body = this.GetEncoding().GetBytes(json);

            try
            {
                this._log.Debug($"Publish Message:{json}");
                channel.BasicPublish(name, "", propertie, body);
            }
            catch (Exception ex)
            {
                this._log.Error($"body:{json},error:{this._jsonConvertor.Serialize(ex)}");
            }
            //发布

            this._log.Debug($"Publish Succeed:{json}");
            return(this);
        }
示例#2
0
文件: MQ.cs 项目: ewin66/SAE
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="action"></param>
        /// <returns></returns>
        public IMQ Subscibe <TMessage>(Action <TMessage> action)
        {
            if (this.IsExist <TMessage>())
            {
                //已被订阅过只需要将订阅附加即可不需要再次开启通道
                this.Add(action);
                return(this);
            }

            this.Add(action);

            var name = NameUtils.Get <TMessage>();

            this._log.Info($"Exchange Name:{name}");
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name), "监听的队列不存在");
            }

            var queryName = $"{this._config.Query}_{name}";

            this._log.Info($"Query Name:{queryName}");
            //创建信道
            var channel = _conn.CreateModel();

            channel.CallbackException += _conn_CallbackException;
            this._log.Debug($"Declare Queue:{queryName}");
            //定义队列
            channel.QueueDeclare(queryName, true, false, false, null);
            this._log.Debug($"Declare Exchange:{name}");
            //定义exchange
            channel.ExchangeDeclare(name, MQType, true, false);
            this._log.Debug($"Bind Queue:exchange={name},queue={queryName}");
            //绑定队列
            channel.QueueBind(queryName, name, "", null);

            this._log.Debug("Create Consumer");
            //创建消费者
            var consumer = new EventingBasicConsumer(channel);

            this._log.Debug("Create Received");

            //消费
            consumer.Received += (mo, arg) =>
            {
                var body = this.GetEncoding().GetString(arg.Body);

                var message = this._jsonConvertor.Deserialize <TMessage>(body);

                this._log.Debug($"Deserialize");

                try
                {
                    this._log.Debug($"Start Received:{message}");

                    this.Get <TMessage>().Invoke(message);

                    if (this._config.Ack)
                    {
                        this._log.Info("Send Ack");

                        channel.BasicAck(arg.DeliveryTag, false);
                    }
                }
                catch (SAEException ex)
                {
                    channel.BasicReject(arg.DeliveryTag, true);
                    //消息消费失败
                    this._log.Error($"The exception caused by the consumption Message", ex);
                }
                catch (Exception ex)
                {
                    //消息消费失败
                    this._log.Error($"The exception caused by the consumption Message", ex);
                }
            };

            if (this._config.Ack)
            {
                this._log.Info("Enable Ack");
                channel.BasicQos(0, this._config.Number, false);
            }


            //基础消费者
            channel.BasicConsume(queryName, !this._config.Ack, consumer);

            return(this);
        }