SpringBoot Redis 订阅发布

本文介绍如何在Spring Boot项目中配置Redis并实现消息监听功能。通过application.yml文件设置Redis连接参数,创建Redis消息监听器,并在启动类中注入监听容器及任务调度器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一  配置application.yml

spring:
  redis:
    jedis:
      pool:
        max-active: 10
        min-idle: 5
        max-idle: 10
        max-wait: 2000
    port: 6379
    host: 192.168.1.88
    timeout: 1000

 

二  实现监听

package com.example.demo.common;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @author Tyler
 * @date 2019/7/11
 */

@Component
public class RedisMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String body=new String(message.getBody());
        String topic=new String(bytes);
        System.out.println(body);
        System.out.println(topic);
    }
}

 

三  注入spring

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Repository;


@SpringBootApplication
@MapperScan(basePackages ="com.example.demo.common", annotationClass = Repository.class)
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


    @Autowired
    private RedisConnectionFactory connectionFactory;
    @Autowired
    private MessageListener redisMsgListener;

    private ThreadPoolTaskScheduler taskScheduler;

    @Bean
    public ThreadPoolTaskScheduler initTaskScheduler()
    {
        if(taskScheduler!=null)
        {
            return taskScheduler;
        }
        taskScheduler=new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(20);
        return taskScheduler;
    }

    @Bean
    public RedisMessageListenerContainer initRedisContainer()
    {
        RedisMessageListenerContainer container=new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setTaskExecutor(initTaskScheduler());
        Topic topic=new ChannelTopic("topic1");
        container.addMessageListener(redisMsgListener,topic);
        return container;
    }

}

 

四  结果:

1  接收exe消息

 

2 接收controller消息

controller:

@Controller
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/stringAndHash")
    @ResponseBody
    public Map<String,Object> testStringAndHash()
    {
        stringRedisTemplate.convertAndSend("topic1","hello");

        Map<String,Object> map=new HashMap<>();
        map.put("success",true);
        return map;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值