Explorar el Código

Merge branch 'dev' into local-master

# Conflicts:
#	nb-service/web-service/src/main/java/com/nb/web/service/bus/hospital/HospitalManager.java
18339543638 hace 2 años
padre
commit
eca546a7d6

+ 5 - 3
nb-service/web-service/src/main/java/com/nb/web/service/bus/hospital/HospitalManager.java

@@ -265,11 +265,13 @@ public class HospitalManager {
             return;
         }
         //参数未改变
-        if(ObjectUtil.equals(this.getUpdateConfig().getInterval(),updateConfig.getInterval())){
+        if(ObjectUtil.isNotNull(this.getUpdateConfig())&&ObjectUtil.equals(this.getUpdateConfig().getInterval(),updateConfig.getInterval())){
             return;
         }else {
-            //取消上次循环
-            hisSchedule.cancel(true);
+            if(hisSchedule!=null){
+                //取消上次循环
+                hisSchedule.cancel(true);
+            }
             hisSchedule = singleHisExecutor.scheduleAtFixedRate(this::scheduleHis, 0, updateConfig.getInterval(),TimeUnit.MINUTES);
         }
         this.updateConfig=updateConfig;

+ 24 - 30
nb-service/web-service/src/main/java/com/nb/web/service/bus/hospital/his/HisScriptSession.java

@@ -1,6 +1,7 @@
 package com.nb.web.service.bus.hospital.his;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import cn.hutool.json.JSON;
@@ -8,6 +9,8 @@ import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
 import com.nb.web.api.entity.BusClinicEntity;
 import com.nb.web.service.bus.entity.*;
 import com.nb.web.service.bus.listener.event.bean.HisEvent;
@@ -62,10 +65,13 @@ public class HisScriptSession {
     private LocalBusConMixService conMixService;
     private LocalBusConDoctorService conDoctorService;
     //缓存发给医院的请求,对相同的请求进行批量处理
-    private Map<String,HisRequest> hisRequestMap=new ConcurrentHashMap<>();
-    //缓存发给医院的请求,对相同的请求进行批量处理
-    private Map<String,List<HisRequest>> hisRequestMapByPatientCode=new ConcurrentHashMap<>();
+    private Cache<String, HisRequest> hisRequestMap = CacheBuilder.newBuilder()
+            .expireAfterWrite(30, TimeUnit.SECONDS)
+            .build();
     public HisScriptSession(String hospitalId, ScriptManager scriptManager, ConfigStorageManager configStorageManager) {
+        Cache<String, HisRequest> build = CacheBuilder.newBuilder()
+                .expireAfterWrite(30, TimeUnit.SECONDS)
+                .build();
         this.hospitalId = hospitalId;
         this.scriptManager = scriptManager;
         this.clinicService = SpringUtil.getBean(LocalBusClinicService.class);
@@ -150,7 +156,7 @@ public class HisScriptSession {
             BusClinicEntity clinic = clinicService.recentClinicByPatientCode(hospitalId, patientCode);
             result.setResult(R.success(clinic));
             //超时后不在响应
-            hisRequestMap.remove(messageId);
+            hisRequestMap.invalidate(messageId);
         });
         return result;
     };
@@ -203,7 +209,7 @@ public class HisScriptSession {
                 BusClinicEntity clinic = clinicService.getById(patient.getClinicId());
                 result.setResult(R.success(clinic));
             }
-            hisRequestMap.remove(messageId);
+            hisRequestMap.invalidate(messageId);
         });
         sendRequest(channelContext,request);
         return result;
@@ -337,32 +343,24 @@ public class HisScriptSession {
         //判断是否成功,是否超时
         String messageId = hisResponse.getMessageId();
         Assert.hasText(messageId,"his脚本响应数据无messageId");
-        HisRequest hisRequest = hisRequestMap.remove(messageId);
+        HisRequest hisRequest = hisRequestMap.getIfPresent(messageId);
+        hisRequestMap.invalidate(messageId);
+        boolean sync = hisResponse.isSync();
+        if(ObjectUtil.isNull(hisRequest)){
+            if(sync){
+                return;
+            }
+        }
         if(hisResponse.isSuccess()){
             //同步请求当超时之后,不再处理数据
-            boolean sync = hisResponse.isSync();
             if (hisRequest == null) {
                 log.warn("响应[{}]请求不存在,或已超时",messageId);
-                if(sync){
-                    return;
-                }
                 //正常响应
                 handle(messageId,Value.simple(hisResponse.getContext()).asString(), hisResponse.getPatientCode());
             }else {
-                Date responseTimestamp = hisResponse.getTimestamp();
-                Date requestTimestamp = hisRequest.getTimestamp();
-                long timeout = hisRequest.getTimeout();
-                TimeUnit timeUnit = hisRequest.getTimeUnit();
                 List<DeferredResult<R<BusClinicEntity>>> results = hisRequest.getResult();
                 if(CollectionUtil.isNotEmpty(results)){
-                    results.parallelStream().forEach(result->{
-                        if (requestTimestamp.getTime()+timeUnit.toMillis(timeout)<responseTimestamp.getTime()) {
-                            log.warn("请求[{}]已超时,请求时间[{}],响应时间[{}],是否为同步请求[{}]",messageId,requestTimestamp,responseTimestamp,sync);
-                            if(sync){
-                                result.setErrorResult("响应超时");
-                                return;
-                            }
-                        }
+                    results.forEach(result->{
                         //正常响应
                         try {
                             BusClinicEntity clinic = handle(messageId,Value.simple(hisResponse.getContext()).asString(), hisResponse.getPatientCode());
@@ -374,19 +372,15 @@ public class HisScriptSession {
                         }catch (ScriptException e){
                             result.setResult(R.fail(String.format("住院号【[%s]】病人数据查询为空",hisResponse.getPatientCode())));
                         }
-
-
                     });
                 }
 
             }
         }else {
-            if (hisRequest != null) {
-                log.warn("医院[{}]拉取信息失败,失败原因[{}]",hospitalId,hisResponse.getErrorMsg());
-                List<DeferredResult<R<BusClinicEntity>>> results = hisRequest.getResult();
-                if(CollectionUtil.isNotEmpty(results)){
-                    results.parallelStream().forEach(result-> result.setResult(R.fail("更新失败,失败原因["+ hisResponse.getErrorMsg()+"]")));
-                }
+            log.warn("医院[{}]拉取信息失败,失败原因[{}]",hospitalId,hisResponse.getErrorMsg());
+            List<DeferredResult<R<BusClinicEntity>>> results = hisRequest.getResult();
+            if(CollectionUtil.isNotEmpty(results)){
+                results.parallelStream().forEach(result-> result.setResult(R.fail("更新失败,失败原因["+ hisResponse.getErrorMsg()+"]")));
             }
         }
     }