6. redis publishing and subscription

Source: Internet
Author: User

Pub/sub is a message communication mode. The main purpose is to decouple the coupling between the message publisher and the message subscriber, which is similar to the observer mode in the design mode. Pub/sub not only solves the Direct Problem of publishers and subscribersCodeLevel coupling also solves the coupling between the two in physical deployment. As a pub/sub server, redis enables message routing between subscribers and publishers. The subscribe and psubscribe commands subscribe to the redis server to subscribe to the Message type that you are interested in. redis calls the message type channel ). When the publisher sends a specific type of message to the redis server through the publish command. All clients that subscribe to this message type will receive this message. Here, messages are delivered in many-to-many ways. A client can subscribe to multiple channels or send messages to multiple channels.
The following is an experiment. Here we use two different clients: redis-CLI, which comes with redis, and simple client written in Java. The Code is as follows:

 Import Java.net .*;
Import Java. Io .*;
Public Class Pubsubtest
{
Public Static Void Main (string [] ARGs)
{
String cmd =
ARGs [0] + "\ R \ n" ;
Try
{
Socket
Socket = New
Socket ( "192.168.56.55" , 6379 );

Inputstream in =
Socket. getinputstream ();

Outputstream out = socket. getoutputstream ();

Out. Write (CMD. getbytes ());
// Send subscription command

Byte [] Buffer = New
Byte [1024];

While (True)
{

Int Readcount =
In. Read (buffer );

System. Out. Write (buffer, 0,
Readcount );

System. Out. println ( "--------------------------------------" );

}
} Catch (Exception E)
{
}

}
}

The Code simply reads the SUBSCRIBE command from the command line and sends it to the redis server through a socket connection. Then, the while loop reads the messages subscribed by the redis server. And print it to the console
1. Compile and run the JavaProgram(I am running under win7)

D :\>Javac pubsubtest. Java
D :\>Java pubsubtest "subscribe
News. Share
News. Blog"
* 3
$9
Subscribe
$10
News. Share
: 1
* 3
$9
Subscribe
$9
News. Blog
: 2

--------------------------------------
2. Start redis-cli

Redis>Psubscribe news .*
Reading messages... (Press
CTRL-C to quit)
1. "psubscribe"
2. "news .*"
3. (integer) 1

3. Start another redis-CLI to publish two messages.

Redis>Publish news. Share"ShareLink 
HTTP:// Www.google.com"
(Integer) 2
Redis>Publish
News. Blog"I post a blog"
(Integer) 2

4. view the output of two subscription clients
In this case, the Java client prints the following content:

* 3
$7
Message
$10
News. Share
$34
ShareLink
HTTP:// Www.google.com
--------------------------------------
* 3
$7
Message
$9
News. Blog
$13
I
Post a blog
--------------------------------------
The output of another redis-CLI is as follows:
1. "pmessage"
2. "news .*"
3. "News. Share"
4. "shareLink
HTTP:// Www.google.com"
1. "pmessage"
2. "news .*"
3. "News. Blog"
4.
"I post a blog"
Under Analysis
Java client uses the SUBSCRIBE command to subscribe to news. share and news. blog two channels, and then immediately receive the successful subscription message returned by the server. It can be seen that the redis Protocol is of the text type. Here we do not explain the specific Protocol content. For details, refer to dig. This packet has two parts. The first part indicates that the socket connection uses subscribe to news. after a successful share, the number of subscription channels for this connection is 1, and the last part indicates that the subscribe is used to subscribe to news. after the blog is successful, the total number of connections for reading is 2.
Redis client uses psubscribe to subscribe to a channel that uses wildcards (* represents any string). This subscription will receive all channel messages that match news. A successful message printed from redis-CLI to the Console indicates that after the message is successfully subscribed using the psubscribe command, the total number of connected subscription channels is 1.
When publish is used in a client to send two messages to the news. Share and news. Blog channels. Redis returns (integer) 2, indicating that two connections have received the message. It can be verified by observing the output of two subscribers. The specific format is simple.
After reading a small example, we should have a perceptual knowledge of the pub/sub function. It should be noted that when a connection is subscribed through the subscribe or psubscribe channel, it enters the subscription mode. In this mode, you cannot send other commands except subscribe to additional channels or exit the subscription mode using the unsubscribe or punsubscribe commands. In addition, the psubscribe command is used to subscribe to multiple wildcard channels. If a message matches multiple channel modes, the same message is received multiple times.
The current version of jredis does not support pub/sub, but it should be quite simple to implement it. The entire application can share the same connection. Because the message returned by redis includes message-related channel information in addition to the message content. After receiving the message, you can call different callback methods based on different channel information.
In addition, I personally think the pub/sub of redis is a little too thin (only 150 lines of code are used for implementation ). There is not much support for security, authentication, and reliability.

From: http://www.cnblogs.com/xhan/archive/2011/02/06/1949473.html

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.