Kaynağa Gözat

异步调用正圆接口

wuweihao 3 yıl önce
ebeveyn
işleme
aa0cb2896a

+ 19 - 2
gis_cms/src/main/java/com/gis/cms/service/impl/HotelServiceImpl.java

@@ -16,7 +16,9 @@ import com.gis.cms.entity.vo.HotelVo;
 import com.gis.cms.mapper.HotelMapper;
 import com.gis.cms.service.HotelService;
 import com.gis.cms.zhengyuan.ZhengYuanService;
+import com.gis.common.base.entity.po.LogEntity;
 import com.gis.common.base.exception.BaseRuntimeException;
+import com.gis.common.base.service.LogService;
 import com.gis.common.base.service.impl.IBaseService;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.ErrorEnum;
@@ -25,6 +27,7 @@ import com.gis.common.util.DateUtils;
 import com.gis.common.util.ExcelUtils;
 import com.gis.common.util.Result;
 import lombok.extern.slf4j.Slf4j;
+import org.slf4j.MDC;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -32,6 +35,7 @@ import org.springframework.stereotype.Service;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.concurrent.ExecutorService;
 
 
 /**
@@ -54,6 +58,18 @@ public class HotelServiceImpl extends ServiceImpl<HotelMapper, HotelEntity> impl
     @Autowired
     ConfigConstant configConstant;
 
+    @Autowired
+    LogService logService;
+
+    @Autowired
+    IBaseService baseService;
+
+
+
+
+    // 链路id
+    static String traceId;
+
 
 
     @Override
@@ -156,8 +172,9 @@ public class HotelServiceImpl extends ServiceImpl<HotelMapper, HotelEntity> impl
         BaseRuntimeException.isNull(entity, ErrorEnum.FAILURE_SYS_2001);
         BeanUtils.copyProperties(param, entity);
         this.updateById(entity);
-        // todo 2022-5-16 主动更新合作商数据
-        zhengYuanService.updateHotel(entity);
+
+        // todo 2022-5-27 主动异步更新合作商数据
+        zhengYuanService.updateHotel(entity, logService, baseService.getUserId());
         return Result.success();
     }
 

+ 36 - 8
gis_cms/src/main/java/com/gis/cms/zhengyuan/ZhengYuanService.java

@@ -2,13 +2,15 @@ package com.gis.cms.zhengyuan;
 
 import com.alibaba.fastjson.JSONObject;
 import com.gis.cms.entity.po.HotelEntity;
-import com.gis.common.base.entity.dto.PageDto;
+import com.gis.common.base.entity.po.LogEntity;
+import com.gis.common.base.service.LogService;
 import com.gis.common.util.RedisUtil;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.ibatis.annotations.Select;
+import org.slf4j.MDC;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -23,12 +25,24 @@ public class ZhengYuanService {
     @Autowired
     RedisUtil redisUtil;
 
+    @Autowired
+    ExecutorService executorService;
+
+    // 链路id
+    static String traceId;
+
+
     private String getToken(){
         if (redisUtil.hasKey(tokenKey)){
             return redisUtil.getCacheObject(tokenKey);
         }
-
-        JSONObject res = ZhengYuanUtil.getCertification();
+        log.info("请求走token");
+        JSONObject res = null;
+        try {
+            res = ZhengYuanUtil.getCertification();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
         String accessToken = res.getString("accessToken");
         Integer availableTime = res.getInteger("tokenAvailableTime");
 
@@ -37,10 +51,21 @@ public class ZhengYuanService {
 
     }
 
-    /**更新酒店信息*/
-    public void updateHotel(HotelEntity param){
-        ZhengYuanUtil.saveHotel(getToken(), param);
-        log.info("正圆酒店信息更新完成");
+
+
+    /**异步更新酒店信息*/
+    public void updateHotel(HotelEntity param, LogService logService, Long userId){
+        traceId = MDC.get("TRACE_ID");
+        executorService.execute(() -> {
+            MDC.put("TRACE_ID", traceId);
+            try {
+                ZhengYuanUtil.saveHotel(getToken(), param);
+                log.info("正圆酒店信息更新完成");
+            } catch (Exception e) {
+                logService.save(new LogEntity(userId, "更新正圆接口异常", e.getMessage(), null));
+                e.printStackTrace();
+            }
+        });
 
         // 查看酒店列表
 //        PageDto dto = new PageDto();
@@ -49,4 +74,7 @@ public class ZhengYuanService {
 //        ZhengYuanUtil.listHotel(getToken(), dto);
 
     }
+
+
+
 }

+ 29 - 10
gis_cms/src/main/java/com/gis/cms/zhengyuan/ZhengYuanUtil.java

@@ -7,16 +7,18 @@ import com.alibaba.fastjson.JSONObject;
 import com.gis.cms.entity.dto.HotelDto;
 import com.gis.cms.entity.po.HotelEntity;
 import com.gis.common.base.entity.dto.PageDto;
+import com.gis.common.base.entity.po.LogEntity;
 import com.gis.common.base.exception.BaseRuntimeException;
+import com.gis.common.base.service.LogService;
+import com.gis.common.base.service.impl.IBaseService;
 import com.gis.common.util.HttpUtils;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.util.EntityUtils;
-import org.apache.shiro.crypto.hash.Hash;
+import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.HashMap;
 
+
 /**
  * Created by owen on 2022/5/16 0016 17:13
  * 正圆相关接口
@@ -24,6 +26,7 @@ import java.util.HashMap;
  * https://www.showdoc.com.cn/1946640511630445/8848832361067591
  * 密码:password123123
  */
+@Slf4j
 public class ZhengYuanUtil {
 
     static final String clientId = "868fb7e85eee499db3941195e350fccc";
@@ -44,11 +47,17 @@ public class ZhengYuanUtil {
     static final String api_listHotel = "/api/sanitationEpidemicHotel/third/listHotel";
 
 
+//    @Autowired
+//    LogService logService;
+//
+//    @Autowired
+//    IBaseService baseService;
+
     /**
      * 获取授权 token
      * @return
      */
-    public static JSONObject getCertification(){
+    public static JSONObject getCertification() throws Exception {
         String api = api_domain + api_getCertification;
         long time = System.currentTimeMillis();
         String sign = clientId+secureKey+time;
@@ -68,7 +77,7 @@ public class ZhengYuanUtil {
      * 新增/修改酒店信息
      * @return
      */
-    public static JSONObject saveHotel(String token, HotelEntity param){
+    public static JSONObject saveHotel(String token, HotelEntity param) throws Exception {
         String api = api_domain + api_saveHotel;
 
         HashMap<String, String> header = new HashMap<>();
@@ -98,7 +107,7 @@ public class ZhengYuanUtil {
      * @param param
      * @return
      */
-    public static JSONObject listHotel(String token, PageDto param){
+    public static JSONObject listHotel(String token, PageDto param) throws Exception {
         HashMap<String, String> header = new HashMap<>();
         header.put("Authorization", "Bearer " + token);
 
@@ -116,16 +125,26 @@ public class ZhengYuanUtil {
      * @param str
      * @return
      */
-    private static JSONObject parseResData(String str){
+    private static JSONObject parseResData(String str) throws Exception{
         ResData resData = JSONObject.parseObject(str, ResData.class);
-        BaseRuntimeException.isTrue(resData.getCode()!=200, null, "正圆接口响应失败");
+        if (resData.getCode()!=200){
+            String errorMsg = "正圆返回值异常: " + resData.getMessage();
+            log.error(errorMsg);
+           throw new Exception(errorMsg);
+        }
         JSONObject parse = JSON.parseObject(resData.getData());
         return parse;
     }
 
     @Test
     public void test1(){
-        getCertification();
+        try {
+            getCertification();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 
+
+
 }

+ 0 - 1
gis_common/src/main/java/com/gis/common/base/entity/po/LogEntity.java

@@ -14,7 +14,6 @@ import java.io.Serializable;
 @NoArgsConstructor                 //无参构造
 @AllArgsConstructor                //有参构造
 @Data
-//@Table(name = "tb_log")
 @TableName("tb_log")
 public class LogEntity extends BaseEntity implements Serializable {
 

+ 21 - 0
gis_common/src/main/java/com/gis/common/config/WebMvcConfig.java

@@ -1,5 +1,6 @@
 package com.gis.common.config;
 
+import cn.hutool.core.thread.ExecutorBuilder;
 import com.alibaba.fastjson.serializer.SerializerFeature;
 import com.alibaba.fastjson.support.config.FastJsonConfig;
 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@@ -16,6 +17,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
 
 /**
  * Created by owen on 2020/2/18 0018 12:01
@@ -89,6 +92,24 @@ public class WebMvcConfig implements WebMvcConfigurer {
         registry.addInterceptor(apiInterceptor).addPathPatterns("/api/**");
     }
 
+    /**
+     * 初始线程池
+     * @return
+     */
+    @Bean
+    public ExecutorService initExecutorService(){
+        /**
+         * 初始5个线程
+         * 最大10个线程
+         * 有界等待队列,最大等待数是100
+         */
+        ExecutorService executor = ExecutorBuilder.create()
+                .setCorePoolSize(10)
+                .setMaxPoolSize(20)
+                .setWorkQueue(new LinkedBlockingQueue<>(100))
+                .build();
+        return executor;
+    }