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

org.springframework.boot.autoconfigure.data.redis.CustomRedisAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 2.0.2.0
Show newest version
/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.data.redis;

import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.CustomRedisProperties.CustomPool;
import org.springframework.boot.autoconfigure.data.redis.CustomRedisProperties.Slave;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.CustomJedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import lombok.NonNull;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Custom {@link RedisAutoConfiguration}
 */
@Configuration
@EnableConfigurationProperties(CustomRedisProperties.class)
public class CustomRedisAutoConfiguration extends RedisAutoConfiguration {
	
	/**
	 * Slave enabled property name
	 */
	public static final String SLAVE_ENABLED_PROPERTY_NAME = "spring.redis.slave.enabled";
	
	/**
	 * {@link CustomRedisPooledConnectionConfiguration#slaveRedisConnectionFactory()} bean name
	 */
	public static final String SLAVE_REDIS_CONNECTION_FACTORY_BEAN_NAME = "slaveRedisConnectionFactory";
	
	/**
	 * {@link CustomRedisConfiguration#slaveRedisTemplate(RedisConnectionFactory)} bean name
	 */
	public static final String SLAVE_REDIS_TEMPLATE_BEAN_NAME = "slaveRedisTemplate";
	
	/**
	 * Custom
	 * {@link org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.RedisPooledConnectionConfiguration}
	 */
	@Configuration
	protected static class CustomRedisPooledConnectionConfiguration extends RedisPooledConnectionConfiguration {
		
		/**
		 * {@link CustomRedisProperties}
		 */
		@Autowired
		private CustomRedisProperties properties;
		
		@Bean
		@Override
		public JedisConnectionFactory redisConnectionFactory() throws UnknownHostException {
			
			CustomJedisConnectionFactory factory = new CustomJedisConnectionFactory(super.redisConnectionFactory());
			
			this.applyConnectionTimeoutProperty(factory, this.properties.getConnectionTimeout());
			this.applySocketTimeoutProperty(factory, this.properties.getSocketTimeout());
			this.applyPoolProperties(factory, this.properties.getPool());
			
			return factory;
		}
		
		/**
		 * Slave {@link RedisConnectionFactory}
		 * 
		 * @return {@link RedisConnectionFactory}
		 */
		@Bean
		@ConditionalOnProperty(name = SLAVE_ENABLED_PROPERTY_NAME, havingValue = "true")
		public RedisConnectionFactory slaveRedisConnectionFactory() {
			
			Slave props = this.properties.getSlave();
			
			CustomJedisConnectionFactory factory = new CustomJedisConnectionFactory();
			factory.setHostName(props.getHost());
			factory.setPort(props.getPort());
			
			this.applyConnectionTimeoutProperty(factory, props.getConnectionTimeout());
			this.applySocketTimeoutProperty(factory, props.getSocketTimeout());
			this.applyPoolProperties(factory, props.getPool());
			
			return factory;
		}
		
		/**
		 * Apply connection timeout property
		 * 
		 * @param factory {@link CustomJedisConnectionFactory}
		 * @param timeout timeout
		 */
		protected void applyConnectionTimeoutProperty(@NonNull CustomJedisConnectionFactory factory, int timeout) {
			
			if (timeout > 0) {
				
				factory.setConnectionTimeout(timeout);
			}
		}
		
		/**
		 * Apply socket timeout property
		 * 
		 * @param factory {@link CustomJedisConnectionFactory}
		 * @param timeout timeout
		 */
		protected void applySocketTimeoutProperty(@NonNull CustomJedisConnectionFactory factory, int timeout) {
			
			if (timeout > 0) {
				
				factory.setSocketTimeout(timeout);
			}
		}
		
		/**
		 * Apply pool properties
		 * 
		 * @param factory {@link CustomJedisConnectionFactory}
		 * @param props {@link CustomPool}
		 */
		protected void applyPoolProperties(@NonNull CustomJedisConnectionFactory factory, @NonNull CustomPool props) {
			
			JedisPoolConfig config = factory.getPoolConfig();
			config.setMaxTotal(props.getMaxActive());
			config.setMaxIdle(props.getMaxIdle());
			config.setMinIdle(props.getMinIdle());
			config.setMaxWaitMillis(props.getMaxWait());
			config.setMinEvictableIdleTimeMillis(60 * 1000);
			config.setSoftMinEvictableIdleTimeMillis(60 * 1000);
			config.setNumTestsPerEvictionRun(-1);
			config.setTestOnCreate(props.isTestOnCreate());
			config.setTestOnBorrow(props.isTestOnBorrow());
			config.setTestOnReturn(props.isTestOnReturn());
			config.setTestWhileIdle(props.isTestWhileIdle());
			config.setTimeBetweenEvictionRunsMillis(30 * 1000);
			config.setBlockWhenExhausted(true);
		}
	}
	
	/**
	 * Custom {@link org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.RedisConfiguration}
	 */
	@Configuration
	protected static class CustomRedisConfiguration extends RedisConfiguration {
		
		@Bean
		@Override
		public RedisTemplate redisTemplate(RedisConnectionFactory factory) throws UnknownHostException {
			
			RedisTemplate template = super.redisTemplate(factory);
			template.setKeySerializer(template.getStringSerializer());
			
			return template;
		}
		
		/**
		 * Slave {@link RedisTemplate}
		 * 
		 * @param factory {@link RedisConnectionFactory}
		 * @return {@link RedisTemplate}
		 */
		@Bean
		@ConditionalOnProperty(name = SLAVE_ENABLED_PROPERTY_NAME, havingValue = "true")
		public RedisTemplate slaveRedisTemplate(
			@Qualifier(SLAVE_REDIS_CONNECTION_FACTORY_BEAN_NAME) RedisConnectionFactory factory) {
			
			RedisTemplate template = new RedisTemplate<>();
			template.setConnectionFactory(factory);
			template.setKeySerializer(template.getStringSerializer());
			
			return template;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy