/// <summary> /// 订阅类实例化时指定的频道,阻塞线程等待消息 /// </summary> public void SubscribeToChannels() { try { redisSubscription.SubscribeToChannels(channelName); } catch { SubscribeToChannels(); } }
public string WaitForNotifyOnAny(params string[] channelNames) { string result = null; var subscription = new RedisSubscription(readOnlyClient); subscription.OnMessage = (channel, msg) => { result = msg; subscription.UnSubscribeFromAllChannels(); }; subscription.SubscribeToChannels(channelNames); //blocks return(result); }
private void OnSubscribe() { RedisClient newclient = new RedisClient(MainWindow.myRedisIP, MainWindow.myRedisPort, null, MainWindow.database); newclient.Subscribe(RedisKeyName.subscribeKey); RedisSubscription sub = new RedisSubscription(newclient); sub.OnUnSubscribe += (obj) => { Console.WriteLine(); }; sub.OnMessage = (sender, argcs) => { string thisID = argcs; try { Task thisTask = new Task(thisID); this.Dispatcher.BeginInvoke(new Action(() => { finishedTaskQueueListView.Items.Insert(0, thisTask); })); if (thisTask.Command == "svn_switch") { if (currentBranchOfServer.ContainsKey(thisTask.ServerID)) { RedisClient redis = new RedisClient(MainWindow.myRedisIP, MainWindow.myRedisPort, null, MainWindow.database); if (preBranchOfServer.ContainsKey(thisTask.ServerID)) { preBranchOfServer[thisTask.ServerID] = currentBranchOfServer[thisTask.ServerID]; } else { preBranchOfServer.Add(thisTask.ServerID, currentBranchOfServer[thisTask.ServerID]); } currentBranchOfServer[thisTask.ServerID] = int.Parse(thisTask.TagID) + 1; redis.Set <int>(RedisKeyName.currentBranchOfServerPrefix + thisTask.ServerID, int.Parse(thisTask.TagID) + 1); redis.Quit(); } else { currentBranchOfServer.Add(thisTask.ServerID, int.Parse(thisTask.TagID) + 1); } this.Dispatcher.BeginInvoke(new Action(() => { UpdateCurrentBranchInServer(); })); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source); return; } }; sub.SubscribeToChannels(RedisKeyName.subscribeKey); newclient.Quit(); }
public long Subscribe() { RedisClient client = CreateClient(); RedisSubscription sub = new RedisSubscription(client); sub.OnMessage += (channel, msg) => { Debug.WriteLine("<<:" + msg); }; Task.Factory.StartNew(() => { sub.SubscribeToChannels("mq:test"); }); return(0); }
private void WaitForMessages() { //the redis subscription is not thread safe, and a subscription cannot be cancelled outside of the //(now blocked) thread that called SubscribeToChannels() - so cleaning up this class on //an external thread is going to cause this to throw try { _redisSubscriber.SubscribeToChannels(_config.RedisMessageChannel); } catch (RedisException ex) { if (!_disposed) { throw ex; } } }
/// <summary> /// 订阅 /// </summary> static void Subscribe() { using (RedisNativeClient redisNativeClient = new RedisNativeClient("localhost", 6379)) using (RedisSubscription redisSubscription = new RedisSubscription(redisNativeClient)) { redisSubscription.OnMessage += (e, m) => { Console.WriteLine("Subscribe OnMessage:{0} {1}", e, m); }; redisSubscription.OnSubscribe += (m) => { Console.WriteLine("Subscribe OnSubscribe:{0}", m); }; redisSubscription.OnUnSubscribe += (m) => { Console.WriteLine(" Subscribe OnUnSubscribe:{0}", m); }; redisSubscription.SubscribeToChannels("channel-lcq"); } }
public void SubscribeToChannels(params string[] channels) { _sub.SubscribeToChannels(channels); //_sub.SubscribeToChannels("__keyevent@0__:expired"); }