Procházet zdrojové kódy

逸曜新增重试逻辑

wzy před 3 týdny
rodič
revize
eddeddf609

+ 62 - 51
mjava-yiyao/src/main/java/com/malk/yiyao/service/impl/YiyaoServiceImpl.java

@@ -1,6 +1,7 @@
 package com.malk.yiyao.service.impl;
 
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.IORuntimeException;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.malk.server.aliwork.YDConf;
@@ -18,6 +19,7 @@ import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.net.UnknownHostException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -1590,58 +1592,67 @@ public class YiyaoServiceImpl implements YiyaoService {
 
     @Override
     public void getCheckInData(JSONObject eventJson) {
-        String timeStamp = eventJson.getString("timeStamp");//签到时间戳
-
-        timeStamp = roundToNearestThousand(Long.parseLong(timeStamp)) + "";
-
-        String staffId = eventJson.getString("StaffId");//签到人userId
-
-        //获取签到数据
-        Map body = new HashMap();
-        body.put("cursor",0);
-        body.put("size",100);
-        body.put("start_time",timeStamp);
-        body.put("end_time",timeStamp);
-        body.put("userid_list",staffId);
-
-
-
-        //查询用户详情
-        DDR_New ddrNew1 = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/v2/user/get", null, ddClient.initTokenParams(), UtilMap.map("userid", staffId), DDR_New.class);
-
-        Map result1 = (Map) ddrNew1.getResult();
-
-        String userName = UtilMap.getString(result1, "name");
-
-        //查询用户考勤数据
-        DDR_New ddrNew = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/checkin/record/get", null, ddClient.initTokenParams(), body, DDR_New.class);
-
-        Map result = (Map) ddrNew.getResult();
-
-        List list = UtilMap.getList(result, "page_list");
-
-        if (!list.isEmpty()){
-            Map formData = new HashMap();
-
-            Map checkInData = (Map) list.get(0);
-
-            Long checkinTime = UtilMap.getLong(checkInData, "checkin_time");
-            String detailPlace = UtilMap.getString(checkInData, "detail_place");
-            String remark = UtilMap.getString(checkInData,"remark");
-
-            formData.put("employeeField_mbypt8ro",Arrays.asList(staffId));//签到人
-            formData.put("textField_mbyq1cji",userName);//签到人-文本
-            formData.put("dateField_mbrb3qfy",checkinTime);//签到时间
-            formData.put("textField_mbrb3qfx",detailPlace);//签到地点
-            formData.put("textareaField_mbypt8rp",remark);//备注
-            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
-            String formattedDate = sdf.format(Long.parseLong(timeStamp));
-            formData.put("textField_mbrbdyl4","签到人:" +userName + ",签到时间:" + formattedDate + ",签到地点:" + detailPlace);//标题
+        int retryCount = 0;
+        int maxRetries = 3;
+        while (retryCount <= maxRetries) {
+            try {
+                String timeStamp = eventJson.getString("timeStamp");//签到时间戳
+                timeStamp = roundToNearestThousand(Long.parseLong(timeStamp)) + "";
+                String staffId = eventJson.getString("StaffId");//签到人userId
+
+                //获取签到数据
+                Map body = new HashMap();
+                body.put("cursor",0);
+                body.put("size",100);
+                body.put("start_time",timeStamp);
+                body.put("end_time",timeStamp);
+                body.put("userid_list",staffId);
+
+                //查询用户详情
+                DDR_New ddrNew1 = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/v2/user/get", null, ddClient.initTokenParams(), UtilMap.map("userid", staffId), DDR_New.class);
+                Map result1 = (Map) ddrNew1.getResult();
+                String userName = UtilMap.getString(result1, "name");
+
+                //查询用户考勤数据
+                DDR_New ddrNew = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/checkin/record/get", null, ddClient.initTokenParams(), body, DDR_New.class);
+                Map result = (Map) ddrNew.getResult();
+                List list = UtilMap.getList(result, "page_list");
+
+                if (!list.isEmpty()){
+                    Map formData = new HashMap();
+                    Map checkInData = (Map) list.get(0);
+                    Long checkinTime = UtilMap.getLong(checkInData, "checkin_time");
+                    String detailPlace = UtilMap.getString(checkInData, "detail_place");
+                    String remark = UtilMap.getString(checkInData,"remark");
+                    formData.put("employeeField_mbypt8ro",Arrays.asList(staffId));//签到人
+                    formData.put("textField_mbyq1cji",userName);//签到人-文本
+                    formData.put("dateField_mbrb3qfy",checkinTime);//签到时间
+                    formData.put("textField_mbrb3qfx",detailPlace);//签到地点
+                    formData.put("textareaField_mbypt8rp",remark);//备注
+                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+                    String formattedDate = sdf.format(Long.parseLong(timeStamp));
+                    formData.put("textField_mbrbdyl4","签到人:" +userName + ",签到时间:" + formattedDate + ",签到地点:" + detailPlace);//标题
+                    ydClient.operateData(YDParam.builder()
+                            .formUuid("FORM-D4389CDAD1704F959E9D38CB64927AD8PKCH")
+                            .formDataJson(JSONObject.toJSONString(formData))
+                            .build(),YDConf.FORM_OPERATION.create);
+                }
 
-            ydClient.operateData(YDParam.builder()
-                    .formUuid("FORM-D4389CDAD1704F959E9D38CB64927AD8PKCH")
-                    .formDataJson(JSONObject.toJSONString(formData))
-                    .build(),YDConf.FORM_OPERATION.create);
+                break;
+            } catch (Exception e) {
+                if (retryCount < maxRetries){
+                    retryCount++;
+                    log.info("第{}次重试,异常信息:{}", retryCount, e.getMessage());
+                    try {
+                        Thread.sleep(1000 * retryCount); // 指数退避
+                    } catch (InterruptedException ie) {
+                        Thread.currentThread().interrupt();// 如果线程在等待期间被中断,恢复中断状态
+                        throw new RuntimeException("Request interrupted", ie);// 抛出运行时异常,终止重试
+                    }
+                    continue;
+                }
+                throw new RuntimeException("超出最大重试次数,异常信息:" + e.getMessage());
+            }
         }
     }
 

+ 17 - 0
mjava-yiyao/src/test/java/com/malk/yiyao/YiyaoTest.java

@@ -1,5 +1,6 @@
 package com.malk.yiyao;
 
+import com.alibaba.fastjson.JSONObject;
 import com.malk.yiyao.service.YiyaoService;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
@@ -66,4 +67,20 @@ public class YiyaoTest {
         yiyaoService.updateYiyao10();
     }
 
+    @Test
+    public void test11() {
+        JSONObject json = new JSONObject();
+        json.put("timeStamp", 1750836577925L);
+        json.put("eventId","80057b2e91694a71b8c2cf6bd0e441a8");
+        json.put("CorpId","dingf11f7d6ff834577b");
+        json.put("StaffId","4537351301674351");
+        json.put("EventType","check_in");
+        json.put("EventTime",1750836577925L);
+        json.put("BizId","65164358-9bc3-44ea-9331-19062bc4676c");
+        json.put("staffId","4537351301674351");
+        json.put("TimeStamp", 1750836577925L);
+
+        yiyaoService.getCheckInData(json);
+    }
+
 }