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... ");}}}