In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
How to implement the redis publish and subscribe function? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends the message and the subscriber (sub) receives the message.
Redis clients can subscribe to any number of channels.
Redis provides publish and subscribe function, which can be used for message transmission. The publish and subscribe mechanism of Redis consists of three parts, publisher, subscriber and Channel.
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.
1. Introduce Jedis
Redis.clients jedis 2.9.0
2, the specific implementation code
Package com.hcmony.sword.redis; import org.apache.commons.lang3.StringUtils;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPubSub; / * Shenjue.java basic description *
* * @ author hcmony * @ since V1.0.0, 2019-05-06 20:07 * / public class RedisMQ {private static final String TOPIC= "TOPIC"; private final JedisPool jedisPool; public RedisMQ (JedisPool jedisPool) {this.jedisPool = jedisPool } / * publish message * @ param topic * @ param messge * / public void publish (String topic, String messge) {Jedis jedis = null; if (StringUtils.isBlank (topic)) {topic=TOPIC } try {jedis = jedisPool.getResource (); jedis.publish (topic,messge);} finally {if (null! = jedis) {jedis.close () } / * subscription message * @ param topic * @ param jedisPubSub * / public void subscribe (String topic,JedisPubSub jedisPubSub) {Jedis jedis = null; if (StringUtils.isBlank (topic)) {topic=TOPIC } try {jedis = jedisPool.getResource (); jedis.subscribe (jedisPubSub,topic);} finally {if (null! = jedis) {jedis.close () } public static void main (String [] args) {/ / default connection to local redis, / / loclhost:6379 JedisPool jedisPool = new JedisPool (); RedisMQ publish = new RedisMQ (jedisPool) New Thread (new Runnable () {@ Override public void run () {publish.subscribe ("PID", new MyjedisPubSub ();}}) .start (); for (int item0)
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.