package com.answern.ceis.web.system.util;
/**
* Created by wangyong
*
* @Date: 2018/10/29.
* @Description:
*/
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.io.InterruptedIOException;
/**
* User: Administrator
* Date: 2017/9/19
* Description:
*/
@Slf4j
public class MyOkHttpRetryInterceptor implements Interceptor {
public int executionCount;//最大重试次数
private long retryInterval;//重试的间隔
MyOkHttpRetryInterceptor(Builder builder) {
this.executionCount = builder.executionCount;
this.retryInterval = builder.retryInterval;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = doRequest(chain, request);
int retryNum = 1;
while ((response == null || !response.isSuccessful()) && retryNum <= executionCount) {
log.info("第{}次重试", retryNum);
final long nextInterval = getRetryInterval();
try {
log.info("等待{}发起回调", nextInterval);
Thread.sleep(nextInterval);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new InterruptedIOException();
}
// retry the request
log.info("第{}次回调开始", retryNum);
response = doRequest(chain, request);
retryNum++;
}
return response;
}
private Response doRequest(Chain chain, Request request) {
Response response = null;
try {
response = chain.proceed(request);
} catch (Exception e) {
}
return response;
}
/**
* retry间隔时间
*/
public long getRetryInterval() {
return this.retryInterval;
}
public static final class Builder {
private int executionCount;
private long retryInterval;
public Builder() {
executionCount = 3;
retryInterval = 1000;
}
public MyOkHttpRetryInterceptor.Builder executionCount(int executionCount) {
this.executionCount = executionCount;
return this;
}
public MyOkHttpRetryInterceptor.Builder retryInterval(long retryInterval) {
this.retryInterval = retryInterval;
return this;
}
public MyOkHttpRetryInterceptor build() {
return new MyOkHttpRetryInterceptor(this);
}
}
}
package com.answern.ceis.web.system.util;
import okhttp3.*;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* Created by wangyong
*
* @Date: 2018/10/29.
* @Description:
*/
@Component("httpClientByW")
public class HttpClient {
public String okPost(String url, String json) throws IOException {
MyOkHttpRetryInterceptor myOkHttpRetryInterceptor = new MyOkHttpRetryInterceptor.Builder()
.executionCount(3)
.retryInterval(2000)
.build();
OkHttpClient okHttpClient= new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.addInterceptor(myOkHttpRetryInterceptor)
.connectionPool(new ConnectionPool())
.connectTimeout(20000, TimeUnit.MILLISECONDS)
.readTimeout(10000, TimeUnit.MILLISECONDS)
.build();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
}
package com.answern.ceis.web.system.controller;
import com.answern.ceis.base.bean.VO.Msg;
import com.answern.ceis.web.system.util.HttpClient;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* Created by wangyong
*
* @Date: 2018/10/29.
* @Description:
*/
@RestController("ok")
@Api("http测试")
public class OkHttpClientController {
private static final Logger logger = LoggerFactory.getLogger(EmailController.class);
@Autowired
HttpClient httpClient;
@PostMapping("okhttp")
@Async
public Msg sendEmail() throws Exception {
Msg msg = new Msg();
String url = "https://testopr.tairanbaoxian.com/api/product/thirdPartInterface/interfaceChannel";
String json = "";
try {
String response = httpClient.okPost(url,json);
logger.info("返回信息为{}",response);
msg.setCode(0);
msg.setMessage(response);
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
}
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>