Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持类似spring-retry的请求失败重试吗? #15

Closed
mostcool opened this issue Sep 2, 2020 · 4 comments
Closed

支持类似spring-retry的请求失败重试吗? #15

mostcool opened this issue Sep 2, 2020 · 4 comments
Labels
question Further information is requested

Comments

@mostcool
Copy link

mostcool commented Sep 2, 2020

支持类似spring-retry的请求失败重试吗?
如:请求timeout时,可自动重试几次。

@troyzhxu
Copy link
Owner

troyzhxu commented Sep 2, 2020

构建 HTTP 实例的时候添加一个拦截器就可以实现啦

@mostcool
Copy link
Author

mostcool commented Sep 2, 2020

构建 HTTP 实例的时候添加一个拦截器就可以实现啦

有示例吗?发来学习下!

@troyzhxu
Copy link
Owner

troyzhxu commented Sep 2, 2020

HTTP http = HTTP.builder()
    .config(builder -> {
        
        builder.connectTimeout(10, TimeUnit.MILLISECONDS);
        builder.readTimeout(10, TimeUnit.MILLISECONDS);
        builder.writeTimeout(10, TimeUnit.MILLISECONDS);
        
        builder.addInterceptor(chain -> {
            int retryTimes = 0;	
            while (true) {
                try {
                    return chain.proceed(chain.request());
                } catch (SocketTimeoutException e) {
                    if (retryTimes >= 3) {
                        throw e;
                    }
                    retryTimes++;
                    System.out.println("超时重试第" + retryTimes + "次!");
                }
            }
        });
        
    }).build();

HttpResult res = http.sync("https://www.baidu.com").get();

System.out.println(res.getBody().toString());

@troyzhxu
Copy link
Owner

troyzhxu commented Sep 2, 2020

image

@troyzhxu troyzhxu closed this as completed Sep 2, 2020
@troyzhxu troyzhxu added the question Further information is requested label Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants