Commit fc064c9c0a7cc511b847542e380a9b7cc3b1ec9f
1 parent
f189c5b3
优化分布式事务异常抛出提示信息显示
Signed-off-by: TanYibin <5491541@qq.com>
Showing
2 changed files
with
24 additions
and
24 deletions
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,12 +3,13 @@ package org.jeecg.modules.wms.framework.controller; | ||
3 | import java.util.concurrent.TimeUnit; | 3 | import java.util.concurrent.TimeUnit; |
4 | 4 | ||
5 | import javax.annotation.Nonnull; | 5 | import javax.annotation.Nonnull; |
6 | -import javax.annotation.Resource; | ||
7 | 6 | ||
8 | import org.jeecg.common.api.vo.Result; | 7 | import org.jeecg.common.api.vo.Result; |
9 | import org.jeecg.utils.support.RedissonDistributedLocker; | 8 | import org.jeecg.utils.support.RedissonDistributedLocker; |
9 | +import org.springframework.beans.factory.annotation.Autowired; | ||
10 | 10 | ||
11 | import cn.hutool.core.date.SystemClock; | 11 | import cn.hutool.core.date.SystemClock; |
12 | +import cn.hutool.core.exceptions.ExceptionUtil; | ||
12 | import cn.hutool.core.util.StrUtil; | 13 | import cn.hutool.core.util.StrUtil; |
13 | import lombok.extern.slf4j.Slf4j; | 14 | import lombok.extern.slf4j.Slf4j; |
14 | 15 | ||
@@ -17,8 +18,7 @@ import lombok.extern.slf4j.Slf4j; | @@ -17,8 +18,7 @@ import lombok.extern.slf4j.Slf4j; | ||
17 | */ | 18 | */ |
18 | @Slf4j | 19 | @Slf4j |
19 | public class HuahengBaseController { | 20 | public class HuahengBaseController { |
20 | - | ||
21 | - @Resource | 21 | + @Autowired |
22 | private RedissonDistributedLocker redissonDistributedLocker; | 22 | private RedissonDistributedLocker redissonDistributedLocker; |
23 | 23 | ||
24 | public interface MultiProcessListener { | 24 | public interface MultiProcessListener { |
@@ -59,7 +59,6 @@ public class HuahengBaseController { | @@ -59,7 +59,6 @@ public class HuahengBaseController { | ||
59 | */ | 59 | */ |
60 | public Result<?> handleMultiProcess(@Nonnull String taskName, @Nonnull String lockKey, MultiProcessListener multiProcessListener) { | 60 | public Result<?> handleMultiProcess(@Nonnull String taskName, @Nonnull String lockKey, MultiProcessListener multiProcessListener) { |
61 | Result<?> result = null; | 61 | Result<?> result = null; |
62 | - | ||
63 | final long startTime = SystemClock.now(); | 62 | final long startTime = SystemClock.now(); |
64 | final String fullLockKey = "RedissonLock_" + taskName + "_" + lockKey; | 63 | final String fullLockKey = "RedissonLock_" + taskName + "_" + lockKey; |
65 | final boolean tryLock = redissonDistributedLocker.tryLock(fullLockKey, TimeUnit.SECONDS, WAIT_TIME, LEASE_TIME); | 64 | final boolean tryLock = redissonDistributedLocker.tryLock(fullLockKey, TimeUnit.SECONDS, WAIT_TIME, LEASE_TIME); |
@@ -73,9 +72,8 @@ public class HuahengBaseController { | @@ -73,9 +72,8 @@ public class HuahengBaseController { | ||
73 | log.info("[{}] 开始分布式事务 lockKey = {},获取锁耗时: {}ms", taskName, fullLockKey, endTime - startTime); | 72 | log.info("[{}] 开始分布式事务 lockKey = {},获取锁耗时: {}ms", taskName, fullLockKey, endTime - startTime); |
74 | result = multiProcessListener.doProcess(); | 73 | result = multiProcessListener.doProcess(); |
75 | } catch (Exception e) { | 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 | } finally { | 77 | } finally { |
80 | redissonDistributedLocker.unlock(fullLockKey); | 78 | redissonDistributedLocker.unlock(fullLockKey); |
81 | final long finishTime = SystemClock.now(); | 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,12 +11,14 @@ import javax.validation.constraints.NotNull; | ||
11 | import org.jeecg.common.exception.JeecgBootException; | 11 | import org.jeecg.common.exception.JeecgBootException; |
12 | import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; | 12 | import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; |
13 | import org.jeecg.utils.aspect.ApiLoggerAspect; | 13 | import org.jeecg.utils.aspect.ApiLoggerAspect; |
14 | +import org.jeecg.utils.constant.QuantityConstant; | ||
14 | import org.slf4j.Logger; | 15 | import org.slf4j.Logger; |
15 | import org.slf4j.LoggerFactory; | 16 | import org.slf4j.LoggerFactory; |
16 | 17 | ||
17 | import com.alibaba.fastjson.JSON; | 18 | import com.alibaba.fastjson.JSON; |
18 | import com.alibaba.fastjson.TypeReference; | 19 | import com.alibaba.fastjson.TypeReference; |
19 | 20 | ||
21 | +import cn.hutool.core.exceptions.ExceptionUtil; | ||
20 | import cn.hutool.core.util.StrUtil; | 22 | import cn.hutool.core.util.StrUtil; |
21 | import okhttp3.ConnectionPool; | 23 | import okhttp3.ConnectionPool; |
22 | import okhttp3.FormBody; | 24 | import okhttp3.FormBody; |
@@ -60,10 +62,12 @@ public class OkHttpUtils { | @@ -60,10 +62,12 @@ public class OkHttpUtils { | ||
60 | * JSON格式 | 62 | * JSON格式 |
61 | */ | 63 | */ |
62 | public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); | 64 | public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); |
65 | + | ||
63 | /** | 66 | /** |
64 | * OkHTTP线程池最大空闲线程数 | 67 | * OkHTTP线程池最大空闲线程数 |
65 | */ | 68 | */ |
66 | public final static int MAX_IDLE_CONNECTIONS = 100; | 69 | public final static int MAX_IDLE_CONNECTIONS = 100; |
70 | + | ||
67 | /** | 71 | /** |
68 | * OkHTTP线程池空闲线程存活时间(秒) | 72 | * OkHTTP线程池空闲线程存活时间(秒) |
69 | */ | 73 | */ |
@@ -85,8 +89,7 @@ public class OkHttpUtils { | @@ -85,8 +89,7 @@ public class OkHttpUtils { | ||
85 | * @throws Exception | 89 | * @throws Exception |
86 | */ | 90 | */ |
87 | public static String sendGet(String url, String param) { | 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,26 +112,24 @@ public class OkHttpUtils { | ||
109 | response = HTTP_CLIENT.newCall(request).execute(); | 112 | response = HTTP_CLIENT.newCall(request).execute(); |
110 | result = response.body().string(); | 113 | result = response.body().string(); |
111 | } catch (Exception e) { | 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 | ApiLoggerAspect.setApiLogException(apiLog, e); | 116 | ApiLoggerAspect.setApiLogException(apiLog, e); |
115 | - throw new RuntimeException(errorString, e); | 117 | + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, JeecgBootException.class); |
116 | } finally { | 118 | } finally { |
117 | ApiLoggerAspect.finishApiLog(apiLog, response, result); | 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 | log.info("执行GET请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); | 123 | log.info("执行GET请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); |
121 | } else { | 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 | return result; | 128 | return result; |
127 | } | 129 | } |
128 | 130 | ||
129 | public static String sendPostByJsonStr(String url, String jsonString) { | 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,18 +152,18 @@ public class OkHttpUtils { | ||
151 | response = HTTP_CLIENT.newCall(request).execute(); | 152 | response = HTTP_CLIENT.newCall(request).execute(); |
152 | result = response.body().string(); | 153 | result = response.body().string(); |
153 | } catch (Exception e) { | 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 | ApiLoggerAspect.setApiLogException(apiLog, e); | 156 | ApiLoggerAspect.setApiLogException(apiLog, e); |
156 | - throw new RuntimeException(errorString); | 157 | + throw ExceptionUtil.convertFromOrSuppressedThrowable(e, JeecgBootException.class); |
157 | } finally { | 158 | } finally { |
158 | ApiLoggerAspect.finishApiLog(apiLog, response, result); | 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 | log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), jsonString, result); | 163 | log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), jsonString, result); |
162 | } else { | 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 | return result; | 168 | return result; |
168 | } | 169 | } |
@@ -199,6 +200,7 @@ public class OkHttpUtils { | @@ -199,6 +200,7 @@ public class OkHttpUtils { | ||
199 | } | 200 | } |
200 | 201 | ||
201 | public static class OkhttpInterceptor implements Interceptor { | 202 | public static class OkhttpInterceptor implements Interceptor { |
203 | + | ||
202 | // 最大重试次数 | 204 | // 最大重试次数 |
203 | private int maxRentry; | 205 | private int maxRentry; |
204 | 206 |