Redis Programming Practice "pub/sub" __ Programming

Source: Internet
Author: User
Tags connection pooling download redis message queue
Redis Programming Practice [pub / sub] Blog Category: Redis
 

    Redis may have been promoted and tested in many enterprises. This article also briefly describes the use of Redis in the actual development process (deployment and architecture, introduced later) based on personal practices. The program execution environment is java + jedis. Let's introduce how to integrate redis-api.

 

Foreword: Download redis-2.6.2, after installing redis, please open the following three configuration properties in the redis.conf file (for testing only): Xml code Redis programming practice [pub / sub] __Programming Redis Programming Practices [pub / sub] __ Programming Redis Programming Practices [pub / sub] __ programming ## Port of the client link, and also the port on which the server listens to the client link ## Each client instance will be at the server A TCP long link is established on this port port 6379 ## The IP address bound to the server. If a physical machine has multiple network interfaces, it can be explicitly specified as the IP address of a certain network port bind 127.0.0.1 ## LINK 中 io Operation idle time, if there is no IO operation within the specified time, the connection will be closed ## However, if you use connection pooling, you need to consider this parameter extra. timeout 0

    ## The port of the client link, and the port on which the server listens to the client link
    ## Each client instance will establish a long TCP link with the server on this port
    port 6379
    ## The IP address bound to the server. If a physical machine has multiple network interfaces, it can be explicitly specified as the IP address of a certain network port.
    bind 127.0.0.1
    ## Link io operation idle time, if there is no IO operation within the specified time, the link will be closed
    ## This property is the same as the timeout option in the TCP link. It is recommended to set it to 0. In many cases, we will only have one redis instance per application.
    ## However, if you use connection pooling, you need to consider this parameter extra.
    timeout 0
 

Pub / Sub: "Publish / Subscribe". For this function, we will think of many JMS implementations. Redis provides this function to be "more and more." However, this function is designed in redis to be very lightweight and concise. It The basic capabilities of "publishing" and "subscribing" to messages have been achieved, but various enterprise-level features such as persistence / durability of messages in JMS have not yet been provided.

    One Redis client publishes messages, and multiple other redis clients subscribe to messages. The published messages are "send immediately and lose." Redis will not persist the published messages; message subscribers will also only get the messages after the subscription. No news will be available. This is similar to the "non-persistent" type of messages in JMS.

    The message publisher, that is, the publish client, does not need an exclusive link. You can use the same redis-client link for other operations while publishing the message (for example: INCR, etc.)

    The message subscriber, the subscribe client, needs an exclusive link, that is, during the subscribe, redis-client cannot intersperse other operations. At this time, the client waits for the "publish" message in a blocking manner; this is well understood, so the subscribe client You need to use a separate link or even use it in an extra thread.

    Once the subscriber is disconnected, some messages will be lost. If you are very concerned about each message, you should consider using JMS or Redis to do some additional supplementary work. If you expect the subscription to be durable, then the following design ideas Can be borrowed (the following principles are based on JMS):

    1) The subscribe terminal first adds a "Subscriber ID" to a Set collection. This Set collection stores "Active Subscribers". The Subscriber ID marks each unique subscriber, for example: sub: email, sub: web. This SET is called "Active Subscriber Collection"

    2) The subcribe terminal starts the subscription operation and creates a LIST data structure with "Subscriber ID" as the KEY based on Redis. This LIST stores all messages that have not yet been consumed. This LIST is called "Subscriber Message Queue"

    3) The publish side: After each message is published, the publish side needs to traverse the "active subscriber set" and in turn append the message of this publication to the tail of each "subscriber message queue".

    4) So far, we can basically guarantee that every message published will be persisted in each "subscriber message queue".

    5) Each time a subscribe receives a subscription message, it must delete a record in its "Subscriber Message Queue" header after consumption.

    6) When the subscribe terminal is started, if it finds that its own "subscriber message queue" has residual records, it will consume these records first, and then subscribe.

 

-------------------------------------------------- ------------ Non-durable subscriptions ---------------------------------- ---------------------

