3

I need some help regarding some issues I encounter when trying to connect to Redis using Spring Boot.

I am using the following RedisConfiguration:

@Component
@Configuration
public class SpringRedisConf extends CachingConfigurerSupport {
    
    @Value("${spring.redis.host}")
    private String redisHostName;      
    
    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setHostName("localhost");
        connectionFactory.setUsePool(true);
        connectionFactory.setPort(PortName);
        connectionFactory.getPoolConfig().setMaxTotal(10);
        connectionFactory.getPoolConfig().setMaxIdle(10);
        return connectionFactory;
    }
    
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        if (null == redisConnectionFactory) {
            LOG.error("Redis Template Service is not available");
            return null;
        }
    
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setEnableTransactionSupport(true);
        return template;
    }

        
    @Bean
    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        // Number of seconds before expiration. Defaults to unlimited (0)
        return cacheManager;
    }

And the following class where I am trying o create a unit tests to test my connection:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserApp.class)
public class RedisConnectionTest {

@Autowired
    private RedisTemplate<String, Object> redisTemplate;

@Test
    public void testRedisConnection() {      
         redisTemplate.opsForValue().set("mouse", "loves cheese");
         assertThat( redisTemplate.opsForValue().get( "mouse").equals("loves cheese"));

    }
}

I know there is a similar question here, but I do not have yet the required score to comment, I have tried their suggestions and I still have the same result. Here is my pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${redis_clients_version}</version>
</dependency>

And here is my properties file:

spring.data.redis.repositories.enabled=true
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=XXXX
spring.cache.redis.time-to-live=0ms
spring.cache.redis.cache-null-values=false

The StackTrace is this:

   redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:589)
            at redis.clients.jedis.Connection.connect(Connection.java:184)



        

Any idea what am I doing wrong? Thanks

Kemal Abdic
  • 25
  • 1
  • 7
  • 1
    Your port numbers are different between your autoconfiguration of spring i.e the properties, and yourself defined connection factory. Theres no point in specifying the properties if you are going to create your own connection factory. – Darren Forsythe Jul 18 '18 at 15:39
  • Now it is working, I did not see honestly that I put the number wrong when setting the port. Thanks a lot! Yes, I know they should be taken from the properties file, that is another issue, I cannot read the int value, I got either a NumberFormatException if I am trying to read it as a String and convert it to int after, or an error saying that it is String and I am trying to read an int. I have posted that question as well. many thanks for seeing my mistake –  Jul 18 '18 at 16:25
  • @DarrenForsythe would you like to respond to my question? I think it is unfair for me to answer it, I lost all day seriously, thanks a lot –  Jul 18 '18 at 17:00

1 Answers1

2

Your port numbers are misconfigured between properties and the self-configured connection factory.

I would recommend sticking with one or the other Spring Boot's RedisAutoconfiguration which pulls in JedisConnectionConfiguration will automatically create a ConnectionFactory for you based of the properties.

https://github.com/spring-projects/spring-boot/blob/8f4bf233b4895a4fade5aff41e0a309f90ba3193/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java

Driven from the spring.redis properties.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54