Beispiel #1
0
 public void Dispose()
 {
     isdisposeing = true;
     try
     {
         try
         {
             //通知关闭监听通道
             if (redisDb != null)
             {
                 var manager = new BSF.Redis.RedisManager();
                 using (var db = manager.CreateClient(RedisServerIp.Split(':')[0], Convert.ToInt32(RedisServerIp.Split(':')[1]), ""))
                 {
                     db.GetClient().PublishMessage(channelName, "RedisNetCommandListener-Close");
                     db.GetClient().Quit();
                 }
             }
         }
         catch { }
         //关闭当前连接
         CloseRedisClient();
     }
     catch (Exception exp)
     { }
 }
Beispiel #2
0
        private void RedisSubscribe(Action <string, string> action, Action <RedisErrorInfo> errorAction, string channelname)
        {
            var manager = new BSF.Redis.RedisManager();

            redisDb = manager.CreateClient(RedisServerIp.Split(':')[0], Convert.ToInt32(RedisServerIp.Split(':')[1]), "");
            using (var subscription = redisDb.GetClient().CreateSubscription())
            {
                subscription.OnSubscribe = channel =>
                {
                    //订阅事件
                };
                subscription.OnUnSubscribe = channel =>
                {
                    //退订事件
                };
                subscription.OnMessage = (channel, msg) =>
                {
                    try
                    {
                        if (msg == "RedisNetCommandListener-Close" || isdisposeing == true)//关闭通道
                        {
                            try { subscription.UnSubscribeFromChannels(channel); }
                            catch { }
                        }
                        else
                        {
                            if (action != null)
                            {
                                action.Invoke(channel, msg);
                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        errorAction.Invoke(new RedisErrorInfo()
                        {
                            ChannelName = channelname, Exception = exp, Message = string.Format("MQredis订阅通信消息,通道:{1},处理消息{0},订阅名:{2}出错", msg.NullToEmpty(), channelname, Name)
                        });
                    }
                };
                subscription.SubscribeToChannels(channelname);
            }
        }