In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Preparation stage
To install redis, you can refer to my other article
Java 1.8
Maven 3.0
Idea
Environmental dependence
To create a new springboot project, you can add a request to understand the springcloud architecture: three five three six two four seven two five nine. In its pom file, add spring-boot-starter-data-redis dependencies:
Org.springframework.boot spring-boot-starter-data-redis
Create a message receiver
The REcevier class, which is a normal class, needs to be injected into springboot.
Public class Receiver {private static final Logger LOGGER = LoggerFactory.getLogger (Receiver.class); private CountDownLatch latch; @ Autowired public Receiver (CountDownLatch latch) {this.latch = latch;} public void receiveMessage (String message) {LOGGER.info ("Received"); latch.countDown ();}}
Inject message recipient
@ Bean Receiver receiver (CountDownLatch latch) {return new Receiver (latch);} @ Bean CountDownLatch latch () {return new CountDownLatch (1);} @ Bean StringRedisTemplate template (RedisConnectionFactory connectionFactory) {return new StringRedisTemplate (connectionFactory);}
Injection message listening container
In spring data redis, sending and receiving a message using redis requires three things:
A connection factory
A message listening container
Redis template
Steps 1 and 3 above have been completed, so you only need to inject the message listening container:
Bean RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (connectionFactory); container.addMessageListener (listenerAdapter, new PatternTopic ("chat")); return container;} @ Bean MessageListenerAdapter listenerAdapter (Receiver receiver) {return new MessageListenerAdapter (receiver, "receiveMessage");}
test
The main method at the springboot entry:
Public static void main (String [] args) throws Exception {ApplicationContext ctx = SpringApplication.run (SpringbootRedisApplication.class, args); StringRedisTemplate template = ctx.getBean (StringRedisTemplate.class); CountDownLatch latch = ctx.getBean (CountDownLatch.class); LOGGER.info ("Sending message..."); template.convertAndSend ("chat", "Hello from Redis!"); latch.await (); System.exit (0);}
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.