Inheritance: IRedisPubSubServer
Exemplo n.º 1
0
        public void BeginListeningForConfigurationChanges()
        {
            try
            {
                if (this.sentinePubSub == null)
                {
                    var sentinelManager = new BasicRedisClientManager(sentinel.SentinelHosts, sentinel.SentinelHosts)
                    {
                        //Use BasicRedisResolver which doesn't validate non-Master Sentinel instances
                        RedisResolver = new BasicRedisResolver(sentinel.SentinelEndpoints, sentinel.SentinelEndpoints)
                    };
                    this.sentinePubSub = new RedisPubSubServer(sentinelManager)
                    {
                        HeartbeatInterval      = null,
                        IsSentinelSubscription = true,
                        ChannelsMatching       = new[] { RedisPubSubServer.AllChannelsWildCard },
                        OnMessage = SentinelMessageReceived
                    };
                }
                this.sentinePubSub.Start();
            }
            catch (Exception ex)
            {
                Log.Error("Error Subscribing to Redis Channel on {0}:{1}"
                          .Fmt(this.sentinelClient.Host, this.sentinelClient.Port), ex);

                if (OnSentinelError != null)
                {
                    OnSentinelError(ex);
                }
            }
        }
        public void Execute(string ipAddress)
        {
            Manager = new RedisManagerPool(ipAddress);
            StartedAt = DateTime.UtcNow;

            var q = new Timer { Interval = 1000 };
            q.Elapsed += OnInterval;
            q.Enabled = true;

            using (PubSubServer = new RedisPubSubServer(Manager, Channel)
            {
                OnStart = () =>
                {
                    Console.WriteLine("OnStart: #" + Interlocked.Increment(ref StartCount));
                },
                OnHeartbeatSent = () =>
                {
                    Console.WriteLine("OnHeartbeatSent: #" + Interlocked.Increment(ref HeartbeatsSent));
                },
                OnHeartbeatReceived = () =>
                {
                    Console.WriteLine("OnHeartbeatReceived: #" + Interlocked.Increment(ref HeartbeatsReceived));
                },
                OnMessage = (channel, msg) =>
                {
                    Console.WriteLine("OnMessage: @" + channel + ": " + msg);
                },
                OnStop = () =>
                {
                    Console.WriteLine("OnStop: #" + Interlocked.Increment(ref StopCount));
                },
                OnError = ex =>
                {
                    Console.WriteLine("OnError: #" + Interlocked.Increment(ref ErrorCount) + " ERROR: " + ex);
                },
                OnFailover = server =>
                {
                    Console.WriteLine("OnFailover: #" + Interlocked.Increment(ref FailoverCount));
                },
                OnDispose = () =>
                {
                    Console.WriteLine("OnDispose: #" + Interlocked.Increment(ref DisposeCount));
                },
                OnUnSubscribe = channel =>
                {
                    Console.WriteLine("OnUnSubscribe: #" + Interlocked.Increment(ref UnSubscribeCount) + " channel: " + channel);
                },
            })
            {
                Console.WriteLine("PubSubServer StartedAt: " + StartedAt.ToLongTimeString());
                PubSubServer.Start();

                "Press Enter to Quit...".Print();
                Console.ReadLine();
                Console.WriteLine("PubSubServer EndedAt: " + DateTime.UtcNow.ToLongTimeString());
                Console.WriteLine("PubSubServer TimeTaken: " + (DateTime.UtcNow - StartedAt).TotalSeconds + "s");
            }
        }
        public void BeginListeningForConfigurationChanges()
        {
            try
            {
                lock (oLock)
                {
                    if (this.sentinePubSub == null)
                    {
                        var sentinelManager = new BasicRedisClientManager(sentinel.SentinelHosts, sentinel.SentinelHosts)
                        {
                            //Use BasicRedisResolver which doesn't validate non-Master Sentinel instances
                            RedisResolver = new BasicRedisResolver(sentinel.SentinelEndpoints, sentinel.SentinelEndpoints)
                        };

                        if (Log.IsDebugEnabled)
                        {
                            Log.Debug($"Starting subscription to {sentinel.SentinelHosts.ToArray()}, replicas: {sentinel.SentinelHosts.ToArray()}...");
                        }

                        this.sentinePubSub = new RedisPubSubServer(sentinelManager)
                        {
                            HeartbeatInterval      = null,
                            IsSentinelSubscription = true,
                            ChannelsMatching       = new[] { RedisPubSubServer.AllChannelsWildCard },
                            OnMessage = SentinelMessageReceived
                        };
                    }
                }

                this.sentinePubSub.Start();
            }
            catch (Exception ex)
            {
                Log.Error($"Error Subscribing to Redis Channel on {sentinelClient.Host}:{sentinelClient.Port}", ex);

                if (OnSentinelError != null)
                {
                    OnSentinelError(ex);
                }
            }
        }
		public static void Main (string[] args)
		{
			//host is a locally running version of redis
			var host = "localhost:6379";

			//make a client and send a value pair
			RedisClient rc = new RedisClient (host);
			rc.SetValue ("hello", "world");

			//init pubsub and listen for messages posted on foo channel
			var clientsManager = new PooledRedisClientManager(host);

				var redisPubSub = new RedisPubSubServer (clientsManager, "foo") {
					OnMessage = (channel, msg) => "Received '{0}' from '{1}'".Print (msg, channel),

				}.Start ();

			//keep the program alive for a bit
			Thread.Sleep (50000);

		}	
