Core code:

There are three main steps:

  1. Access to theme
  2. Topics add subscribers
  3. Publish events by topic
RTopic<SomeObject> topic = redisson.getTopic("anyTopic"); topic.addListener(new MessageListener<SomeObject>() { @Override public void onMessage(String channel, SomeObject message) { //.. .}}); // in other thread or JVM RTopic<SomeObject> topic = redisson.getTopic("anyTopic"); long clientsReceivedMessage = topic.publish(new SomeObject());Copy the code

The specific process

Subscriber implementation

@Component public class ProjectMessageRedisListener implements MessageListener<ContractPassedEvent> { public static final String TOPIC = "ContractPassedEvent"; @Autowired RedissonClient redissonClient; @override public void onMessage(CharSequence channel, ContractPassedEvent MSG) {system.out.println (" received event: "+ MSG); @data public Class ContractPassedEvent implements Serializable {private static Final Long serialVersionUID = -1L; private String name; }Copy the code

Registered subscribers

@Configuration @EnableAsyncpublic class WebMvcConfigure implements WebMvcConfigurer { @Autowired RedissonClient redissonClient; @bean ("contractPassedTopic") public RTopic contractPassedTopic(){// Register to listen on RTopic = redissonClient.getTopic(ProjectMessageRedisListener.TOPIC); topic.addListener(ContractPassedEvent.class, projectMessageRedisListener); return topic; }Copy the code

Publish event

@RunWith(SpringRunner.class) @SpringBootTest(classes = WebSpringBootTestApplication.class) public class ProjectMessageRedisListenerTest{ @Autowired RTopic topic; @Test public void testRedisTopic(){ topic.publish(new ContractPassedEvent("A00-Nj-2021-01-289-002932")); }}Copy the code

Subscribe to multiple topics

// subscribe to all topics by `topic1.*` pattern RPatternTopic<Message> topic1 = redisson.getPatternTopic("topic1.*"); int listenerId = topic1.addListener(new PatternMessageListener<Message>() { @Override public void onMessage(String pattern, String channel, Message msg) { Assert.fail(); }});Copy the code

The principle of

Subscribe to:

Release:

Redis publish subscribe features

Topic pattern listeners automatically re-subscribe when reconnecting to the Redis server or when the Redis server recovers from a failure.

  1. The sender and receiver of a message are bound by a channel: a channel can be a definite string, or it can be based on pattern matching
  2. Clients can subscribe to as many channels as they want
  3. Messages sent by the sender cannot be persisted, so messages can be lost
  4. Because messages cannot be persisted, consumers cannot receive messages sent before they subscribe to a channel
  5. There is no ACK mechanism for sending and receiving messages between the sender and the client

The application scenarios of Redis publish subscribe feature

Redis’s publish-subscribe functionality is unreliable because there is no guarantee of message persistence or ACK. As a result, it has limited application scenarios. It is recommended to be used in scenarios where real-time and reliability requirements are not high. For example, message push and notification in the Intranet environment