Johnnes Souza
Johnnes Souza

Reputation: 391

Is there a way to set a timeout in Java with Redis reactive?

I'm using ReactiveRedisConnection to configure a connection to a local redis container.

But in the future the application will be hosted on a webserver and the redis on a different server.

Is there any option to set a timeout for a request?

Upvotes: 1

Views: 3503

Answers (2)

Johnnes Souza
Johnnes Souza

Reputation: 391

After some research and tests, I found that the timeout must be set on the request query instead.

So on the config Class:

@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString
(ReactiveRedisConnectionFactory connectionFactory) {
    return new ReactiveRedisTemplate<>
              (connectionFactory, RedisSerializationContext.string());
}

and in the service:

@Autowired
private ReactiveRedisTemplate<String, Response> repository;
public Mono<String> execute(String value){
        return repository.opsForHash().entries("KEY_TO_SEARCH")
                .timeout(Duration.ofMillis(TIMEOUT))
                .collect(Collectors.toMap("CODE_HERE");

Edit: Thank for everyone who helped here.

Upvotes: 1

shazin
shazin

Reputation: 21883

Timeout can be configured on your Reactive Connection Implementation. If you are using Lettuce for Redis Connection, you can do the following.

@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
    return new LettuceConnectionFactory(new RedisStandaloneConfiguration(), LettuceClientConfiguration.builder().commandTimeout(Duration.ofSeconds(2)).build());
}

And then use the connectionFactory to create ReactiveRedisTemplate.

@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString
  (ReactiveRedisConnectionFactory connectionFactory) {
    return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
}

Upvotes: 0

Related Questions