RedisPubSub
2014-11-17 17:10
288 查看
RedisPubSub
mythz edited this page on 8 Feb 2011 · 2 revisions
Pages 16
AuthenticationCaching
DesigningNoSqlDatabase
Designingnosqldatabase
Home
IRedisClient
IRedisNativeClient
IRedisTransaction
IRedisTypedClient
IRedisTypedTransaction
MigrationsUsingSchemalessNoSql
Redis
Client Release Notes
RedisLocks
RedisPubSub
RedisTransactions
Show
1 more pages…
Clone this wiki locally
Clone in Desktop
This page shows how to use ServiceStack's C# Redis
Client to take advantage of the Publish/Subscribe messaging paradigm built into Redis to be able to develop high-performance
network notification servers.
Publish/Subscribe
messaging pattern in Redis
Redis is largely recognized as the most efficient NoSQL solution to manage rich data structures, providing the data source to power stateless back-end app servers. What a lot of people don't know is that it alsohas a number of characteristics that also make it a prime candidate to develop high-performance networking solutions with. Probably its most noteworthy feature in this area to date, is its built-in Publishing / Subscribe / Messaging support which enables a
new range of elegant comet-based and high performance networking solutions to be developed.
Under the covers this is achieved with the PUBLISH/SUBSCRIBE
redis server operations. Essentially these operations allows any number of clients to be able to Listen on any arbitrary named channel. As soon as an external client Publishesa message to that channel, each listening client
is notified. The clients will continue to receive messages as long as they maintain at least one active subscription.
Service Stack's C# Client exposes this functionality in a similar way as the redis-rb
Ruby client. Essentially you create a Subscription, Then Register your handlers on each of the events you're interested in, then it's just a matter of Subscribing to the channels you're
interested in. When you want your consumers to stop receiving messages you need to unsubscribe from all channels.
Pub/Sub
Examples
Below are some examples showing how to use the API to accomplish some basic tasks. At the end of each example is the Console output showing the sequence of events helping you visualize the order of each operation.The full runnable source code for these examples are available
here.
Example
1 - Publish and receive 5 messages
var messagesReceived = 0; using (var redisConsumer = new RedisClient(TestConfig.SingleHost)) using (var subscription = redisConsumer.CreateSubscription()) { subscription.OnSubscribe = channel => { Console.WriteLine("Subscribed to '{0}'", channel); }; subscription.OnUnSubscribe = channel => { Console.WriteLine("UnSubscribed from '{0}'", channel); }; subscription.OnMessage = (channel, msg) => { Console.WriteLine("Received '{0}' from channel '{1}'", msg, channel); //As soon as we've received all 5 messages, disconnect by unsubscribing to all channels if (++messagesReceived == PublishMessageCount) { subscription.UnSubscribeFromAllChannels(); } }; ThreadPool.QueueUserWorkItem(x => { Thread.Sleep(200); Console.WriteLine("Begin publishing messages..."); using (var redisPublisher = new RedisClient(TestConfig.SingleHost)) { for (var i = 1; i <= PublishMessageCount; i++) { var message = MessagePrefix + i; Console.WriteLine("Publishing '{0}' to '{1}'", message, ChannelName); redisPublisher.PublishMessage(ChannelName, message); } } }); Console.WriteLine("Started Listening On '{0}'", ChannelName); subscription.SubscribeToChannels(ChannelName); //blocking } Console.WriteLine("EOF"); /*Output: Started Listening On 'CHANNEL' Subscribed to 'CHANNEL' Begin publishing messages... Publishing 'MESSAGE 1' to 'CHANNEL' Received 'MESSAGE 1' from channel 'CHANNEL' Publishing 'MESSAGE 2' to 'CHANNEL' Received 'MESSAGE 2' from channel 'CHANNEL' Publishing 'MESSAGE 3' to 'CHANNEL' Received 'MESSAGE 3' from channel 'CHANNEL' Publishing 'MESSAGE 4' to 'CHANNEL' Received 'MESSAGE 4' from channel 'CHANNEL' Publishing 'MESSAGE 5' to 'CHANNEL' Received 'MESSAGE 5' from channel 'CHANNEL' UnSubscribed from 'CHANNEL' EOF */
Example
2 - Publish 5 messages to 3 different clients
const int noOfClients = 3; for (var i = 1; i <= noOfClients; i++) { var clientNo = i; ThreadPool.QueueUserWorkItem(x => { using (var redisConsumer = new RedisClient(TestConfig.SingleHost)) using (var subscription = redisConsumer.CreateSubscription()) { var messagesReceived = 0; subscription.OnSubscribe = channel => { Console.WriteLine("Client #{0} Subscribed to '{1}'", clientNo, channel); }; subscription.OnUnSubscribe = channel => { Console.WriteLine("Client #{0} UnSubscribed from '{1}'", clientNo, channel); }; subscription.OnMessage = (channel, msg) => { Console.WriteLine("Client #{0} Received '{1}' from channel '{2}'", clientNo, msg, channel); if (++messagesReceived == PublishMessageCount) { subscription.UnSubscribeFromAllChannels(); } }; Console.WriteLine("Client #{0} started Listening On '{1}'", clientNo, ChannelName); subscription.SubscribeToChannels(ChannelName); //blocking } Console.WriteLine("Client #{0} EOF", clientNo); }); } using (var redisClient = new RedisClient(TestConfig.SingleHost)) { Thread.Sleep(500); Console.WriteLine("Begin publishing messages..."); for (var i = 1; i <= PublishMessageCount; i++) { var message = MessagePrefix + i; Console.WriteLine("Publishing '{0}' to '{1}'", message, ChannelName); redisClient.PublishMessage(ChannelName, message); } } Thread.Sleep(500); /*Output: Client #1 started Listening On 'CHANNEL' Client #2 started Listening On 'CHANNEL' Client #1 Subscribed to 'CHANNEL' Client #2 Subscribed to 'CHANNEL' Client #3 started Listening On 'CHANNEL' Client #3 Subscribed to 'CHANNEL' Begin publishing messages... Publishing 'MESSAGE 1' to 'CHANNEL' Client #1 Received 'MESSAGE 1' from channel 'CHANNEL' Client #2 Received 'MESSAGE 1' from channel 'CHANNEL' Publishing 'MESSAGE 2' to 'CHANNEL' Client #1 Received 'MESSAGE 2' from channel 'CHANNEL' Client #2 Received 'MESSAGE 2' from channel 'CHANNEL' Publishing 'MESSAGE 3' to 'CHANNEL' Client #3 Received 'MESSAGE 1' from channel 'CHANNEL' Client #3 Received 'MESSAGE 2' from channel 'CHANNEL' Client #3 Received 'MESSAGE 3' from channel 'CHANNEL' Client #1 Received 'MESSAGE 3' from channel 'CHANNEL' Client #2 Received 'MESSAGE 3' from channel 'CHANNEL' Publishing 'MESSAGE 4' to 'CHANNEL' Client #1 Received 'MESSAGE 4' from channel 'CHANNEL' Client #3 Received 'MESSAGE 4' from channel 'CHANNEL' Publishing 'MESSAGE 5' to 'CHANNEL' Client #1 Received 'MESSAGE 5' from channel 'CHANNEL' Client #3 Received 'MESSAGE 5' from channel 'CHANNEL' Client #1 UnSubscribed from 'CHANNEL' Client #1 EOF Client #3 UnSubscribed from 'CHANNEL' Client #3 EOF Client #2 Received 'MESSAGE 4' from channel 'CHANNEL' Client #2 Received 'MESSAGE 5' from channel 'CHANNEL' Client #2 UnSubscribed from 'CHANNEL' Client #2 EOF */
Pub/Sub
API
public interface IRedisSubscription : IDisposable { // The number of active subscriptions this client has int SubscriptionCount { get; } // Registered handler called after client *Subscribes* to each new channel Action<string> OnSubscribe { get; set; } // Registered handler called when each message is received Action<string, string> OnMessage { get; set; } // Registered handler called when each channel is unsubscribed Action<string> OnUnSubscribe { get; set; } // Subscribe to channels by name void SubscribeToChannels(params string[] channels); // Subscribe to channels matching the supplied patterns void SubscribeToChannelsMatching(params string[] patterns); void UnSubscribeFromAllChannels(); void UnSubscribeFromChannels(params string[] channels); void UnSubscribeFromChannelsMatching(params string[] patterns); }
Find
out more
For more information on the capabilities of the Publish/Subscribe API check out the PubSubunit tests.
相关文章推荐
- phpredis---pub/sub订阅超时
- Redis pub/sub机制在实际运用场景的缺陷&&模拟JMS消息发布订阅的持久化特性
- Redis客户端之Jedis(Key、String、Set、List、pub/sub、cluster、pool、pipleline)
- Redis 中 PubSub 的初步使用示例
- 使用Redis的Pub/Sub来实现类似于JMS的消息持久化
- redis的pub与sub笔记
- Redis编程实践【pub/sub】
- Spring Data Redis—Pub/Sub(附Web项目源码)
- redis 高级应用之二(Redis的持久化 和 消息的[pub/sub]发布和订阅)
- Spring Data Redis(Redis Messaging/PubSub)
- Redis的pub/Sub(订阅与发布)在java中的实践
- php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用
- Redis教程03——Redis 发布/订阅(Pub/Sub)
- redis的发布订阅模式pubsub
- Redis发布与订阅(pub/sub)
- Redis实践:使用Pub/Sub实现远程Trace或Log信息跟踪监控
- NoSQL数据库之Redis数据库管理六(Redis的高级应用之事务处理、持久化操作、pub_sub、虚拟内存)
- redis的pub和sub
- 用redis 做一个简单的 pub / sub 系统