Spring RestTemplate Config Timeout setting

I have set 2000msec in Spring RedisTemplate in anticipation of timeout, but it may not work and processing may stall. Checking if it can be cleared by adding settings, if not, time out by yourself. .. ..

·Configured setConnectionRequestTimeout:10000msec setReadTimeout:2000msec

·add to? setConnectionRequestTimeout → I couldn't verify because I didn't have enough time

・ How to time out by yourself This time it was Async (method using Spring @Async), so set return to Future Also use RetryTemplate to respond with the following feeling

RetryTemplate


RetryTemplate retryTemplate = new RetryTemplate();

Map<Class<? extends Throwable>, Boolean> exceptions = Maps.newHashMap();
exceptions.put(TimeoutException.class, true);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(10, exceptions));

FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(10);
retryTemplate.setBackOffPolicy(backOffPolicy);

Future<T>(getResult)


Future<T> future = async.execute();
T result = retryTemplate.execute(new RetryCallback<T, TimeoutException>() { 
    @Override
    public T doWithRetry(RetryContext context) throws TimeoutException {
        if (future.isDone()) {
            return future.get();
        }
        throw new TimeoutException();
    }
});

reference ・ API https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.html

Recommended Posts

Spring RestTemplate Config Timeout setting
Spring RedisTemplate Config Timeout setting
Spring + MyBatis connection setting method
Java Config with Spring MVC
Spring Boot External setting priority
[Rails] Session timeout setting in devise
Java HTTP Client API timeout setting
Spring Cloud Config Embedding the Config Server