From 5aed2d55fadad3d0c06d681e62e5a00abbba9089 Mon Sep 17 00:00:00 2001 From: lzy Date: Mon, 22 Nov 2021 11:16:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BC=82=E6=AD=A5=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webservice/config/AsyncConfiguration.java | 32 ++++++ .../webservice/handler/TBasicWebService.java | 63 ++--------- .../webservice/service/IPcmachineService.java | 6 ++ .../service/PcmachineServiceImpl.java | 100 +++++++++++++++++- 4 files changed, 146 insertions(+), 55 deletions(-) create mode 100644 docus-webservice/src/main/java/com/docus/webservice/config/AsyncConfiguration.java diff --git a/docus-webservice/src/main/java/com/docus/webservice/config/AsyncConfiguration.java b/docus-webservice/src/main/java/com/docus/webservice/config/AsyncConfiguration.java new file mode 100644 index 0000000..f6b55ab --- /dev/null +++ b/docus-webservice/src/main/java/com/docus/webservice/config/AsyncConfiguration.java @@ -0,0 +1,32 @@ +package com.docus.webservice.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +@Configuration +@EnableAsync +public class AsyncConfiguration { + @Bean("execCollector") + public Executor doSomethingExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + // 核心线程数:线程池创建时候初始化的线程数 + executor.setCorePoolSize(10); + // 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程 + executor.setMaxPoolSize(20); + // 缓冲队列:用来缓冲执行任务的队列 + executor.setQueueCapacity(500); + // 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁 + executor.setKeepAliveSeconds(60); + // 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池 + executor.setThreadNamePrefix("do-something-"); + // 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程) + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); + executor.initialize(); + return executor; + } +} diff --git a/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java b/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java index 04af18d..606f72a 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java +++ b/docus-webservice/src/main/java/com/docus/webservice/handler/TBasicWebService.java @@ -12,12 +12,14 @@ import com.docus.webservice.entity.TBasic; import com.docus.webservice.enums.Codes; import com.docus.webservice.mapper.AfInterfaceCollectMapper; import com.docus.webservice.service.IMrReportErrorService; +import com.docus.webservice.service.IPcmachineService; import com.docus.webservice.service.ITBasicService; import com.docus.webservice.utils.*; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import javax.jws.WebService; import java.io.ByteArrayInputStream; @@ -36,6 +38,9 @@ public class TBasicWebService implements ITBasicWebService { @Autowired private IMrReportErrorService mrReportErrorService; + @Autowired + IPcmachineService pcmachineService; + @Autowired private AfInterfaceCollectMapper afInterfaceCollectMapper; @@ -82,7 +87,8 @@ public class TBasicWebService implements ITBasicWebService { tBasic = JSON.parseObject(JSON.toJSONString(tBasicMap), TBasic.class); tBasic.setCreateTime(new Date()); itBasicService.savaAndSub(tBasic, tBasicSubMap); - this.saveAndCall(tBasic); +// this.saveAndCall(tBasic); + pcmachineService.saveAndCall(tBasic); } else { insertLog(tbasic); log.info("请提供完整的xml"); @@ -168,7 +174,8 @@ public class TBasicWebService implements ITBasicWebService { tBasic.setUpdateTime(new Date()); try { itBasicService.updateAndSub(tBasic, tBasicSubMap); - this.saveAndCall(tBasic); +// this.saveAndCall(tBasic); + pcmachineService.saveAndCall(tBasic); } catch (RuntimeException e) { insertLog(tbasic); e.printStackTrace(); @@ -189,58 +196,8 @@ public class TBasicWebService implements ITBasicWebService { } - public void saveAndCall(TBasic tBasic) throws URISyntaxException { - log.info("病案对象" + tBasic.toString()); - //解析json映射文件 - String json = JsonUtils.readJsonFile(CurrentPath() + Codes.JSON_COLLECTLIST.getMessage()); - if (StringUtils.isNotBlank(json)) { - Map jsonMap = JSON.parseObject(json, Map.class); - //任务数 - Integer task_count = JSON.parseObject(String.valueOf(jsonMap.get(Codes.JSON_TASK_COUNT.getMessage())), Integer.class); - - List collectLists = JSON.parseArray(String.valueOf(jsonMap.get(Codes.JSON_COLLECTLIST_ROOT.getMessage())), CollectList.class); - - //添加省中医病案采集 - SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0); - AfInterfaceCollect afc = new AfInterfaceCollect(); - Long afcId = idWorker.nextId(); - afc.setId(afcId); - afc.setJzh(tBasic.getJzh()); - afc.setTaskCount(task_count); - afc.setCreateTime(new Date()); - int i = afInterfaceCollectMapper.addAfInterfaceCollect(afc); - if (i < 0) { - log.info("省中医病案采集表添加信息失败!"); - } - //添加省中医病案采集-子任务 - List list = new ArrayList<>(); - for (CollectList collectList : collectLists) { - AfInterfaceCollectSub afInterfaceCollectSub = new AfInterfaceCollectSub(); - Long afcsId = idWorker.nextId(); - afInterfaceCollectSub.setId(afcsId); - afInterfaceCollectSub.setAfInterfaceCollectId(afcId); - afInterfaceCollectSub.setCollectsysCode(collectList.getCollectsys_code()); - afInterfaceCollectSub.setJzh(tBasic.getJzh()); - collectList.setId(afcsId); - list.add(afInterfaceCollectSub); - } - int i1 = afInterfaceCollectMapper.addAfInterfaceCollectSub(list); - if (i1 < 0) { - log.info("省中医病案采集-子任务失败!"); - } - for (CollectList collectList : collectLists) { - //调用http发送请求 - this.sendHttp(collectList, tBasic.getJzh()); - } - } - } - private String sendHttp(CollectList collectList, String jzh) throws URISyntaxException { - Map params = new HashMap<>(); - params.put("empId", jzh); - params.put("collectSubId", String.valueOf(collectList.getId())); - return HttpUtils.get(collectList.getRequestUrl(), params); - } + } diff --git a/docus-webservice/src/main/java/com/docus/webservice/service/IPcmachineService.java b/docus-webservice/src/main/java/com/docus/webservice/service/IPcmachineService.java index 25205c1..3ca8830 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/service/IPcmachineService.java +++ b/docus-webservice/src/main/java/com/docus/webservice/service/IPcmachineService.java @@ -1,9 +1,15 @@ package com.docus.webservice.service; +import com.docus.webservice.entity.TBasic; + +import java.net.URISyntaxException; + public interface IPcmachineService { void beat(String code, String ip); void isBeat(); Integer count(); + + void saveAndCall(TBasic tBasic) throws URISyntaxException; } diff --git a/docus-webservice/src/main/java/com/docus/webservice/service/PcmachineServiceImpl.java b/docus-webservice/src/main/java/com/docus/webservice/service/PcmachineServiceImpl.java index aa0fbc7..7c911ec 100644 --- a/docus-webservice/src/main/java/com/docus/webservice/service/PcmachineServiceImpl.java +++ b/docus-webservice/src/main/java/com/docus/webservice/service/PcmachineServiceImpl.java @@ -1,18 +1,40 @@ package com.docus.webservice.service; +import com.alibaba.fastjson.JSON; +import com.docus.webservice.dto.CollectList; +import com.docus.webservice.entity.AfInterfaceCollect; +import com.docus.webservice.entity.AfInterfaceCollectSub; import com.docus.webservice.entity.Pcmachine; +import com.docus.webservice.entity.TBasic; +import com.docus.webservice.enums.Codes; +import com.docus.webservice.mapper.AfInterfaceCollectMapper; import com.docus.webservice.mapper.PcmachineMapper; +import com.docus.webservice.utils.HttpUtils; +import com.docus.webservice.utils.JsonUtils; +import com.docus.webservice.utils.SnowflakeIdWorker; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import java.util.Date; +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.*; @Service public class PcmachineServiceImpl implements IPcmachineService { + private Logger log= LogManager.getLogger(); + @Autowired PcmachineMapper pcmachineMapper; + @Autowired + private AfInterfaceCollectMapper afInterfaceCollectMapper; + @Override public void beat(String code, String ip) { @@ -23,10 +45,12 @@ public class PcmachineServiceImpl implements IPcmachineService { pcmachine.setIpaddress(ip); pcmachine.setLastonline(new Date()); pcmachine.setPcstatus(1); - System.out.println(pcmachine); pcmachineMapper.update(pcmachine); } + + + @Override public void isBeat() { pcmachineMapper.isBeat(); @@ -36,4 +60,76 @@ public class PcmachineServiceImpl implements IPcmachineService { public Integer count() { return pcmachineMapper.count(); } + + /** + * 获取jar包所在位置 + * + * @return + */ + private String CurrentPath() { + File dir = new File("."); + String currentpath = ""; + try { + currentpath = dir.getCanonicalPath(); + } catch (IOException e) { + e.printStackTrace(); + } + return currentpath; + } + + @Async("execCollector") + @Override + public void saveAndCall(TBasic tBasic) throws URISyntaxException { + log.info("病案对象" + tBasic.toString()); + //解析json映射文件 + String json = JsonUtils.readJsonFile(CurrentPath() + Codes.JSON_COLLECTLIST.getMessage()); + if (StringUtils.isNotBlank(json)) { + Map jsonMap = JSON.parseObject(json, Map.class); + //任务数 + Integer task_count = JSON.parseObject(String.valueOf(jsonMap.get(Codes.JSON_TASK_COUNT.getMessage())), Integer.class); + + List collectLists = JSON.parseArray(String.valueOf(jsonMap.get(Codes.JSON_COLLECTLIST_ROOT.getMessage())), CollectList.class); + + //添加省中医病案采集 + SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0); + AfInterfaceCollect afc = new AfInterfaceCollect(); + Long afcId = idWorker.nextId(); + afc.setId(afcId); + afc.setJzh(tBasic.getJzh()); + afc.setTaskCount(task_count); + afc.setCreateTime(new Date()); + int i = afInterfaceCollectMapper.addAfInterfaceCollect(afc); + if (i < 0) { + log.info("省中医病案采集表添加信息失败!"); + } + //添加省中医病案采集-子任务 + List list = new ArrayList<>(); + for (CollectList collectList : collectLists) { + AfInterfaceCollectSub afInterfaceCollectSub = new AfInterfaceCollectSub(); + Long afcsId = idWorker.nextId(); + afInterfaceCollectSub.setId(afcsId); + afInterfaceCollectSub.setAfInterfaceCollectId(afcId); + afInterfaceCollectSub.setCollectsysCode(collectList.getCollectsys_code()); + afInterfaceCollectSub.setJzh(tBasic.getJzh()); + collectList.setId(afcsId); + list.add(afInterfaceCollectSub); + } + int i1 = afInterfaceCollectMapper.addAfInterfaceCollectSub(list); + if (i1 < 0) { + log.info("省中医病案采集-子任务失败!"); + } + for (CollectList collectList : collectLists) { + //调用http发送请求 + this.sendHttp(collectList, tBasic.getJzh()); + } + + } + } + + private String sendHttp(CollectList collectList, String jzh) throws URISyntaxException { + Map params = new HashMap<>(); + params.put("empId", jzh); + params.put("collectSubId", String.valueOf(collectList.getId())); + return HttpUtils.get(collectList.getRequestUrl(), params); + } }