• 重试机制自己实现


    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>
  • 相关阅读:
    NO12 useradd-passwd-uname-hostname命令-上传rz下载sz-批量部署- Linux用户相关操作
    NO11 SSH故障排查思路和netstat命令
    NO10 查看Linux操作系统-版本-内核-Linux分区
    NO9 Linux快捷键整理及最常用命令
    NO8 find结合sed查找替换企业案例多方法精讲&命令总结!
    NO7 利用三剑客awk-grep-sed-head-tail等7种方法实践
    python 对比图片相似度
    MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出
    monkeyrunner对比屏幕局部图像.getSubImage()
    锤子便签的 monkeyrunner 测试脚本(转)
  • 原文地址:https://www.cnblogs.com/sunny-miss/p/9872362.html
Copyright © 2020-2023  润新知