Commit fc064c9c0a7cc511b847542e380a9b7cc3b1ec9f

Authored by 谭毅彬
1 parent f189c5b3

优化分布式事务异常抛出提示信息显示

Signed-off-by: TanYibin <5491541@qq.com>
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;
3 3 import java.util.concurrent.TimeUnit;
4 4  
5 5 import javax.annotation.Nonnull;
6   -import javax.annotation.Resource;
7 6  
8 7 import org.jeecg.common.api.vo.Result;
9 8 import org.jeecg.utils.support.RedissonDistributedLocker;
  9 +import org.springframework.beans.factory.annotation.Autowired;
10 10  
11 11 import cn.hutool.core.date.SystemClock;
  12 +import cn.hutool.core.exceptions.ExceptionUtil;
12 13 import cn.hutool.core.util.StrUtil;
13 14 import lombok.extern.slf4j.Slf4j;
14 15  
... ... @@ -17,8 +18,7 @@ import lombok.extern.slf4j.Slf4j;
17 18 */
18 19 @Slf4j
19 20 public class HuahengBaseController {
20   -
21   - @Resource
  21 + @Autowired
22 22 private RedissonDistributedLocker redissonDistributedLocker;
23 23  
24 24 public interface MultiProcessListener {
... ... @@ -59,7 +59,6 @@ public class HuahengBaseController {
59 59 */
60 60 public Result<?> handleMultiProcess(@Nonnull String taskName, @Nonnull String lockKey, MultiProcessListener multiProcessListener) {
61 61 Result<?> result = null;
62   -
63 62 final long startTime = SystemClock.now();
64 63 final String fullLockKey = "RedissonLock_" + taskName + "_" + lockKey;
65 64 final boolean tryLock = redissonDistributedLocker.tryLock(fullLockKey, TimeUnit.SECONDS, WAIT_TIME, LEASE_TIME);
... ... @@ -73,9 +72,8 @@ public class HuahengBaseController {
73 72 log.info("[{}] 开始分布式事务 lockKey = {},获取锁耗时: {}ms", taskName, fullLockKey, endTime - startTime);
74 73 result = multiProcessListener.doProcess();
75 74 } catch (Exception e) {
76   - String errorMessage = StrUtil.format("[{}] 执行分布式事务失败 lockKey = {},errorMessage = {}", taskName, fullLockKey, e.getCause());
77   - log.error(errorMessage, e);
78   - throw (RuntimeException)e;
  75 + log.error("[{}] 执行分布式事务失败 lockKey = {},errorMessage = {}", taskName, fullLockKey, ExceptionUtil.getMessage(e), e);
  76 + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, RuntimeException.class);
79 77 } finally {
80 78 redissonDistributedLocker.unlock(fullLockKey);
81 79 final long finishTime = SystemClock.now();
... ...
huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java
... ... @@ -11,12 +11,14 @@ import javax.validation.constraints.NotNull;
11 11 import org.jeecg.common.exception.JeecgBootException;
12 12 import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog;
13 13 import org.jeecg.utils.aspect.ApiLoggerAspect;
  14 +import org.jeecg.utils.constant.QuantityConstant;
14 15 import org.slf4j.Logger;
15 16 import org.slf4j.LoggerFactory;
16 17  
17 18 import com.alibaba.fastjson.JSON;
18 19 import com.alibaba.fastjson.TypeReference;
19 20  
  21 +import cn.hutool.core.exceptions.ExceptionUtil;
20 22 import cn.hutool.core.util.StrUtil;
21 23 import okhttp3.ConnectionPool;
22 24 import okhttp3.FormBody;
... ... @@ -60,10 +62,12 @@ public class OkHttpUtils {
60 62 * JSON格式
61 63 */
62 64 public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
  65 +
63 66 /**
64 67 * OkHTTP线程池最大空闲线程数
65 68 */
66 69 public final static int MAX_IDLE_CONNECTIONS = 100;
  70 +
67 71 /**
68 72 * OkHTTP线程池空闲线程存活时间(秒)
69 73 */
... ... @@ -85,8 +89,7 @@ public class OkHttpUtils {
85 89 * @throws Exception
86 90 */
87 91 public static String sendGet(String url, String param) {
88   - Map<String, String> headers = new HashMap<>();
89   - return sendGet(url, headers, param);
  92 + return sendGet(url, new HashMap<String, String>(), param);
90 93 }
91 94  
92 95 /**
... ... @@ -109,26 +112,24 @@ public class OkHttpUtils {
109 112 response = HTTP_CLIENT.newCall(request).execute();
110 113 result = response.body().string();
111 114 } catch (Exception e) {
112   - String errorString =
113   - StrUtil.format("执行GET请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), param, e.getMessage());
  115 + log.error("执行GET请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), param, ExceptionUtil.getMessage(e), e);
114 116 ApiLoggerAspect.setApiLogException(apiLog, e);
115   - throw new RuntimeException(errorString, e);
  117 + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, JeecgBootException.class);
116 118 } finally {
117 119 ApiLoggerAspect.finishApiLog(apiLog, response, result);
118 120 }
119   - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功
  121 + // 调用成功
  122 + if (response.isSuccessful() && Objects.nonNull(response.body())) {
120 123 log.info("执行GET请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result);
121 124 } else {
122   - String errorString = StrUtil.format("执行GET请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url,
123   - JSON.toJSONString(headers), param, response.code(), response.message());
124   - throw new JeecgBootException(errorString);
  125 + throw new JeecgBootException(StrUtil.format("执行GET请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers),
  126 + param, response.code(), response.message()));
125 127 }
126 128 return result;
127 129 }
128 130  
129 131 public static String sendPostByJsonStr(String url, String jsonString) {
130   - Map<String, String> headers = new HashMap<>();
131   - return sendPostByJsonStr(url, headers, jsonString);
  132 + return sendPostByJsonStr(url, new HashMap<String, String>(), jsonString);
132 133 }
133 134  
134 135 /**
... ... @@ -151,18 +152,18 @@ public class OkHttpUtils {
151 152 response = HTTP_CLIENT.newCall(request).execute();
152 153 result = response.body().string();
153 154 } catch (Exception e) {
154   - String errorString = StrUtil.format("执行POST请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), jsonString, e.getMessage());
  155 + log.error("执行POST请求异常,url:{},header:{},param:{},errorMessage:{}", url, JSON.toJSONString(headers), jsonString, ExceptionUtil.getMessage(e), e);
155 156 ApiLoggerAspect.setApiLogException(apiLog, e);
156   - throw new RuntimeException(errorString);
  157 + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, JeecgBootException.class);
157 158 } finally {
158 159 ApiLoggerAspect.finishApiLog(apiLog, response, result);
159 160 }
160   - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功
  161 + // 调用成功
  162 + if (response.isSuccessful() && Objects.nonNull(response.body())) {
161 163 log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), jsonString, result);
162 164 } else {
163   - String errorString = StrUtil.format("执行POST请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url,
164   - JSON.toJSONString(headers), jsonString, response.code(), response.message());
165   - throw new RuntimeException(errorString);
  165 + throw new JeecgBootException(StrUtil.format("执行POST请求失败,url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers),
  166 + jsonString, response.code(), response.message()));
166 167 }
167 168 return result;
168 169 }
... ... @@ -199,6 +200,7 @@ public class OkHttpUtils {
199 200 }
200 201  
201 202 public static class OkhttpInterceptor implements Interceptor {
  203 +
202 204 // 最大重试次数
203 205 private int maxRentry;
204 206  
... ...