SpringBoot uses redis to integrate message queue, springbootredis

Source: Internet
Author: User

SpringBoot uses redis to integrate message queue, springbootredis
I. pom file dependency

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId> 
</dependency>

 

Ii. Create a Message Receiver
Variables, methods, and constructors are marked to complete automatic assembly. Use @ Autowired to remove the set and get methods. @ Autowired public Referer (CountDownLatch latch) {this. latch = latch;} public void receiveMessage (String message) {LOGGER.info ("received message: <" + message + ">"); latch. countDown ();}}

 

After the above basic conditions are met, the following are the three elements of implementation:
  • A connection Factory
  • A message listening container
  • Redis template
3. Inject message recipients in application. java
@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); }

Iv. Inject a message listening container
// Necessary redis Message Queue connection factory @ BeanReceiver receiver (CountDownLatch latch) {return new Receiver (latch);} // necessary redis Message Queue connection factory @ BeanCountDownLatch latch () {return new CountDownLatch (1);} // redis template @ BeanStringRedisTemplate template (RedisConnectionFactory connectionFactory) {return new StringRedisTemplate (connectionFactory );} // inject the message listener container @ BeanRedisMessageListenerContainer container (RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new redismessagelistenereneriner (); container. setConnectionFactory (connectionFactory); container. addMessageListener (listenerAdapter, new PatternTopic ("msg"); return container;} // inject the message listener container @ BeanMessageListenerAdapter listenerAdapter (Receiver er receiver ER) {return new messagelistadapter ener( receiver er, "receiveMessage ");}
V. Unit Testing
Import java. util. concurrent. countDownLatch; import org. junit. test; import org. junit. runner. runWith; import org. slf4j. logger; import org. slf4j. loggerFactory; import org. springframework. beans. factory. annotation. autowired; import org. springframework. boot. test. context. springBootTest; import org. springframework. context. applicationContext; import org. springframework. data. redis. core. stringRedisTemplate; import org. Springframework. test. context. junit4.SpringRunner; import com. application; @ RunWith (SpringRunner. class) @ SpringBootTest (classes = Application. class) public class MsgQueueTest {@ Autowiredprotected ApplicationContext ctx; private static final Logger logger = LoggerFactory. getLogger (MsgQueueTest. class); @ Testpublic void SendMsg () {StringRedisTemplate template = ctx. getBean (StringRedisTemplate. class); Coun TDownLatch latch = ctx. getBean (CountDownLatch. class); logger.info ("I want to send a message... "); template. convertAndSend ("msg", "Welcome to the redis message queue! "); Try {// send a message connection waiting for logger.info (" The message is being sent... "); latch. await ();} catch (InterruptedException e) {logger.info ("message sending failed... ");}}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.