From fc064c9c0a7cc511b847542e380a9b7cc3b1ec9f Mon Sep 17 00:00:00 2001 From: TanYibin <5491541@qq.com> Date: Mon, 19 Jun 2023 09:32:47 +0800 Subject: [PATCH] 优化分布式事务异常抛出提示信息显示 --- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java | 12 +++++------- huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java | 36 +++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java index d34b506..c4198a1 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java @@ -3,12 +3,13 @@ package org.jeecg.modules.wms.framework.controller; import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; -import javax.annotation.Resource; import org.jeecg.common.api.vo.Result; import org.jeecg.utils.support.RedissonDistributedLocker; +import org.springframework.beans.factory.annotation.Autowired; import cn.hutool.core.date.SystemClock; +import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; @@ -17,8 +18,7 @@ import lombok.extern.slf4j.Slf4j; */ @Slf4j public class HuahengBaseController { - - @Resource + @Autowired private RedissonDistributedLocker redissonDistributedLocker; public interface MultiProcessListener { @@ -59,7 +59,6 @@ public class HuahengBaseController { */ public Result<?> handleMultiProcess(@Nonnull String taskName, @Nonnull String lockKey, MultiProcessListener multiProcessListener) { Result<?> result = null; - final long startTime = SystemClock.now(); final String fullLockKey = "RedissonLock_" + taskName + "_" + lockKey; final boolean tryLock = redissonDistributedLocker.tryLock(fullLockKey, TimeUnit.SECONDS, WAIT_TIME, LEASE_TIME); @@ -73,9 +72,8 @@ public class HuahengBaseController { log.info("[{}] 开始分布式事务 lockKey = {},获取锁耗时: {}ms", taskName, fullLockKey, endTime - startTime); result = multiProcessListener.doProcess(); } catch (Exception e) { - String errorMessage = StrUtil.format("[{}] 执行分布式事务失败 lockKey = {},errorMessage = {}", taskName, fullLockKey, e.getCause()); - log.error(errorMessage, e); - throw (RuntimeException)e; + log.error("[{}] 执行分布式事务失败 lockKey = {},errorMessage = {}", taskName, fullLockKey, ExceptionUtil.getMessage(e), e); + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, RuntimeException.class); } finally { redissonDistributedLocker.unlock(fullLockKey); final long finishTime = SystemClock.now(); diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java index 6cd76ca..c71e201 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java @@ -11,12 +11,14 @@ import javax.validation.constraints.NotNull; import org.jeecg.common.exception.JeecgBootException; import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; import org.jeecg.utils.aspect.ApiLoggerAspect; +import org.jeecg.utils.constant.QuantityConstant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; +import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.util.StrUtil; import okhttp3.ConnectionPool; import okhttp3.FormBody; @@ -60,10 +62,12 @@ public class OkHttpUtils { * JSON格式 */ public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); + /** * OkHTTP线程池最大空闲线程数 */ public final static int MAX_IDLE_CONNECTIONS = 100; + /** * OkHTTP线程池空闲线程存活时间(秒) */ @@ -85,8 +89,7 @@ public class OkHttpUtils { * @throws Exception */ public static String sendGet(String url, String param) { - Map<String, String> headers = new HashMap<>(); - return sendGet(url, headers, param); + return sendGet(url, new HashMap<String, String>(), param); } /** @@ -109,26 +112,24 @@ public class OkHttpUtils { response = HTTP_CLIENT.newCall(request).execute(); result = response.body().string(); } catch (Exception e) { - String errorString = - StrUtil.format("执行GET请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), param, e.getMessage()); + log.error("执行GET请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), param, ExceptionUtil.getMessage(e), e); ApiLoggerAspect.setApiLogException(apiLog, e); - throw new RuntimeException(errorString, e); + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, JeecgBootException.class); } finally { ApiLoggerAspect.finishApiLog(apiLog, response, result); } - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 + // 调用成功 + if (response.isSuccessful() && Objects.nonNull(response.body())) { log.info("执行GET请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); } else { - String errorString = StrUtil.format("执行GET请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, - JSON.toJSONString(headers), param, response.code(), response.message()); - throw new JeecgBootException(errorString); + throw new JeecgBootException(StrUtil.format("执行GET请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), + param, response.code(), response.message())); } return result; } public static String sendPostByJsonStr(String url, String jsonString) { - Map<String, String> headers = new HashMap<>(); - return sendPostByJsonStr(url, headers, jsonString); + return sendPostByJsonStr(url, new HashMap<String, String>(), jsonString); } /** @@ -151,18 +152,18 @@ public class OkHttpUtils { response = HTTP_CLIENT.newCall(request).execute(); result = response.body().string(); } catch (Exception e) { - String errorString = StrUtil.format("执行POST请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), jsonString, e.getMessage()); + log.error("执行POST请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), jsonString, ExceptionUtil.getMessage(e), e); ApiLoggerAspect.setApiLogException(apiLog, e); - throw new RuntimeException(errorString); + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, JeecgBootException.class); } finally { ApiLoggerAspect.finishApiLog(apiLog, response, result); } - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 + // 调用成功 + if (response.isSuccessful() && Objects.nonNull(response.body())) { log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), jsonString, result); } else { - String errorString = StrUtil.format("执行POST请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, - JSON.toJSONString(headers), jsonString, response.code(), response.message()); - throw new RuntimeException(errorString); + throw new JeecgBootException(StrUtil.format("执行POST请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), + jsonString, response.code(), response.message())); } return result; } @@ -199,6 +200,7 @@ public class OkHttpUtils { } public static class OkhttpInterceptor implements Interceptor { + // 最大重试次数 private int maxRentry; -- libgit2 0.22.2