SUBSCRIBE, UNSUBSCRIBE and PUBLISH implement the PUBLISH/SUBSCRIBE message paradigm, in which subscribers can subscribers a message from publishers without programming it. Rather, the published message goes into a channel. You don’t need to know if there are subscribers. Subscribers publish one or more channels that interest them, and only accept messages that interest them, whether or not the publisher exists. Decoupling of publishers and subscribers allows for greater scalability and more dynamic network topologies.

I also wrote an article about publishing subscriptions (message queues) in SpringBoot advanced Tutorial (22). If you’re interested, check it out. Publish and subscribe is based on Redis.

V Preparations

Before learning this chapter, it is recommended to read the following articles in order to better connect the full text. If you have mastered the following listed knowledge points, please skip:

  • Centos install Redis
  • SpringBoot(24) integrates Redis

V Command line operation publish subscription

In the example below, the left window in the figure is considered client 1 and the right window is considered client 2.

1.1 Client 1 Subscribe channel chatDemo

subscribe chatDemo

1.2 Client 2 sends two messages to chatDemo, and Client 1 receives the two messages in real time.

publish chatDemo "Hello World."

publish chatDemo "Hello Demo."

1.3 Unsubscribe channels in Client 1, or Ctrl+C to exit redis connection mode.

unsubscribe chatDemo

The above examples focus on subscribing to channels, publishing messages to specified channels, then pushing messages to subscribers, and unsubscribing.

V Operate publish and subscribe in project

2.1 Message listener classes

package com.demo.common; import org.springframework.stereotype.Component; /** * Created by toutou on 2019/2/23. */ @Component public class RedisReceiver { public void receiveMessage(String System.out.println(message); }}Copy the code

2.2 Redis Message subscription configuration class

package com.demo.Redis; import com.demo.common.RedisReceiver; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; /** * Created by toutou on 2019/1/20. */ @Configuration @EnableCaching public class RedisCacheConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();  container.setConnectionFactory(connectionFactory); / / you can add multiple messageListener, configuration of different switches container. AddMessageListener (listenerAdapter, new PatternTopic (channel: "test")); return container; } /** * message listener adapter, binding message handler, Call the business method of the message processor using reflection technology * @param receiver * @return */ @bean MessageListenerAdapter listenerAdapter(RedisReceiver receiver) { System.out.println(" message adapter 1"); return new MessageListenerAdapter(receiver, "receiveMessage"); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); }}Copy the code

2.3 Test Interface

package com.demo.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * Created by toutou on 2019/1/20. */ @RestController @Slf4j public class RedisController { @Autowired StringRedisTemplate template; @RequestMapping(value = "/syncmessage") public String SyncMessage(){ for(int i = 1; i <= 5; I++){try{// to simulate the message, sleep. Thread.sleep(2000); } Catch (InterruptedException ex){} template.convertAndSend("channel:test", string. format(" I am message {%d} number: %tT", i, new Date())); } return "5"; }}Copy the code

2.4 Project directory structure

2.5 Operating Effect

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \