public RedisMessageBus(string server, int port, string password, int db, IEnumerable<string> keys, IDependencyResolver resolver) : base(resolver) { _db = db; _keys = keys.ToArray(); _connection = new RedisConnection(host: server, port: port, password: password); _connection.Closed += OnConnectionClosed; _connection.Error += OnConnectionError; // Start the connection _connectTask = _connection.Open().Then(() => { // Create a subscription channel in redis _channel = _connection.GetOpenSubscriberChannel(); // Subscribe to the registered connections _channel.Subscribe(_keys, OnMessage); // Dirty hack but it seems like subscribe returns before the actual // subscription is properly setup in some cases while (_channel.SubscriptionCount == 0) { Thread.Sleep(500); } }); }
public bool ConnectionReady() { if (_redisConnection == null || _redisConnection.State != RedisConnectionBase.ConnectionState.Open) { lock (_connectionLock) { if (_redisConnection == null || _redisConnection.State != RedisConnectionBase.ConnectionState.Open) { _redisConnection = new RedisConnection(Host, Port, password: Password); try { _redisConnection.Open(); _subscriberConnection = _redisConnection.GetOpenSubscriberChannel(); _subscriberConnection.MessageReceived += OnRedisMessageReceived; _subscriberConnection.Error += OnRedisError; return true; } catch (Exception ex) { return false; } } } } return true; }
public RedisSignalBus(RedisConnection redisConnection) { _handlers = new ConcurrentDictionary<string, SafeSet<EventHandler<SignaledEventArgs>>>(); _redisConnection = redisConnection; _subscriberConnection = _redisConnection.GetOpenSubscriberChannel(); _subscriberConnection.MessageReceived += OnRedisMessageReceived; }
/// <summary> /// This is a small application to test event subscriptions in redis - which are required for automatic expiry of cache items /// if subscriptions dont work try setting the redis config : /// config set notify-keyspace-events Ex /// </summary> /// <param name="args"></param> private static void Main(string[] args) { Console.WriteLine("Start"); _conn = new RedisConnection("localhost"); var c = _conn.Open(); c.Wait(); Console.WriteLine("Conn : " + _conn.State); _conn.Keys.Remove(_db, "_expireys"); _subConn = new RedisSubscriberConnection("localhost"); var s = _subConn.Open(); s.Wait(); Console.WriteLine("SubConn : " + _subConn.State); channel = _conn.GetOpenSubscriberChannel(); Thread.Sleep(100); Console.WriteLine("Channel : " + channel.State); channel.PatternSubscribe("*:expired", OnExecutionCompleted).Wait(); Console.WriteLine("Subscriptions : " + channel.SubscriptionCount); Set(1, 4); Thread.Sleep((6 * 1000) ); if (received > 0) { Console.WriteLine("Subscriptions have worked"); } else { Console.WriteLine("Subscriptions have not worked"); } Console.ReadKey(); }