新增异步调用

docus_webservice_1.1
lzy 4 years ago
parent 9f2c342360
commit 5aed2d55fa

@ -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;
}
}

@ -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<CollectList> 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<AfInterfaceCollectSub> 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<String, String> params = new HashMap<>();
params.put("empId", jzh);
params.put("collectSubId", String.valueOf(collectList.getId()));
return HttpUtils.get(collectList.getRequestUrl(), params);
}
}

@ -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;
}

@ -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<CollectList> 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<AfInterfaceCollectSub> 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<String, String> params = new HashMap<>();
params.put("empId", jzh);
params.put("collectSubId", String.valueOf(collectList.getId()));
return HttpUtils.get(collectList.getRequestUrl(), params);
}
}

Loading…
Cancel
Save