###
UI控制层:
private static Boolean WEBINUSE = false;
@GET
@Path("/testWebserviceAll/{type}/{startIndex}")
public String testWebserviceAll(@PathParam("type")String typeName,@PathParam("startIndex")Integer startIndex) throws InterruptedException {
if(WEBINUSE) {
return "{'message':'数据上传中'}";
}else {
WEBINUSE = true;
}
Timestamp startTime = new Timestamp(System.currentTimeMillis()-100L*24*60*60*1000);
Timestamp endTime = new Timestamp(System.currentTimeMillis());
UploadDataType type = UploadDataType.valueOf(typeName);
webService.uploadDatasInThread(startIndex,type,startTime, endTime);
WEBINUSE = false;
return "{'success':'success'}";
}
serviceimpl实现业务功能
@Override
public void uploadDatasInThread(Integer startIndex,UploadDataType dataType, Timestamp startTime, Timestamp endTime)
throws InterruptedException {
// 查询对应数据集数据
Integer count = dataService.queryUploadDataCount(dataType, startTime, endTime);
Integer pageCount = count / UploadThread.PAGE_SIZE + 1;
System.out.println(dataType.name() + "::count:" + pageCount);
for (int i = startIndex; i <= pageCount; i++) {//循环开启10个线程
while (UploadThread.RUNNING_COUNT >= UploadThread.RUNNING_MAX_COUNT) {
//当前运行线程数大于最大运行线程数时休息2s
Thread.sleep(2000);
}
UploadThread.RUNNING_COUNT++;
UploadThread thread = new UploadThread(dataService, logService, this, dataType, startTime, endTime, i,
pageCount);
thread.start();
}
}
多线程实现
public class UploadThread extends Thread {
/**
* 当前运行线程数
*/
public static Integer RUNNING_COUNT = 0;
/**
* 最大运行线程数
*/
public static Integer RUNNING_MAX_COUNT = 10;
public static final Integer PAGE_SIZE = 100;
private DataConfigBaseService dataService;
private DataLogService logService;
private UploadDataWebservice webService;
private UploadDataType dataType;
private Timestamp startTime;
private Timestamp endTime;
private Integer pageNum; //第几页
private Integer pageCount; //总页数
public UploadThread(DataConfigBaseService dataService, DataLogService logService, UploadDataWebservice webService,
UploadDataType dataType, Timestamp startTime, Timestamp endTime, Integer pageNum, Integer pageCount) {
super();
this.dataService = dataService;
this.logService = logService;
this.webService = webService;
this.dataType = dataType;
this.startTime = startTime;
this.endTime = endTime;
this.pageNum = pageNum;
this.pageCount = pageCount;
}
public UploadThread() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void run() {
//System.out.println("start!!:"+dataType.name()+","+pageNum+"/"+pageCount);
// 创建一个用于产生WebServiceImpl实例的工厂,WebServiceImplService类是wsimport工具生成的
WebServiceEntryService factory = new WebServiceEntryService();
// 通过工厂生成一个WebServiceImpl实例,WebServiceImpl是wsimport工具生成的
WebServiceEntry wsImpl = factory.getWebServiceEntryPort();
// 查询对应数据集数据
Integer successNum = 0;
StringBuilder failedMessage = new StringBuilder();
Set<String> allReasons = new HashSet<>();
List<LogNodeView> logNode = dataService.queryUploadData(dataType, startTime, endTime, pageNum, PAGE_SIZE);
if (CollectionUtils.isNotEmpty(logNode)) {
for (LogNodeView nodeView : logNode) {
String xml = nodeView.getNoveView() == null ? "" : nodeView.getNoveView().toString();
// System.out.println(xml);
try {
// 发送webService请求
String result = webService.sendMessage(wsImpl, xml);
String code = getResultCode(result);
Set<String> reasons = getReason(result);
if(reasons!=null) {
allReasons.addAll(reasons);
}
if ("200".equals(code)) {
successNum++;
}else {
failedMessage.append(nodeView.getId()+",");
}
// System.out.println(result);
} catch (Exception_Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DataLog log = new DataLog();
log.setId(GeneratorUUID.uuid());
log.setCreateDate(new Date());
log.setDataType(dataType.name());
log.setFailedIds(failedMessage.toString());
log.setSuccessNum(successNum);
log.setFlag("1");
log.setPageNum(pageNum);
log.setPageSize(PAGE_SIZE);
log.setPageCount(pageCount);
log.setFailedReasons(allReasons.toString());
logService.addLog(log);
System.out.println("type:" + dataType.name() + ",pageNum::" + pageNum + ",pageSize+::" + PAGE_SIZE
+ ",successNum::" + successNum);
RUNNING_COUNT--;
}
/**
* 获取信息的code节点
* @param result
* @return
*/
private static String getResultCode(String result) {
// 创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 创建一个DocumentBuilder的对象
try {
// 创建DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
// 通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
org.w3c.dom.Document document = db.parse(new InputSource(new StringReader(result)));
NodeList nodes = document.getChildNodes();
if (nodes.getLength() > 0) {
for (int i = 0; i < nodes.getLength(); i++) {
String value = getCodeValue(nodes.item(i));
if (value != null) {
return value;
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String getCodeValue(Node node) {
if (node.getNodeName().equals("Code")) {
NodeList childNodes = node.getChildNodes();
if (childNodes.getLength() > 0) {
return childNodes.item(0).getNodeValue();
}
return node.getNodeValue();
} else {
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
String value = getCodeValue(childNode);
if (value != null) {
return value;
}
}
}
return null;
}
/**
* 获取错误信息里的reason节点
* @param result
* @return
*/
private Set<String> getReason(String result) {
// 创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 创建一个DocumentBuilder的对象
try {
// 创建DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
// 通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
org.w3c.dom.Document document = db.parse(new InputSource(new StringReader(result)));
NodeList nodes = document.getChildNodes();
if (nodes.getLength() > 0) {
for (int i = 0; i < nodes.getLength(); i++) {
Set<String> value = getReasonValue(nodes.item(i));
if (CollectionUtils.isNotEmpty(value)) {
return value;
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static Set<String> getReasonValue(Node node) {
if (node.getNodeName().equals("Describe")) {
NodeList childNodes = node.getChildNodes();
if (childNodes.getLength() > 0) {
return getReasons(childNodes.item(0).getNodeValue());
}
return getReasons(node.getNodeValue());
} else {
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
Set<String> value = getReasonValue(childNode);
if (CollectionUtils.isNotEmpty(value)) {
return value;
}
}
}
return null;
}
private static Set<String> getReasons(String value){
Set<String> reasons = new HashSet<>();
if(StringUtils.isNotEmpty(value)) {
String[] messages = value.split("reason:");
if(messages.length>0) {
for(String message:messages) {
if(message.contains("节点")) {
if(message.endsWith(",")) {
message = message.substring(0, message.length()-1);
}
reasons.add(message);
}
}
}
}
return reasons;
}
}