コード例 #1
0
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exchange"></param>
        /// <param name="queue"></param>
        /// <param name="routingKey"></param>
        /// <param name="handler">消费处理</param>
        /// <param name="type">交换器类型</param>
        public void Pull <T>(string exchange, string queue, string routingKey, Action <T> handler, string type = ExchangeType.Direct) where T : class
        {
            var lChannel = GetModel(exchange, queue, routingKey, type);

            var lResult = lChannel.BasicGet(queue, false);

            if (lResult == null)
            {
                return;
            }

            var lMsg = JsonHelper.BytesToObject <T>(lResult.Body);

            try
            {
                handler(lMsg);
            }
            catch (Exception lEx)
            {
                mLogService.Error(this, $"拉取消息失败,exchange:{exchange},queue:{queue}", lEx);
            }
            finally
            {
                lChannel.BasicAck(lResult.DeliveryTag, false);
            }
        }
コード例 #2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queue">队列名称</param>
        /// <param name="isProperties">是否持久化</param>
        /// <param name="handler">消费处理</param>
        public void Subscribe <T>(string queue, Action <T> handler, bool isProperties = false) where T : class
        {
            //队列声明
            var lChannel = GetModel(queue, isProperties);

            var lConsumer = new EventingBasicConsumer(lChannel);

            lConsumer.Received += (model, ea) =>
            {
                var lBody = ea.Body;
                var lMsg  = JsonHelper.BytesToObject <T>(lBody);
                try
                {
                    handler(lMsg);
                }
                catch (Exception lEx)
                {
                    mLogService.Error(this, $"接收消息成功,处理事件失败,queue:{queue}", lEx);
                }
                finally
                {
                    lChannel.BasicAck(ea.DeliveryTag, false);
                }
            };
            lChannel.BasicConsume(queue, false, lConsumer);
        }
コード例 #3
0
        public T Get <T>(string key)
        {
            var lRedis  = Instance.GetDatabase();
            var lHasKey = lRedis.KeyExists(key);

            if (!lHasKey)
            {
                return(default(T));
            }
            var lVaule = lRedis.StringGet(key);

            return(JsonHelper.BytesToObject <T>(lVaule));
        }
コード例 #4
0
        public async Task <T> GetAsync <T>(string key)
        {
            var lRedis  = Instance.GetDatabase();
            var lHasKey = await lRedis.KeyExistsAsync(key);

            if (!lHasKey)
            {
                return(default(T));
            }
            var lVaule = await lRedis.StringGetAsync(key);

            return(JsonHelper.BytesToObject <T>(lVaule));
        }
コード例 #5
0
        public T Subscribe <T>()
        {
            using (var lConnection = Factory.CreateConnection())
            {
                using (var lChannel = lConnection.CreateModel())
                {
                    var lResult = lChannel.BasicGet(QueueName, false);
                    if (lResult == null)
                    {
                        return(default(T));
                    }

                    lChannel.BasicAck(lResult.DeliveryTag, false);
                    var lMessage = JsonHelper.BytesToObject <T>(lResult.Body);
                    return(lMessage);
                }
            }
        }