SpringBoot Redis发布订阅

举报
程序员-上善若水 发表于 2022/06/23 22:27:21 2022/06/23
【摘要】 SpringBoot Redis发布订阅 一、监听订阅 1. CacheConfig @Configuration @EnableCaching public class CacheConfig e...

SpringBoot Redis发布订阅

一、监听订阅

1. CacheConfig

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {


    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter CatAdapter, MessageListenerAdapter FishAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        container.addMessageListener(CatAdapter, new PatternTopic("One"));
        container.addMessageListener(FishAdapter, new PatternTopic("Two"));

        return container;
    }


    @Bean
    MessageListenerAdapter CatAdapter() {
        return new MessageListenerAdapter(new OneListener());
    }


    @Bean
    MessageListenerAdapter FishAdapter() {
        return new MessageListenerAdapter(new TwoListener());
    }


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

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);


        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

2. listener

@Component
public class OneListener implements MessageListener {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println("One监听" + message.toString());
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
public class TwoListener implements MessageListener {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println("Two监听" + message.toString());
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、发布订阅

stringRedisTemplate.convertAndSend(key,value);

  
 
  • 1

RedisUtil工具类

@Component
@Service
public class RedisUtil {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    public void putValue(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

    public void putValue(String key,String value,long time){
        stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
    }

    public void putValueTopic(String key,String value){
        stringRedisTemplate.convertAndSend(key,value);
    }

    public String getValue(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void putObject(String key, RedisInfoBean bean){
        redisTemplate.opsForValue().set(key,bean);
    }

    public void putObjectTopic(String key, RedisInfoBean bean){
        redisTemplate.convertAndSend(key,bean);
    }

    public void putObject(String key, RedisInfoBean bean,long time){
        redisTemplate.opsForValue().set(key,bean,time, TimeUnit.SECONDS);
    }

    public RedisInfoBean getObject(String key){
        try{
            RedisInfoBean bean = (RedisInfoBean) redisTemplate.opsForValue().get(key);
            return bean;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

补充:redis 添加不可重复写的key,value

Boolean lock = stringRedisTemplate.opsForValue().setIfAbsent(redisLockKey, lockId, expire, TimeUnit.SECONDS);

  
 
  • 1

文章来源: blog.csdn.net,作者:小毕超,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_43692950/article/details/107443700

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。