package com.ruoyi.common.im.mq;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.FastJson2JsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.nio.charset.StandardCharsets;


@Configuration
public class RedisMQConfig {

    @Bean
    public RedisMQTemplate redisMQTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisMQTemplate redisMQTemplate = new RedisMQTemplate();
     try{
         redisMQTemplate.setConnectionFactory(redisConnectionFactory);

         // 设置值（value）的序列化采用FastJsonRedisSerializer
         redisMQTemplate.setValueSerializer(fastJsonRedisSerializer());
         redisMQTemplate.setHashValueSerializer(fastJsonRedisSerializer());

         // 设置键（key）的序列化采用StringRedisSerializer。
         redisMQTemplate.setKeySerializer(new StringRedisSerializer());
         redisMQTemplate.setHashKeySerializer(new StringRedisSerializer());

         redisMQTemplate.afterPropertiesSet();
     }catch (Exception e){
         System.out.println(e);
     }
        return redisMQTemplate;
    }

    @Bean
    public FastJson2JsonRedisSerializer fastJsonRedisSerializer() {
        return new FastJson2JsonRedisSerializer(Object.class);
    }
}


