[Mr. Zhao Qiang] news release and subscription of redis

Time:2021-10-7

As a publish / subscribe server, redis plays the role of message routing. Subscribers can subscribe the message types they are interested in to the redis server through the subscribe and psubscribe commands. When the publisher sends a specific type of message to the redis server through the publish command. All clients subscribing to this message type will receive this message. Message delivery here is many to many. A client can subscribe to multiple channels or send messages to multiple channels.

The following figure shows the architecture of redis message mechanism.

Both the publisher and subscriber are redis clients, and the channel is the redis server. The publisher sends a message to a channel, and subscribers who subscribe to the channel can receive the message. Redis’s publish subscribe mechanism is similar to topic based publish subscribe, and channel is equivalent to topic.

The following lists the commands related to redis publishing and subscribing to messages.

publish:
Send message: redis sends a message using the publish command, and its return value is the number of subscribers who have received the message. 

subscribe:
Subscribe to a channel: redis subscribes to a channel using the subscribe command. Its return value includes the channels subscribed by the client, the number of channels subscribed at present, and the messages received. Subscribe indicates that a channel has been successfully subscribed to. 

psubscribe:
Pattern matching: the pattern matching function allows the client to subscribe to channels that conform to a certain pattern. Redis uses psubscribe to subscribe to all channels that conform to a certain pattern, with "" representing the pattern, and "" can be replaced by any value.

Case 1: one message producer and two message consumers

Case 2: two message producers and one message consumer

Case 3: Java API of redis message mechanism

Add dependency:

redis.clients
    jedis
    3.1.0

 

Message listener class:

import redis.clients.jedis.JedisPubSub;
 
public class RedisMsgPubSubListener extends JedisPubSub {
    @Override
    public void unsubscribe() {
        super.unsubscribe();
    }
 
    @Override
    public void unsubscribe(String... channels) {
        super.unsubscribe(channels);
    }
 
    @Override
    public void subscribe(String... channels) {
        super.subscribe(channels);
    }
 
    @Override
    public void psubscribe(String... patterns) {
        super.psubscribe(patterns);
    }
 
    @Override
    public void punsubscribe() {
        super.punsubscribe();
    }
 
    @Override
    public void punsubscribe(String... patterns) {
        super.punsubscribe(patterns);
    }
 
    @Override
    public void onMessage(String channel, String message) {
        System.out.println("channel:" + channel + "receives message :" + message);
        this.unsubscribe();
    }
 
    @Override
    public void onPMessage(String pattern, String channel, String message) {
 
    }
 
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);
    }
 
    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
 
    }
 
    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {
 
    }
 
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been unsubscribed:" + subscribedChannels);
    }
}

 

Test procedure:

import redis.clients.jedis.Jedis;

public class TestMain {

   @Test
    public void testSubscribe() throws Exception{
        Jedis jedis = new Jedis("localhost");
        RedisMsgPubSubListener listener = new RedisMsgPubSubListener();
        jedis.subscribe(listener, "redisChatTest");
        //other code
    }

    @Test
    public void testPublish() throws Exception{
        Jedis jedis = new Jedis("localhost");
        jedis.publish("redisChatTest", "Hello World");
        Thread.sleep(5000);
        jedis.publish("redisChatTest", "Hello Redis");
    }
}

Recommended Today

Browser rearrangement (reflow) redrawing and optimization scheme

1、 What is rearrangement and redrawing To clarify reflow and repaint, you must first understand the arrangement and rendering. When the browser renders a page, after obtaining HTML and CSS resources, it will roughly go through the following steps.(1) HTML generate HTML tree(2) CSS forms CSS rules(3) The two form a rendering tree(4) Go to […]