PrintListener.java: Subscriber message handler Java code Redis programming practice [pub / sub] __programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class PrintListener extends JedisPubSub { @Override public void onMessage (String channel, String message) {String time = DateFormatUtils.format (new Date (), "yyyy-MM-dd HH: mm: ss"); System.out.println ("message receive:" + message + ", channel:" + channel + "..." + time); // Here we can unsubscribe if (message.equalsIgnoreCase ("quit")) {this.unsubscribe (channel);}}. ..}

public class PrintListener extends JedisPubSub {

@Override
public void onMessage (String channel, String message) {
String time = DateFormatUtils.format (new Date (), "yyyy-MM-dd HH: mm: ss");
System.out.println ("message receive:" + message + ", channel:" + channel + "..." + time);
// Here we can cancel the subscription
if (message.equalsIgnoreCase ("quit")) {
this.unsubscribe (channel);
}
}
...
}
 
PubClient.java: Java code Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class PubClient {private Jedis jedis; // public PubClient (String host, int port) {jedis = new Jedis (host, port);} public void pub (String channel, String message) {jedis.publish (channel, message);} public void close (String channel ) {jedis.publish (channel, "quit"); jedis.del (channel); //}}

public class PubClient {

private Jedis jedis; //
public PubClient (String host, int port) {
jedis = new Jedis (host, port);
}
Ranch
public void pub (String channel, String message) {
jedis.publish (channel, message);
}
Ranch
public void close (String channel) {
jedis.publish (channel, "quit");
jedis.del (channel); //
}

}
 

SubClient.java: Java code Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class SubClient {private Jedis jedis; // public SubClient (String host, int port) {jedis = new Jedis (host, port);} public void sub (JedisPubSub listener, String channel) {jedis.subscribe (listener, channel); // will block here , When the client code level is JedisPubSub, when processing messages, it will "exclusively" link // and adopt a while loop to listen to the subscribed messages //}}

public class SubClient {

private Jedis jedis; //
Ranch
public SubClient (String host, int port) {
jedis = new Jedis (host, port);
}
Ranch
public void sub (JedisPubSub listener, String channel) {
jedis.subscribe (listener, channel);
// It will block here. At the client code level, JedisPubSub will “exclusively” link when processing messages.
// And adopted a while loop to listen to the subscribed messages
//
}

}
 

PubSubTestMain.java: test guide class Java code Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class PubSubTestMain {/ ** * @param args * / public static void main (String [] args) throRedis Programming Practice [pub / sub] Blog Category: Redis
 

    Redis may have been promoted and tested in many enterprises. This article also briefly describes the use of Redis in the actual development process (deployment and architecture, introduced later) based on personal practices. The program execution environment is java + jedis. Let's introduce how to integrate redis-api.

 

Foreword: Download redis-2.6.2, after installing redis, please open the following three configuration properties in the redis.conf file (for testing only): Xml code Redis programming practice [pub / sub] __Programming Redis Programming Practices [pub / sub] __ Programming Redis Programming Practices [pub / sub] __ programming ## Port of the client link, and also the port on which the server listens to the client link ## Each client instance will be at the server A TCP long link is established on this port port 6379 ## The IP address bound to the server. If a physical machine has multiple network interfaces, it can be explicitly specified as the IP address of a certain network port bind 127.0.0.1 ## LINK 中 io Operation idle time, if there is no IO operation within the specified time, the connection will be closed ## However, if you use connection pooling, you need to consider this parameter extra. timeout 0

    ## The port of the client link, and the port on which the server listens to the client link
    ## Each client instance will establish a long TCP link with the server on this port
    port 6379
    ## The IP address bound to the server. If a physical machine has multiple network interfaces, it can be explicitly specified as the IP address of a certain network port.
    bind 127.0.0.1
    ## Link io operation idle time, if there is no IO operation within the specified time, the link will be closed
    ## This property is the same as the timeout option in the TCP link. It is recommended to set it to 0. In many cases, we will only have one redis instance per application.
    ## However, if you use connection pooling, you need to consider this parameter extra.
    timeout 0
 

Pub / Sub: "Publish / Subscribe". For this function, we will think of many JMS implementations. Redis provides this function to be "more and more." However, this function is designed in redis to be very lightweight and concise. It The basic capabilities of "publishing" and "subscribing" to messages have been achieved, but various enterprise-level features such as persistence / durability of messages in JMS have not yet been provided.

    One Redis client publishes messages, and multiple other redis clients subscribe to messages. The published messages are "send immediately and lose." Redis will not persist the published messages; message subscribers will also only get the messages after the subscription. No news will be available. This is similar to the "non-persistent" type of messages in JMS.

    The message publisher, that is, the publish client, does not need an exclusive link. You can use the same redis-client link for other operations while publishing the message (for example: INCR, etc.)

    The message subscriber, the subscribe client, needs an exclusive link, that is, during the subscribe, redis-client cannot intersperse other operations. At this time, the client waits for the "publish" message in a blocking manner; this is well understood, so the subscribe client You need to use a separate link or even use it in an extra thread.

    Once the subscriber is disconnected, some messages will be lost. If you are very concerned about each message, you should consider using JMS or Redis to do some additional supplementary work. If you expect the subscription to be durable, then the following design ideas Can be borrowed (the following principles are based on JMS):

    1) The subscribe terminal first adds a "Subscriber ID" to a Set collection. This Set collection stores "Active Subscribers". The Subscriber ID marks each unique subscriber, for example: sub: email, sub: web. This SET is called "Active Subscriber Collection"

    2) The subcribe terminal starts the subscription operation and creates a LIST data structure with "Subscriber ID" as the KEY based on Redis. This LIST stores all messages that have not yet been consumed. This LIST is called "Subscriber Message Queue"

    3) The publish side: After each message is published, the publish side needs to traverse the "active subscriber set" and in turn append the message of this publication to the tail of each "subscriber message queue".

    4) So far, we can basically guarantee that every message published will be persisted in each "subscriber message queue".

    5) Each time a subscribe receives a subscription message, it must delete a record in its "Subscriber Message Queue" header after consumption.

    6) When the subscribe terminal is started, if it finds that its own "subscriber message queue" has residual records, it will consume these records first, and then subscribe.

 

