简体   繁体   中英

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?

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());
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM