Spring Boot Part 14: Implementing message queues with redis in springboot

This article mainly describes how to implement message queue with reids in springboot.

Preparation Phase

  • To install redis, you can refer to my other article, which will take you to get started with Redis in 5 minutes.
  • java 1.8
  • maven 3.0
  • idea

environment dependent

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

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

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 <" + message + ">");
        latch.countDown();
    }
}

inject message receiver

@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 listener container

In spring data redis, using redis to send a message and receive a message requires three things:

  • a connection factory
  • A message listening container
  • Redis template

The above steps 1 and 3 have been completed, so just inject the message listener 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 use redisTemplate to send a message, and after the receiver receives it, print it out. Start the springboot program, and the console prints:

2017-04-20 17:25:15.536 INFO 39148 —- [ main] com.forezp.SpringbootRedisApplication : Sending message…
2017-04-20 17:25:15.544 INFO 39148 —- [ container-2] com.forezp.message.Receiver : 》Received

The test passes, the receiver does receive the sender's message.

Source code download:

https://github.com/forezp/SpringBootLearning

References

messaging-redis

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324539898&siteId=291194637