All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.redis.RedissonConnectionConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.redis;

import java.lang.reflect.Field;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.redis.redisson.RedissonClientBuilder;
import org.zodiac.redis.redisson.RedissonConfig;
import org.zodiac.redis.redisson.RedissonInfo;

@SpringBootConfiguration
@ConditionalOnProperty(prefix = SystemPropertiesConstants.Zodiac.SPRING_REDIS_REDISSON_PREFIX)
@ConditionalOnClass(value = {org.redisson.api.RedissonClient.class, org.redisson.spring.data.connection.RedissonConnectionFactory.class})
class RedissonConnectionConfiguration {

    private final ApplicationContext applicationContext;

    public RedissonConnectionConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    // @Bean(destroyMethod = "shutdown")
    // @ConditionalOnMissingBean(value = {org.redisson.api.RedissonClient.class})
    // protected org.redisson.api.RedissonClient redissonClient(RedisConfigProperties redisConfigProperties) {
    // return org.redisson.Redisson.create(redisConfigProperties.getRedisson());
    // }

    @Bean
    @ConditionalOnMissingBean(value = org.redisson.api.RedissonClient.class)
    protected org.redisson.api.RedissonClient redissonClient(RedisCacheCloudProperties redisConfigProperties) {
        return createRedissonClient(redisConfigProperties);
    }

    @Bean
    @ConditionalOnMissingBean(value = {RedisConnectionFactory.class})
    protected org.redisson.spring.data.connection.RedissonConnectionFactory
        redisConnectionFactory(ObjectProvider redissonClientProvider,
            RedisCacheCloudProperties redisConfigProperties) {
        org.redisson.api.RedissonClient client = redissonClientProvider.getIfAvailable();
        return null != client ? new org.redisson.spring.data.connection.RedissonConnectionFactory(client) : createRedissonConnectionFactory(redisConfigProperties);
    }

    private org.redisson.spring.data.connection.RedissonConnectionFactory
        createRedissonConnectionFactory(RedisCacheCloudProperties redisConfigProperties) {
        RedisCacheCloudRedissonProperties redisRedissonConfigProperties = (RedisCacheCloudRedissonProperties)redisConfigProperties;
        // TODO 根据Redisson中单点、集群、哨兵、主备等不同配置判断特殊配置
        RedissonInfo redissonInfo = redisRedissonConfigProperties.getRedisson();
        RedissonConfig redissonConfig = redissonInfo.getConfig();
        Long appId = redissonInfo.getCacheCloudAppId();
        org.redisson.config.Config config = new org.redisson.config.Config();
        if (appId == null) {
            if (redissonConfig.getClusterServersConfig() != null) {
                setConfigPrivateFiled("clusterServersConfig", config, redissonConfig.getClusterServersConfig());
            }

            if (redissonConfig.getSentinelServersConfig() != null) {
                setConfigPrivateFiled("sentinelServersConfig", config, redissonConfig.getSentinelServersConfig());
            }

            if (redissonConfig.getSingleServerConfig() != null) {
                setConfigPrivateFiled("singleServerConfig", config, redissonConfig.getSingleServerConfig());
            }

            config.setNettyThreads(redissonConfig.getNettyThreads());
            config.setThreads(redissonConfig.getThreads());
        }
        return new org.redisson.spring.data.connection.RedissonConnectionFactory(config);
    }

    private org.redisson.api.RedissonClient createRedissonClient(RedisCacheCloudProperties redisConfigProperties) {
        RedisCacheCloudRedissonProperties redisRedissonConfigProperties = (RedisCacheCloudRedissonProperties)redisConfigProperties;
        // TODO 根据Redisson中单点、集群、哨兵、主备等不同配置判断特殊配置
        RedissonInfo redissonInfo = redisRedissonConfigProperties.getRedisson();
//        RedissonConfig redissonConfig = redissonInfo.getConfig();
//        Long appId = redissonInfo.getCacheCloudAppId();
//        org.redisson.config.Config config = new org.redisson.config.Config();
//        if (appId == null) {
//            if (redissonConfig.getClusterServersConfig() != null) {
//                setConfigPrivateFiled("clusterServersConfig", config, redissonConfig.getClusterServersConfig());
//            }
//
//            if (redissonConfig.getSentinelServersConfig() != null) {
//                setConfigPrivateFiled("sentinelServersConfig", config, redissonConfig.getSentinelServersConfig());
//            }
//
//            if (redissonConfig.getSingleServerConfig() != null) {
//                setConfigPrivateFiled("singleServerConfig", config, redissonConfig.getSingleServerConfig());
//            }
//
//            config.setNettyThreads(redissonConfig.getNettyThreads());
//            config.setThreads(redissonConfig.getThreads());
//        }
//        return org.redisson.Redisson.create(config);
        return new RedissonClientBuilder("default", redissonInfo, applicationContext).build();
    }

    private void setConfigPrivateFiled(String fieldName, org.redisson.config.Config config, Object obj) {
        try {
            Class cl = org.redisson.config.Config.class;
            Field field = cl.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(config, obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy