1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring RestTemplate Config Timeout設定

Last updated at Posted at 2018-05-14

Spring RedisTemplate でタイムアウトを期待して2000msec 設定しているけど効かずに処理が滞留することがある
設定追加でクリアできるか確認中、だめなら自力でタイムアウト...

・設定済
setConnectionRequestTimeout:10000msec
setReadTimeout:2000msec

・追加?
setConnectionRequestTimeout
→ 時間が足らずに検証できず

・自力タイムアウトの方法
今回は Async(Spring @Async 使用のメソッド)だったので return を Future にして対応
RetryTemplate も使用して以下の感じで対応

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

※実際は InterruptedException, ExecutionException とかExceptionは適切に catch して処理

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

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?