async Task <bool> IMessageReceiver.Loop(CancellationToken t) { token = t; while (!t.IsCancellationRequested) { try { client = new CSRedisClient(RedisOption.Instance.ConnectionString); if (client == null) { continue; } subscribeObject = client.Subscribe((Service.ServiceName, arg => OnMessagePush())); break; } catch (Exception ex) { logger.Error(() => $"启动订阅失败.{ex.Message}"); await Task.Delay(3000); } } OnMessagePush(); loopTask = new TaskCompletionSource <bool>(); await loopTask.Task; return(true); }
/// <summary> /// 初始化一个<see cref="RedisSubscription"/>类型的实例 /// </summary> /// <param name="storage">Redis存储</param> /// <param name="redisClient">Redis客户端</param> public RedisSubscription(RedisStorage storage, CSRedisClient redisClient) { _storage = storage ?? throw new ArgumentNullException(nameof(storage)); _redisClient = redisClient ?? throw new ArgumentNullException(nameof(redisClient)); Channel = _storage.GetRedisKey("JobFetchChannel"); _subscribeObject = _redisClient.Subscribe((Channel, r => _mre.Set())); }
/// <summary> /// 订阅 /// </summary> /// <param name="asynchronousClient"></param> private static void Subscribe(AsynchronousClient asynchronousClient) { SubscribeObject subscribeObject = new SubscribeObject(); subscribeObject.topic = "test"; Console.WriteLine($"我订阅了主题[{subscribeObject.topic}]"); asynchronousClient.Send(subscribeObject, MsgOperation.订阅消息Socket方式); }
async Task <bool> IMessageReceiver.Loop(CancellationToken t) { token = t; while (!token.IsCancellationRequested) { try { client = new CSRedisClient(RedisOption.Instance.ConnectionString); subscribeObject = client.Subscribe((Service.ServiceName, OnMessagePush)); break; } catch (Exception ex) { Logger.Error(() => $"CSRedisEventReceiver Loop.\r\n{ex}"); await Task.Delay(3000); } } loopTask = new TaskCompletionSource <bool>(); await loopTask.Task; return(true); }
public MutilCacheStore(IMemoryStore memoryStore, IRedisStore redisStore) { _memoryStore = memoryStore; _redisStore = redisStore; _subscribeObject = _redisStore.Client.Subscribe((TOPIC_NAME, (msg) => { if (msg == null || string.IsNullOrEmpty(msg.Body)) { return; } var message = msg.Body.ToObject <CacheMessage>(); if (message == null) { return; } OnMessage(message); } )); }