A preface

Although there are message queues, we still need to take a look at the Redis publish subscribe model at !!!!!

Publish and subscribe mode

  • PUBLISH sends information to the channel, which is called the Publisher publisher;
  • SUBSCRIBE SUBSCRIBE information to command channel, this client is called subscriber subscriber;
  • The name of the publish and subscription module in Redis is PubSub, which is PublisherSubscriber;
  • One publisher sends messages to one channel, and subscribers can subscribe messages to multiple channels; When a publisher publishes a message to a channel, if a subscriber subscribes to the channel, the subscriber receives the message; It’s kind of like a radio station, where I listen to a radio channel, and when the channel sends a message, I get the message;

Three PUBSub module commands

  • Subscribe: to subscribe to one or more channels;
  • Unsubscribe: to unsubscribe from one or more channels;
  • Publish: sends a message to a channel;
  • Psubscribe: Subscribes to all channels that match a given pattern;
  • Punsubscribe: Unsubscribe all channels of a given mode. If no mode is specified, unsubscribe all channels.

To use the help command, run the help command. The following is an example:

help subscribe
Copy the code

Four client implementation

SUBSCRIBE to a channel and create a new channel if it doesn’t exist. This client is then the subscriber

127.0.0.1:6379> subscribe zszxz
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "zszxz"
3) (integer) 1

Copy the code

Send a message to the channel with the command publish; The client is the publisher

127.0.0.1:6379> publish zszxz "l miss you"
(integer) 1
127.0.0.1:6379>
Copy the code

Let’s look at the client and get the message

127.0.0.1:6379> subscribe zszxz
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "zszxz"
3) (integer) 1
1) "message"
2) "zszxz"
3) "l miss you"
Copy the code

Five Java implementation

Define 2 subscribers to subscribe to the channel message, in the use of Jedis need to inherit JedisPubSub class, rewrite the onMessage method; The subscriber can process the business logic of the message in this method;

The subscriber 1

/ * * *@AuthorLSC * <p> Subscriber 1 </p> */
@Component
public class Sub1 extends JedisPubSub {

    @Override
    public void onMessage(String channel, String message) {
        System.out.println("sub1 channel is :"+ channel+ " mesage is :"+message); }}Copy the code

The subscriber 2

/ * * *@AuthorLSC * <p> Subscriber 2 </p> */
@Component
public class Sub2 extends JedisPubSub {

    @Override
    public void onMessage(String channel, String message) {
        System.out.println("sub2 channel is :"+ channel+ " mesage is :"+message); }}Copy the code

The publisher

/ * * *@Author lsc
 * <p> </p>
 */
@Component
public class Pub {

    public void publishMessage(Jedis jedis, String channel, String msg) { jedis.publish(channel,msg); }}Copy the code

The test class,

Note that Redis’s publisk-subscribe mode blocks, requiring a subscriber to restart a thread;

    @Autowired
    Pub pub;

    @Autowired
    JedisUtil jedisUtil;

    @Autowired
    Sub1 sub1;

    @Autowired
    Sub1 sub2;

    @Test
    public void test(a){

        new Thread(()-> {
            while (true){
                jedisUtil.getJedis().subscribe(sub1,"zszxz");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch(InterruptedException e) { e.printStackTrace(); }}//jedisUtil.getJedis().subscribe(sub2,"zszxz");
        }).start();
        pub.publishMessage(jedisUtil.getJedis(),"zszxz"."l miss you");
    }
Copy the code

Six disadvantages

  • A PubSub producer passes a message directly to the consumer. If there is no consumer, the message is simply discarded. If there are multiple consumers and one consumer suddenly hangs up, the producer will continue to send messages, and the other consumers will continue to receive messages. However, the message during disconnection will be completely lost after the consumers who hang up are reconnected.
  • If Redis stops and restarts, PubSub messages will not persist

Please focus on