private void CloseRedisClient() { try { if (redisDb != null) { if (redisDb.GetClient() != null) { redisDb.GetClient().Quit(); } //redisDb.Dispose(); redisDb = null; } } catch (Exception exp) { } }
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); } }
private void RedisSubscribe(Action<string, string> action, string mqpath, string channelname) { var manager = new XXF.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) { ErrorLogHelper.WriteLine(-1, mqpath, "RedisSubscribe", string.Format("MQredis订阅通信消息,通道:{1},处理消息{0},订阅名:{2}出错", msg.NullToEmpty(), channelname, Name), exp); } }; subscription.SubscribeToChannels(channelname); } }
public SimpleRedisMQ(RedisDb redisdb) { client = redisdb.GetClient(); }