In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
As a publish/subscribe server, Redis plays the function of message routing. Subscribers can subscribe to Redis server for the message type they are interested in through the subscribe and psubscribe commands, when the publisher sends a specific type of message to Redis server through the publish command. All client that subscribe to this message type will receive this message. The message delivery here is many-to-many. A client can subscribe to multiple channel or send messages to multiple channel.
The following figure shows the architecture of the Redis messaging mechanism.
Both publishers and subscribers are Redis clients, while Channel is the Redis server. The publisher sends a message to a channel, and subscribers to that channel can receive the message. This publishing and subscribing mechanism of Redis is similar to topic-based publishing and subscribing, and Channel is equivalent to topics.
The commands that Redis publishes and subscribes to messages are listed below.
Publish: send message: Redis uses the PUBLISH command to send a message, and the return value is the number of subscribers who received the message. Subscribe: subscribe to a channel: Redis uses the SUBSCRIBE command to subscribe to a channel. The returned values include the channels subscribed by the client, the number of channels currently subscribed, and the messages received. Subscribe indicates that a channel has been subscribed successfully. Psubscribe: pattern matching: the pattern matching function allows the client to subscribe to channels that conform to a certain pattern. Redis uses PSUBSCRIBE to subscribe to all channels that conform to a certain pattern, using "" to indicate the pattern, and "" can be replaced by any value.
Case 1: one message producer and two message consumers
Case 2: two message producers and one message consumer
Case 3: Java API of Redis message mechanism
Add dependencies:
Message listener class:
Import redis.clients.jedis.JedisPubSub;public class RedisMsgPubSubListener extends JedisPubSub {@ Override public void unsubscribe () {super.unsubscribe ();} @ Override public void unsubscribe (String... Channels) {super.unsubscribe (channels);} @ Override public void subscribe (String... Channels) {super.subscribe (channels);} @ Override public void psubscribe (String... Patterns) {super.psubscribe (patterns);} @ Override public void punsubscribe () {super.punsubscribe ();} @ Override public void punsubscribe (String...) Patterns) {super.punsubscribe (patterns);} @ Override public void onMessage (String channel, String message) {System.out.println ("channel:" + channel + "receives message:" + message); this.unsubscribe () } @ Override public void onPMessage (String pattern, String channel, String message) {} @ Override public void onSubscribe (String channel, int subscribedChannels) {System.out.println ("channel:" + channel + "is been subscribed:" + subscribedChannels) } @ Override public void onPUnsubscribe (String pattern, int subscribedChannels) {} @ Override public void onPSubscribe (String pattern, int subscribedChannels) {} @ Override public void onUnsubscribe (String channel, int subscribedChannels) {System.out.println ("channel:" + channel + "is been unsubscribed:" + subscribedChannels);}}
Test program:
Package demo.redis;import redis.clients.jedis.Jedis;public class TestMain {@ Test public void testSubscribe () throws Exception {Jedis jedis = new Jedis ("localhost"); RedisMsgPubSubListener listener = new RedisMsgPubSubListener (); jedis.subscribe (listener, "redisChatTest"); / / other code} @ Test public void testPublish () throws Exception {Jedis jedis = new Jedis ("localhost"); jedis.publish ("redisChatTest", "Hello World") Thread.sleep (5000); jedis.publish ("redisChatTest", "Hello Redis");}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.