1. 程式人生 > >第 21 講 在Springboot使用Redis實現訊息佇列

第 21 講 在Springboot使用Redis實現訊息佇列

第二十一講 在Springboot使用Redis實現訊息佇列

1.引入依賴:pom.xml

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

2. 建立訊息接收者:Receiver

注意處理訊息的方法名:handleMessage

package com.springboot.redismq.config;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import java.util.concurrent.CountDownLatch; /** * @Description: 訊息接收者 * @Author: zrblog * @CreateTime: 2018-10-17 22:31 * @Version:v1.0 */
public class Receiver { static final Logger logger = LoggerFactory.getLogger(Receiver.class); public CountDownLatch latch; @Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void handleMessage(String message) { logger.info("Received <"
+ message + ">"); latch.countDown(); } }

3. 配置:RedisMQConfig

在Spring data redis中,利用Redis傳送一條訊息和接收一條訊息,需要三樣東西:

  1. 連線工程: MessageListenerAdapter
  2. 訊息監聽容器: RedisMessageListenerContainer
  3. Redis Template: StringRedisTemplate
package com.springboot.redismq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.concurrent.CountDownLatch;

/**
 * @Description:
 * @Author: zrblog
 * @CreateTime: 2018-10-17 22:37
 * @Version:v1.0
 */
@Configuration
public class RedisMQConfig {

    @Bean
    Receiver receiver(CountDownLatch latch) {
        return  new Receiver(latch);
    }

    @Bean
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory factory) {
        return new StringRedisTemplate(factory);
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(listenerAdapter,new PatternTopic("redisTopic"));
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver);
    }
}

4.測試:Application

package com.springboot.redismq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.CountDownLatch;

@SpringBootApplication
public class Application {

	private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);


	public static void main(String[] args) throws InterruptedException {
		ApplicationContext ctx =  SpringApplication.run(Application.class, args);

		StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
		CountDownLatch latch = ctx.getBean(CountDownLatch.class);

		LOGGER.info("Sending message...");

        //使用StringRedisTemplate傳送訊息
		template.convertAndSend("redisTopic", "Hello from Redis!");

		latch.await();

		System.exit(0);
	}
}

a