Servicestack. redis subscription and publishing service call (z)

Source: Internet
Author: User

1. redis subscription publishing Introduction
Redis subscription and publishing is a message communication mode: the publisher (publisher) sends messages, and the subscriber (subscriber) receives messages. Similar to the observer mode in design patterns.
The publisher and subscriber use a channel for communication. When a message needs to be sent, the subscriber sends the message to the channel through the publish command, and the message is sent to the subscriber of the channel.

Picture from http://www.runoob.com/redis/redis-pub-sub.html

2. servicestack. redis
Servicestack. redis is the C # client of redis and is part of servicestack.
Servicestack address is https://servicestack.net

 

3. subscriber
Create a redisclient first, call the createsubscription () method to create a subscription client, and then set the events of the subscription client:
Onmessage: when the message is received.
Onsubscribe: when subscribing to a channel.
Onunsubscribe: When the channel is canceled.
Finally, subscribetochannels (channelname) is called to subscribe to the channel.
The Code is as follows:

1 /// <summary> 2 // redis subscription 3 /// </Summary> 4 public static void subscribe () 5 {6 using (redisclient consumer = new redisclient ("127.0.0.1", 6379) 7 {8 // create subscription 9 iredissubscription subscription = consumer. createsubtasks (); 10 // 11 subtasks when the message is received. onmessage = (Channel, MSG) => 12 {13 console. writeline ($ "received message from Channel: {channel}: {MSG}, time: {datetime. now. tostring ("yyyymmdd hh: mm: SS")} "); 14 console. writeline ($ "Number of channel subscriptions: {subscribe. subscriptioncount} "); 15 console. writeline ("___________________________________________________________________"); 16}; 17 // when subscribing to the channel, 18 subtopics. onsubscribe = (Channel) => 19 {20 console. writeline ("subscribe client: Subscribe" + channel); 21}; 22 // 23 subscribe When the subscribe channel is canceled. onunsubscribe = (A) => {console. writeline ("subscribe client: Unsubscribe") ;}; 24 25 // subscribe channel 26 subscribe. subscribetochannels ("channel1"); 27} 28}

4. Publisher
Create a redisclient and call publishmessage (channelname, message) to publish the message.
The Code is as follows:
Redisclient client = new redisclient ("127.0.0.1", 6379 );
String message = "Publish message test ";
Client. publishmessage ("channel1", message );

So far, a simple redis publishing and subscription has been completed.


5. redis release service
The publisher can only publish messages, but cannot detect changes in some events. redis also has a redispublishserver class, which includes some events that can enable us to well detect the running of services.
Onmessage: receives the message;
Onstart: When the Publishing Service starts running;
Onstop: When the Publishing Service stops running;
Onunsubscribe: When the subscriber cancels the subscription;
Onerror: When an error occurs during publishing;
Onfailover: During redis server redundancy switchover;
After the Publishing Server Initialization is complete, call the START () method to start executing the Publishing Service.

After the Publishing Service is executed, when client. publishmessage is published, the Publishing Server can also receive the published message.
The Code is as follows:

1 Public void publish () 2 {3 // pooledredisclientmanager 4 iredisclientsmanager redisclientmanager = new pooledredisclientmanager ("127.0.0.1: 6379 "); 5 // publish and subscribe to iredispubsubserver 6 redispubsubserver pubsubserver = new redispubsubserver (redisclientmanager, "channel1") 7 {8 onmessage = (Channel, MSG) => 9 {10 console. writeline ($ "received message from Channel: {channel}: {MSG}, time: {datetime. now. tostring ("yyyymmdd hh: mm: SS")} "); 11 console. writeline ("___________________________________________________________________"); 12}, 13 onstart = () => 14 {15 console. writeline ("published service started"); 16 console. writeline ("___________________________________________________________________"); 17}, 18 onstop = () => {console. writeline ("stop publishing service") ;}, 19 onunsubscribe = channel =>{ console. writeline (Channel) ;}, 20 onerror = e =>{ console. writeline (E. message) ;}, 21 onfailover = s =>{ console. writeline (s) ;}, 22 }; 23 // receives the message 24 pubsubserver. start (); 25}

Servicestack. redis subscription and publishing service call (z)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.