Ver código fonte

fix: fix didn't update record problem

CompletableFuture must use join() method
linwenhua 3 anos atrás
pai
commit
fdd41c6111

+ 56 - 25
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/inform/CommonInformServiceImpl.java

@@ -15,6 +15,7 @@ import com.inspur.customer.service.inform.handler.EmailMessageHandler;
 import com.inspur.customer.service.inform.handler.NoteMessageHandler;
 import com.inspur.customer.service.inform.handler.WeChatAppletMessageHandler;
 import com.inspur.customer.service.inform.handler.WeChatMessageHandler;
+import com.inspur.customer.service.inform.object.InformResult;
 import com.inspur.inform.client.email.SmsbEmailService;
 import com.inspur.inform.client.sms.SmsbSmsService;
 import com.inspur.inform.client.wechat.IWeChatService;
@@ -38,6 +39,7 @@ import java.util.concurrent.CompletableFuture;
 
 /**
  * common inform service impl
+ *
  * @author linwenhua
  * @date 2022-06-29 15:30
  **/
@@ -81,7 +83,7 @@ public class CommonInformServiceImpl implements CommonInformService {
         TenantExceptionInformStrategyCO informStrategyCo = tenantExceptionInformStrategyService.getTenantInformStrategyCache(informCo.getTenant());
         ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
         switch (informCo.getTypeEnum().getLevel()) {
-            case HINT : {
+            case HINT: {
                 log.info("hint");
                 break;
             }
@@ -91,13 +93,13 @@ public class CommonInformServiceImpl implements CommonInformService {
                 users.addAll(keycloakService.getUsersInRole("ROLE_SUPER_ADMIN"));
                 break;
             }
-            case URGENT : {
+            case URGENT: {
                 log.info("urgent");
                 //todo replace get user method
                 users.addAll(keycloakService.getUsersInRole("ROLE_SUPER_ADMIN"));
                 break;
             }
-            default : {
+            default: {
                 log.info("not support");
                 throw new EventBaseException("类型错误", 500);
             }
@@ -113,6 +115,7 @@ public class CommonInformServiceImpl implements CommonInformService {
         List<KeycloakUserCO> users = keycloakService.getGroupSupervisor(informCo.getTenant());
         // get strategy
         TenantExceptionInformStrategyCO informStrategyCo = tenantExceptionInformStrategyService.getTenantInformStrategyCache(informCo.getTenant());
+        log.info("strategy: {}", informStrategyCo);
         ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
         // execute inform
         executeInformUserCmd(informCo, informStrategyCmd, users);
@@ -141,7 +144,14 @@ public class CommonInformServiceImpl implements CommonInformService {
         executeInformUserCmd(informCo, informStrategyCmd, users);
     }
 
+    /**
+     * execute inform action
+     * @param informCo inform object
+     * @param informStrategyCmd strategy
+     * @param users users
+     */
     private void executeInformUserCmd(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, List<KeycloakUserCO> users) {
+        // set addressees
         InformAddressees addressees = new InformAddressees(users.size());
         users.forEach(user -> {
             log.info("user: {}", user);
@@ -156,19 +166,25 @@ public class CommonInformServiceImpl implements CommonInformService {
             // send message
             InformResult informResult = sendMessage(informCo, informStrategyCmd, addressees);
             // update record
-            updateSendRecord(informResult, contentId, informStrategyCmd, addressees.getUserIds());
-        } catch (JsonProcessingException e) {
-            log.error("序列化出错: {}", e.getMessage(), e);
-            throw new CustomerBaseException("参数序列化出错", 500);
+            updateSendRecord(informResult, contentId, informStrategyCmd);
         } catch (IOException e) {
+            if (e instanceof JsonProcessingException) {
+                log.error("序列化出错: {}", e.getMessage(), e);
+                throw new CustomerBaseException("参数序列化出错", 500);
+            }
             log.error("模板转换出错: {}", e.getMessage(), e);
             throw new CustomerBaseException("模板转换出错", 500);
-        } catch (Exception e) {
-            e.printStackTrace();
         }
     }
 
-    private InformResult sendMessage(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, InformAddressees addressees) throws Exception {
+    /**
+     * send message
+     * @param informCo inform co
+     * @param informStrategyCmd strategy
+     * @param addressees address
+     * @return inform result(CompletableFuture)
+     */
+    private InformResult sendMessage(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, InformAddressees addressees) throws IOException {
         MessageTemplateEnum templateEnum = informCo.getTypeEnum().getTemplateEnum();
         // inform result
         InformResult informResult = new InformResult();
@@ -195,11 +211,20 @@ public class CommonInformServiceImpl implements CommonInformService {
         return informResult;
     }
 
+    /**
+     * save record
+     * @param informCo inform co
+     * @param informStrategyCmd strategy
+     * @param users users
+     * @return content id
+     * @throws JsonProcessingException json
+     */
     private Long saveSendRecord(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, List<KeycloakUserCO> users) throws JsonProcessingException {
         InformMessageRecordDto messageRecordDto = new InformMessageRecordDto();
         messageRecordDto.setTenant(informCo.getTenant());
         messageRecordDto.setOrg(informCo.getOrg());
         Map<String, String> messageMap = new HashMap<>(4);
+        log.info("strategy: {}", informStrategyCmd);
         if (Boolean.TRUE.equals(informStrategyCmd.getEmail())) {
             messageMap.put(InformMessageRecordDto.EMAIL_KEY, objectMapper.writeValueAsString(informCo.getEmailMessage()));
         }
@@ -216,43 +241,49 @@ public class CommonInformServiceImpl implements CommonInformService {
         return informMessageRecordService.saveRecord(users, informStrategyCmd, messageRecordDto);
     }
 
-    private void updateSendRecord(InformResult informResult, Long contentId, ExceptionInformStrategyCmd informStrategyCmd, List<String> userIds) {
-        CompletableFuture[] completableFutures = setFutureCombine(informResult, informStrategyCmd.getCount());
-        for (CompletableFuture completableFuture : completableFutures) {
-            log.info("com: {}", completableFuture);
-        }
-        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(completableFutures);
-        completableFuture.whenComplete((result, error) -> {
-            log.info("error: {}", error);
+    /**
+     * update record after future done
+     * @param informResult result
+     * @param contentId contentId
+     * @param informStrategyCmd strategy
+     */
+    private void updateSendRecord(InformResult informResult, Long contentId, ExceptionInformStrategyCmd informStrategyCmd) {
+        CompletableFuture<Void> totalFuture = setFutureCombine(informResult, informStrategyCmd.getCount());
+        totalFuture.whenComplete((result, error) -> {
+            log.error("future error: {}", error.getMessage(), error);
             log.info("update result flag");
-            informResult.updateResultFlag();
         });
+        totalFuture.join();
+        informResult.updateResultFlag();
         // update record
         informMessageRecordService.updateRecord(contentId, informResult.getResultFlag());
     }
 
-    public CompletableFuture[] setFutureCombine(InformResult informResult, Integer size) {
+    /**
+     * set future combine
+     * @param informResult result
+     * @param size future count
+     * @return combined future
+     */
+    public CompletableFuture<Void> setFutureCombine(InformResult informResult, Integer size) {
         int index = 0;
         CompletableFuture[] completableFutures = new CompletableFuture[size];
         if (informResult.getNoteResult() != null) {
-            log.info("index: {}", index);
             completableFutures[index] = informResult.getNoteResult();
             index++;
         }
         if (informResult.getEmailResult() != null) {
-            log.info("index: {}", index);
             completableFutures[index] = informResult.getEmailResult();
             index++;
         }
         if (informResult.getWeChatResult() != null) {
-            log.info("index: {}", index);
             completableFutures[index] = informResult.getWeChatResult();
             index++;
         }
         if (informResult.getWeChatAppletResult() != null) {
-            log.info("index: {}", index);
             completableFutures[index] = informResult.getWeChatAppletResult();
         }
-        return completableFutures;
+        log.info("combine future");
+        return CompletableFuture.allOf(completableFutures);
     }
 }