In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 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.
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.