• 请求重试


    该代码就是普通的尝试五次请求,并没有对请求的时间间隔进行限制

    在逻辑代码里可以加入请求时间的间隔。

    package com.qdsg.ylt_vedio.common.untils;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.retry.RecoveryCallback;
    import org.springframework.retry.RetryCallback;
    import org.springframework.retry.RetryContext;
    import org.springframework.retry.policy.SimpleRetryPolicy;
    import org.springframework.retry.support.RetryTemplate;

    /**
    * @Description
    * @ClassName ReTryUtil
    * @Authetr CZQ
    * @Date 2018/9/20 8:38
    * @VERSION 1.0
    **/
    public class ReTryUtil {
    private static final Logger logger = LoggerFactory.getLogger(ReTryUtil.class);

    public static Object doIs(String uri, Object user, Object param) {

    // 重试机制
    RetryTemplate oRetryTemplate = new RetryTemplate();
    SimpleRetryPolicy oRetryPolicy = new SimpleRetryPolicy();
    oRetryPolicy.setMaxAttempts(5);// 重试5次
    oRetryTemplate.setRetryPolicy(oRetryPolicy);
    Object obj = null;
    try {
    // obj为doWithRetry的返回结果,可以为任意类型
    obj = oRetryTemplate.execute(new RetryCallback<Object, Exception>() {
    @Override
    public Object doWithRetry(RetryContext context) throws Exception {// 开始重试
    String redo = Redo(uri, user, param);
    return redo;//这里是我们处理的逻辑
    }
    }, new RecoveryCallback<Object>() {
    @Override
    public Object recover(RetryContext context) throws Exception { // 重试多次后都失败了
    return "系统异常,不能结束会议";
    }
    });
    } catch (Exception e) {
    logger.error("重试结束会议异常 {}", e.getMessage());
    }
    return obj;
    }

    private static String Redo(String uri, Object user, Object param) {
    String str = HttpClientUtil.httpPostRequest(uri, user, param);
    JSONObject jsonObject = JSON.parseObject(str);
    String code = jsonObject.get("code").toString();
    if (code.equals("200")) {
    return str;
    } else {
    logger.info("重试5次,仍然没有结束掉会议");
    throw new RuntimeException();
    }
    }
    }

  • 相关阅读:
    统计 (Standard IO)
    存储过程中的错误处理
    簡單SQL存儲過程實例
    SQLSERVER存储过程基本语法
    SQL Server游标的使用【转】
    实现业务系统中的用户权限管理--实现篇
    实现业务系统中的用户权限管理--设计篇
    C#.net 微信公众账号接口开发
    jquery select radio
    asp.net Repeater使用例子,包括分页
  • 原文地址:https://www.cnblogs.com/guagua-join-1/p/9680195.html
Copyright © 2020-2023  润新知