Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use redis to implement message queue in springboot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use redis to implement message queues in springboot". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement message queues with redis in springboot".

Preparation stage

Install redis

Java 1.8

Maven 3.0

Idea

Environmental dependence

Create a new springboot project, add the spring-boot-starter-data-redis dependency in its pom file:

Org.springframework.boot spring-boot-starter-data-redis creates 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);} inject 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);}

First send a message using redisTemplate. After receiving it, the receiver prints it out. Start the springboot program and print on the console:

2017-04-2017: 25 com.forezp.SpringbootRedisApplication 15.536 INFO 39148-- [main] com.forezp.SpringbootRedisApplication: Sending message...

2017-04-2017: 25 com.forezp.message.Receiver 15.544 INFO 39148-- [container-2] com.forezp.message.Receiver: "Received

The test passed and the receiver did receive a message from the sender.

At this point, I believe you have a deeper understanding of "how to use redis to implement message queuing in springboot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report