Jedis Redis message publishing and subscription

 

 

Publish and subscribe (pub/sub) is a message communication pattern whose main purpose is to decouple the coupling between message publishers and message subscribers, which is similar to the observer pattern in the design pattern. pub/sub not only solves the direct code-level coupling between publishers and subscribers, but also solves the coupling between the two in physical deployment. As a pub/sub server, Redis functions as message routing between subscribers and publishers. Subscribers can subscribe to the redis server for message types they are interested in through the subscribe and psubscribe commands. Redis calls the message type a channel. When a publisher sends a specific type of message to the redis server through the publish command. All clients subscribed to this message type will receive this message. The message passing here is many-to-many. A client can subscribe to multiple channels and send messages to multiple channels .

 

==================================================

 ==================================================================

 

Since redis has built-in publish/subscribe function, it can be used as a message mechanism.

 

normal subscription

void redis.clients.jedis.Jedis.subscribe(JedisPubSub jedisPubSub, String... channels)

 

Pattern subscription

void redis.clients.jedis.Jedis.psubscribe(JedisPubSub jedisPubSub, String... patterns)

 

Both ordinary subscriptions and mode subscriptions can subscribe to one or more channels.

The difference between the two is that schema subscription supports * as a matcher. For example it* matches all channels starting with it ( it.news , it.blog , it.tweets , etc.). news.* matches all channels starting with news. ( news.it , news.global.today , etc.), and so on.

 

Wildcard description:

* 0 to any number of characters

? 1 character

 

Test code:

Two Clients, respectively subscribe to some channels.

a Publish. Responsible for publishing messages to the specified channel.

 

Publish

	public static void main(String[] args) {
		Jedis jedis = new Jedis("10.110.20.152",6379);
		jedis.publish("china.beijing", "Beijing, China");
		jedis.publish("china.jinan", "China Jinan");
		jedis.publish("it.software", "软件");
		jedis.publish("test1", "测试1");
		jedis.publish("test11", "测试11");
		jedis.close();
	}

 

 

Client1

public static void main(String[] args) {
		
		Jedis jedis = new Jedis("10.110.20.152",6379);
        JedisPubSub jedisPubSub=new JedisPubSub() {
           
        	/**
        	 * Process messages from normal subscribed channels.
        	 */
        	@Override
            public void onMessage(String channel, String message) {
                System.out.println("TestClient1:channel:"+channel+",message:"+message);

            }
        };
        
        //After executing subscribe, this thread will block.
        //Subscribe to two channels: china.beijing and china.shanghai
        //When the subscribed channel has a message, the onMessage method of jedisPubSub will be executed.
        jedis.subscribe(jedisPubSub,"china.beijing","china.shanghai");
        
        System.out.println("This line of code cannot be executed");
	}

 

Output result:

TestClient1: channel: china.beijing, message: Beijing, China

 

Client2

	public static void main(String[] args) {
		
		Jedis jedis = new Jedis("10.110.20.152",6379);
        JedisPubSub jedisPubSub=new JedisPubSub() {
           
        	/**
        	 * Process messages from normal subscribed channels.
        	 */
        	@Override
            public void onMessage(String channel, String message) {
            	System.out.println("TestClient2:channel:"+channel+",message:"+message);

            }
        	/**
        	 * Get channel messages for pattern matching subscriptions.
        	 */
            @Override
            public void onPMessage(String pattern, String channel, String message) {  
            	System.out.println("TestClient2:pattern:"+pattern+",channel:"+channel+",message:"+message);
            }  
        };
     
        //After executing psubscribe, this thread will block.
        //Subscribe to three channel mode: all channels prefixed with china. + all channels prefixed with test and only one character after test + channel test11.
        //When the subscribed channel has a message, the onPMessage method of jedisPubSub will be executed.
        jedis.psubscribe(jedisPubSub,"china.*","test?","test11");
        
        System.out.println("This line of code cannot be executed");
	}

 

Output result:

TestClient2: pattern: china.*, channel: china.beijing, message: Beijing, China

TestClient2: pattern: china.*, channel: china.jinan, message: Jinan, China

TestClient2:pattern:test?,channel:test1,message:测试1

TestClient2:pattern:test11,channel:test11,message:测试11

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326280588&siteId=291194637