JedisConnectionFactory setHostName is deprecated
Asked Answered
E

4

37

This will be my first time connecting Spring to Redis. The documentation for jedis connection factory: http://www.baeldung.com/spring-data-redis-tutorial

Offers the following code:

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory jedisConFactory
            = new JedisConnectionFactory();

    jedisConFactory.setHostName("localhost");
    jedisConFactory.setPort(6379);
    return jedisConFactory;
}

Looks great, but my IDE is telling me that the setHostName and setPort methods have been deprecated (even though I'm using the versions from the tutorial).

I was wondering if anyone had a simple "get spring data connected to redis" example that uses the non-deprecated API calls?

Exsanguine answered 28/2, 2018 at 4:28 Comment(0)
Y
61

With Spring Data Redis 2.0, those methods have been deprecated. You now need to configure using RedisStandaloneConfiguration

Reference: https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.html#setHostName-java.lang.String-

Example:

JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    redisStandaloneConfiguration.setPassword(RedisPassword.of("yourRedisPasswordIfAny"));
    return new JedisConnectionFactory(redisStandaloneConfiguration);
}
Yecies answered 28/2, 2018 at 15:4 Comment(3)
So application.properties spring configuration vars like spring.redis.password and spring.redis.host won't work, you have to configure them via code?Absquatulate
@Absquatulate Some applications may have multiple redis connection at the same time hence have to configure it programmatically.Emilioemily
instead spring.redis.host you have to do spring.data.redis.hostGile
N
29
@Bean
    JedisConnectionFactory jedisConnectionFactory() {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("localhost");
        redisStandaloneConfiguration.setPort(6379);
        redisStandaloneConfiguration.setDatabase(0);
        redisStandaloneConfiguration.setPassword(RedisPassword.of("password"));

        JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));// 60s connection timeout

        JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(redisStandaloneConfiguration,
                jedisClientConfiguration.build());

        return jedisConFactory;
    }
Neptune answered 12/5, 2018 at 18:57 Comment(1)
Could you post the details about jedisClientConfiguration?Misogyny
L
0

In addition to the response by @Thang Le

You can configure your time out of read operations, adding this line:

jedisClientConfigurationBuilder.readTimeout(Duration.ofSeconds(1));

This is the value that you can obtain when use:

jedisConFactory.getTimeout()

Lightsome answered 25/9, 2018 at 23:4 Comment(0)
E
0

you should try this:: Add the given dependency in the pom.xml and go with the rest template configuration.

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
----------------------------------------
@Configuration
@EnableCaching
public class RedisConfig {

 @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}
Exhibitioner answered 3/5 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.