redis订阅发布

redis订阅发布

    • 1.项目结构
    • 2.pom.xml
    • 3.RedisConfig
    • 4.消息监听
    • 4.TestSendController
    • 5.MyApplication
    • 6.启动项目

1.项目结构

redis订阅发布_第1张图片

2.pom.xml

	
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    

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

3.RedisConfig

@Configuration
@EnableCaching
public class RedisConfig {

    /**
     *   redis消息监听容器
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        //订阅通道  绑定方法
        container.addMessageListener(listenerAdapter(new RedisChannelSub1()),new PatternTopic("lmh"));
        container.addMessageListener(listenerAdapter2222222(new RedisChannelSub1()),new PatternTopic("lmh"));
        container.addMessageListener(listenerAdapter(new RedisChannelSub2()),new PatternTopic("54h"));
        container.addMessageListener(listenerAdapter(new RedisChannelSub3()),new PatternTopic("54h"));
        return container;
    }

    /**
     *  一定要有@Bean注解
     *  绑定 方法和通道
     */
    @Bean
    @Scope("prototype")
    MessageListenerAdapter listenerAdapter(RedisMsg redisMsg){
        //这个方法 是给messageListenerAdapter 传入一个接收处理器,利用反射方法调用 "receiveMessage"
        //也有好几个重载方法 ,这边默认调用处理器的方法 叫handlerMessage 可以到源码里面看
        return new MessageListenerAdapter(redisMsg,"receiveMessage");//两个通道的调用方法都要为 "receiveMessage"
    }

    @Bean
    @Scope("prototype")
    MessageListenerAdapter listenerAdapter2222222(RedisMsg redisMsg){
        return new MessageListenerAdapter(redisMsg,"receiveMessage222");
    }
}

4.消息监听

RedisChannelSub1

public class RedisChannelSub1 implements RedisMsg {
    @Override
    public void receiveMessage(String message) {
        System.out.println("通道监听接收消息 111111"+"----"+message);
    }

    public void receiveMessage222(String message){
        System.out.println("通道监听接收消息 111111"+"----"+message+"==================================");
    }
}

RedisChannelSub2

public class RedisChannelSub2 implements RedisMsg {
    @Override
    public void receiveMessage(String message) {
        System.out.println("通道监听接收消息 2222"+"----"+message);
    }
}

RedisChannelSub3

public class RedisChannelSub3 implements RedisMsg {
    @Override
    public void receiveMessage(String message) {
        System.out.println("通道监听接收消息 333"+"----"+message);
    }
}

4.TestSendController

@EnableScheduling
@Component
public class TestSendController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    //向redis消息队列index发送消息
    @Scheduled(fixedRate = 2000)
    public void sendMessage(){

        redisTemplate.convertAndSend("lmh",String.valueOf(Math.random()));
        redisTemplate.convertAndSend("54h",String.valueOf(Math.random()));
    }
}

5.MyApplication

@SpringBootApplication
public class MyApplication {

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

6.启动项目

*分析:只需要在RedisConfig配置类里面,把通道名称和监听通道的类和方法绑定,就能监听到该通道的消息。redis通道发送的消息,所有监听该通道的方法,都能接收到消息
*
打印结果如下:
在这里插入图片描述

你可能感兴趣的:(java,redis,java,redis,队列)