Exemplo n.º 5
0
        /// <summary>
        /// 发布
        /// </summary>
        public void Pub()
        {
            ServiceStack.Redis.IRedisClientsManager RedisClientManager = new ServiceStack.Redis.PooledRedisClientManager("127.0.0.1.65:6379");
            //发布、订阅服务 IRedisPubSubServer
            ServiceStack.Redis.RedisPubSubServer pubSubServer = new ServiceStack.Redis.RedisPubSubServer(RedisClientManager, "channel-1");

            pubSubServer.OnMessage = (channel, msg) =>
            {
                Console.WriteLine(string.Format("服务端:频道{0}接收消息:{1}    时间:{2}", channel, msg, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
                Console.WriteLine("___________________________________________________________________");
            };
            pubSubServer.OnStart = () =>
            {
                Console.WriteLine("发布服务已启动");
                Console.WriteLine("___________________________________________________________________");
            };
            pubSubServer.OnStop        = () => { Console.WriteLine("服务停止"); };
            pubSubServer.OnUnSubscribe = (channel) => { Console.WriteLine(channel); };
            pubSubServer.OnError       = (e) => { Console.WriteLine(e.Message); };
            pubSubServer.OnFailover    = (s) => { Console.WriteLine(s); };
            pubSubServer.Start();
        }
        public void BeginListeningForConfigurationChanges()
        {
            try
            {
                if (this.sentinePubSub == null)
                {
                    var sentinelManager = new BasicRedisClientManager(sentinel.SentinelHosts, sentinel.SentinelHosts) 
                    {
                        //Use BasicRedisResolver which doesn't validate non-Master Sentinel instances
                        RedisResolver = new BasicRedisResolver(sentinel.SentinelHosts, sentinel.SentinelHosts)
                    };
                    this.sentinePubSub = new RedisPubSubServer(sentinelManager)
                    {
                        HeartbeatInterval = null,
                        IsSentinelSubscription = true,
                        ChannelsMatching = new[] { RedisPubSubServer.AllChannelsWildCard },
                        OnMessage = SentinelMessageReceived
                    };
                }
                this.sentinePubSub.Start();
            }
            catch (Exception ex)
            {
                Log.Error("Error Subscribing to Redis Channel on {0}:{1}"
                    .Fmt(this.sentinelClient.Host, this.sentinelClient.Port), ex);

                if (OnSentinelError != null)
                    OnSentinelError(ex);
            }
        }
 public void Publish()
 {
     try
     {
         ServiceStack.Redis.IRedisClientsManager RedisClientManager = new RedisClientConfig().GetRedisClientManager();
         //发布、订阅服务 IRedisPubSubServer
         ServiceStack.Redis.IRedisPubSubServer pubSubServer = new ServiceStack.Redis.RedisPubSubServer(RedisClientManager, _channelName);
         //接收消息事件
         pubSubServer.OnMessage = (channel, msg) =>
         {
             if (_onPublicMessage != null)
             {
                 _onPublicMessage(channel, msg, _mainHttpApplication);
             }
         };
         //启动服务事件处理事件
         pubSubServer.OnStart = () =>
         {
             if (_onPublicStart != null)
             {
                 _onPublicStart();
             }
         };
         //停止服务事件处理事件
         pubSubServer.OnStop = () =>
         {
             if (_onPublicStop != null)
             {
                 _onPublicStop();
             }
         };
         //取消发布处理事件
         pubSubServer.OnUnSubscribe = (channel) =>
         {
             if (_onPublicUnSubscribe != null)
             {
                 _onPublicUnSubscribe(channel, _mainHttpApplication);
             }
         };
         //发布报错时处理事件
         pubSubServer.OnError = (e) =>
         {
             if (_onPublicError != null)
             {
                 _onPublicError(e);
             }
         };
         //发布故障时处理事件
         pubSubServer.OnFailover = (s) =>
         {
             if (_onPublicFailover != null)
             {
                 _onPublicFailover(s);
             }
         };
         //启动发布服务
         pubSubServer.Start();
     }
     catch (Exception ex)
     {
         //Log.Error("EasyFrameRedis", "Publish", ex.Message);
     }
 }