springboot整合各种消息队列(一):redis消息队列

cover


本篇博客将介绍使用redis作为消息中间件和springboot的整合使用;

安装配置redis

请参考笔者另一篇博客,有详细介绍:https://jsbintask.cn/2019/01/24/middleware/redis-install/#more

整合springboot

新建项目

新建一个springboot项目,并且修改application.yml文件,pom如下:

spring:
  redis:
    host: youripaddress
    password: jsbintask

host和password修改成自己的服务器用户名密码。pom:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    cn.jsbintask
    springboot-redis-learning
    0.0.1-SNAPSHOT
    springboot-redis-learning
    Demo project for Spring Boot redis

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


消息消费者(接收者)

编写一个消息消费者类:

@Log
@Component
public class RedisMessageReceiver {
    @Autowired
    private CountDownLatch countDownLatch;

    public void receivedMsg(String msg) {
        log.info("received msg: " + msg);
        // 计数,减一
        countDownLatch.countDown();
    }
}

并且加入@Component注解,将其作为bean归spring管理,并且通过@Autowried注入了一个CountdownLatch类。

将消息消费者作为监听器监听 redis的消息:

@Configuration
public class RedisConfig {
    public static final String MSG_TOPIC = "chat";

    @Bean
    public CountDownLatch countDownLatch() {
        return new CountDownLatch(1);
    }

    /**
     * 消息消费者 适配器,其中 receivedMsg为定义的消费者的消费方法,必须保持一致
     */
    @Bean
    MessageListenerAdapter listenerAdapter(RedisMessageReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receivedMsg");
    }

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

    /**
     * 消息监听容器,将适配器加入, 注意此处的 topic
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic(MSG_TOPIC));

        return container;
    }
}

启动测试类,发送消息

@SpringBootApplication
public class SpringbootRedisLearningApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootRedisLearningApplication.class, args);

        //从 spring中取出已经有的bean
        CountDownLatch countDownLatch = applicationContext.getBean(CountDownLatch.class);
        StringRedisTemplate stringRedisTemplate = applicationContext.getBean(StringRedisTemplate.class);
        
        stringRedisTemplate.convertAndSend(RedisConfig.MSG_TOPIC, "hello from jsbintask.");

        // 一直等待消息被接收,没接收不退出
        countDownLatch.await();
    }
}

启动,查看控制台:收到消息并且打印:

/pass

这样redis作为消息队列就成功了。 源码地址: https://github.com/jsbintask22/springboot-redis-learning
本文原创地址:https://jsbintask.cn/2019/01/25/springboot/springboot-redis-jms/,未经允许,禁止转载。

你可能感兴趣的:(springboot整合各种消息队列(一):redis消息队列)