-------------------------------------------------- ------------ Non-durable subscriptions ---------------------------------- ---------------------

PrintListener.java: Subscriber message handler Java code Redis programming practice [pub / sub] __programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class PrintListener extends JedisPubSub { @Override public void onMessage (String channel, String message) {String time = DateFormatUtils.format (new Date (), "yyyy-MM-dd HH: mm: ss"); System.out.println ("message receive:" + message + ", channel:" + channel + "..." + time); // Here we can unsubscribe if (message.equalsIgnoreCase ("quit")) {this.unsubscribe (channel);}}. ..}

public class PrintListener extends JedisPubSub {

@Override
public void onMessage (String channel, String message) {
String time = DateFormatUtils.format (new Date (), "yyyy-MM-dd HH: mm: ss");
System.out.println ("message receive:" + message + ", channel:" + channel + "..." + time);
// Here we can cancel the subscription
if (message.equalsIgnoreCase ("quit")) {
this.unsubscribe (channel);
}
}
...
}
 
PubClient.java: Java code Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class PubClient {private Jedis jedis; // public PubClient (String host, int port) {jedis = new Jedis (host, port);} public void pub (String channel, String message) {jedis.publish (channel, message);} public void close (String channel ) {jedis.publish (channel, "quit"); jedis.del (channel); //}}

public class PubClient {

private Jedis jedis; //
public PubClient (String host, int port) {
jedis = new Jedis (host, port);
}
Ranch
public void pub (String channel, String message) {
jedis.publish (channel, message);
}
Ranch
public void close (String channel) {
jedis.publish (channel, "quit");
jedis.del (channel); //
}

}
 

SubClient.java: Java code Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class SubClient {private Jedis jedis; // public SubClient (String host, int port) {jedis = new Jedis (host, port);} public void sub (JedisPubSub listener, String channel) {jedis.subscribe (listener, channel); // will block here , When the client code level is JedisPubSub, when processing messages, it will "exclusively" link // and adopt a while loop to listen to the subscribed messages //}}

public class SubClient {

private Jedis jedis; //
Ranch
public SubClient (String host, int port) {
jedis = new Jedis (host, port);
}
Ranch
public void sub (JedisPubSub listener, String channel) {
jedis.subscribe (listener, channel);
// It will block here. At the client code level, JedisPubSub will “exclusively” link when processing messages.
// And adopted a while loop to listen to the subscribed messages
//
}

}
 

PubSubTestMain.java: test guide class Java code Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming Redis programming practice [pub / sub] __ programming public class PubSubTestMain {/ ** * @param args * / public static void main (String [] args) thro...


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.