The article directories

  • Introduction to the
  • news
  • Subscription publishing processing
  • Subscribe to the channel
  • test

Introduction to the

First of all, we need to know that when a user subscribes to a certain channel, he will always listen to whether the channel sends messages, which is a kind of blocked state. Therefore, we implement the subscription function with multiple threads, and assign a thread to each subscriber.

news



Publish the message method is relatively simple, we just need to specify which channel to publish what content on the line, here I wrapped them in a util class, using static method, easy to use.

public static void publishMsg(String channel, String message) { // Jedis jedis = getJedis(); Jedis Jedis = new Jedis("127.0.0.1",6379); try { jedis.publish(channel, message); } catch (Exception e) { System.out.println(e.getMessage()); } finally { jedis.close(); }}Copy the code

Subscription publishing processing

This class is used to handle subscriptions, that is, what we do when we subscribe to a channel, what we do when we unsubscribe from a channel, what we do when we receive a message. Because I’ve wrapped them all under the util class, I’m going to use the inner class.

public static JedisPubSub jedisPubSub = new JedisPubSub() { @Override public void onMessage(String channel, String message) {system.out.println (" received "+ channel +" channel: "+ message); System.out.println(channel + ":" + message); } @Override public void onSubscribe(String channel, Int subscribedChannels) {system.out.println (" subscribe "+ channel +" channel "); System.out.println(channel + ":" + subscribedChannels); } @Override public void onUnsubscribe(String channel, Int subscribedChannels) {system.out.println (" unsubscribe "+ channel +" channel "); System.out.println(channel + ":" + subscribedChannels); }};Copy the code

Subscribe to the channel



Subscription channel, we only need to specify the subscribed to which channel, it is important to note here, we also need to specify, when making operation is related to the channel (subscribe, unsubscribe, receive news channel, etc.) we need how to deal with, it is used to the class above, of course, because this is a blocked thread, so use multithreading.

/** * Receive the message. * After this method is called, it is executed forever. When a corresponding message is published, it is received in jedisPubSub! * @param channels */ public static void subscribeMsg(String channels) { // Jedis jedis = getJedis(); Jedis Jedis = new Jedis ("127.0.0.1", 6379); new Thread(()->{ try { jedis.subscribe(jedisPubSub, channels); } catch (Exception e) { System.out.println(e.getMessage()); } finally { // jedis.close(); } }).start(); }Copy the code

test

To ensure that the post is posted after the subscription, sleep 100 milliseconds

public static void main(String[] args) {
        JedisUtil.subscribeMsg("test");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JedisUtil.publishMsg("test","hello");
        JedisUtil.publishMsg("test","hello aaa");
}
Copy the code