enchancement #I3BZFY 部分工具类改成hutool的实现方式

This commit is contained in:
bryan31
2021-03-18 17:00:05 +08:00
parent 578469783e
commit 3aab928439
5 changed files with 16 additions and 165 deletions

View File

@@ -21,6 +21,7 @@ import java.util.Map.Entry;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap;
import cn.hutool.core.collection.BoundedPriorityQueue;
import com.yomahub.liteflow.util.SpringAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,7 +42,7 @@ public class MonitorBus {
private Logger LOG = LoggerFactory.getLogger(this.getClass());
private ConcurrentHashMap<String, LimitQueue<CompStatistics>> statisticsMap = new ConcurrentHashMap<String, LimitQueue<CompStatistics>>();
private final ConcurrentHashMap<String, BoundedPriorityQueue<CompStatistics>> statisticsMap = new ConcurrentHashMap<>();
public MonitorBus() {
}
@@ -69,7 +70,7 @@ public class MonitorBus {
if(statisticsMap.containsKey(statistics.getComponentClazzName())){
statisticsMap.get(statistics.getComponentClazzName()).offer(statistics);
}else{
LimitQueue<CompStatistics> queue = new LimitQueue<CompStatistics>(queueLimit);
BoundedPriorityQueue<CompStatistics> queue = new BoundedPriorityQueue<>(queueLimit);
queue.offer(statistics);
statisticsMap.put(statistics.getComponentClazzName(), queue);
}
@@ -81,7 +82,7 @@ public class MonitorBus {
long totalTimeSpent = 0;
for(Entry<String, LimitQueue<CompStatistics>> entry : statisticsMap.entrySet()){
for(Entry<String, BoundedPriorityQueue<CompStatistics>> entry : statisticsMap.entrySet()){
for(CompStatistics statistics : entry.getValue()){
totalTimeSpent += statistics.getTimeSpent();
}
@@ -90,12 +91,7 @@ public class MonitorBus {
List<Entry<String, BigDecimal>> compAverageTimeSpentEntryList = new ArrayList<>(compAverageTimeSpent.entrySet());
Collections.sort(compAverageTimeSpentEntryList,new Comparator<Entry<String, BigDecimal>>() {
@Override
public int compare(Entry<String, BigDecimal> o1, Entry<String, BigDecimal> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
Collections.sort(compAverageTimeSpentEntryList, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));
StringBuilder logStr = new StringBuilder();
logStr.append("以下为LiteFlow中间件统计信息\n");

View File

@@ -7,14 +7,13 @@
*/
package com.yomahub.liteflow.parser;
import com.yomahub.liteflow.util.IOUtil;
import cn.hutool.core.io.FileUtil;
public class LocalXmlFlowParser extends XmlFlowParser{
private final String ENCODING_FORMAT = "UTF-8";
public void parseMain(String rulePath) throws Exception {
String ruleContent = IOUtil.read(rulePath, ENCODING_FORMAT);
String ENCODING_FORMAT = "UTF-8";
String ruleContent = FileUtil.readString(rulePath, ENCODING_FORMAT);
parse(ruleContent);
}
}

View File

@@ -10,9 +10,11 @@ import java.util.regex.Pattern;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.entity.flow.*;
import com.yomahub.liteflow.exception.ExecutableItemNotFoundException;
import com.yomahub.liteflow.exception.ParseException;
import com.yomahub.liteflow.util.SpringAware;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +22,6 @@ import org.slf4j.LoggerFactory;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.spring.ComponentScaner;
import com.yomahub.liteflow.util.Dom4JReader;
public abstract class XmlFlowParser {
@@ -29,7 +30,7 @@ public abstract class XmlFlowParser {
public abstract void parseMain(String path) throws Exception;
public void parse(String content) throws Exception {
Document document = Dom4JReader.getFormatDocument(content);
Document document = DocumentHelper.parseText(content);
parse(document);
}
@@ -42,10 +43,10 @@ public abstract class XmlFlowParser {
if(ComponentScaner.nodeComponentMap.isEmpty()){
// 解析node节点
List<Element> nodeList = rootElement.element("nodes").elements("node");
String id = null;
String clazz = null;
Node node = null;
NodeComponent component = null;
String id;
String clazz;
Node node;
NodeComponent component;
Class<NodeComponent> nodeComponentClass;
for (Element e : nodeList) {
node = new Node();
@@ -57,6 +58,7 @@ public abstract class XmlFlowParser {
component = SpringAware.registerOrGet(nodeComponentClass);
if (component == null) {
LOG.error("couldn't find component class [{}] ", clazz);
throw new ParseException("cannot parse flow xml");
}
component.setNodeId(id);
node.setInstance(component);

View File

@@ -1,117 +0,0 @@
package com.yomahub.liteflow.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
public class Dom4JReader {
private static final String ENCODING_FORMAT = "UTF-8";
public static Document getDocument(String text) throws DocumentException {
return DocumentHelper.parseText(text);
}
public static Document getFormatDocument(String text) throws DocumentException, UnsupportedEncodingException {
return getFormatDocument(text, ENCODING_FORMAT);
}
public static Document getFormatDocument(String text, String charset) throws DocumentException, UnsupportedEncodingException {
String formatText = new String(text.getBytes("ISO-8859-1"), ENCODING_FORMAT);
return getDocument(formatText);
}
public static Document getDocument(File file) throws DocumentException, IOException {
InputStream inputStream = new FileInputStream(file);
return getDocument(inputStream);
}
public static Document getFormatDocument(File file) throws DocumentException, IOException, UnsupportedEncodingException {
return getFormatDocument(file, ENCODING_FORMAT);
}
public static Document getFormatDocument(File file, String charset) throws DocumentException, IOException, UnsupportedEncodingException {
InputStream inputStream = new FileInputStream(file);
return getFormatDocument(inputStream, charset);
}
public static Document getDocument(InputSource inputSource) throws DocumentException {
SAXReader saxReader = new SAXReader();
return saxReader.read(inputSource);
}
public static Document getFormatDocument(InputSource inputSource) throws DocumentException {
return getFormatDocument(inputSource, ENCODING_FORMAT);
}
public static Document getFormatDocument(InputSource inputSource, String charset) throws DocumentException {
inputSource.setEncoding(charset);
return getDocument(inputSource);
}
public static Document getDocument(InputStream inputStream) throws DocumentException, IOException {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(inputStream);
} catch (DocumentException e) {
throw e;
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return document;
}
public static Document getFormatDocument(InputStream inputStream) throws DocumentException, IOException, UnsupportedEncodingException {
return getFormatDocument(inputStream, ENCODING_FORMAT);
}
public static Document getFormatDocument(InputStream inputStream, String charset) throws DocumentException, IOException, UnsupportedEncodingException {
Reader inputStreamReader = new InputStreamReader(inputStream, charset);
return getDocument(inputStreamReader);
}
public static Document getDocument(Reader reader) throws DocumentException, IOException {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(reader);
} catch (DocumentException e) {
throw e;
} finally {
if (reader != null) {
reader.close();
}
}
return document;
}
public static Document getDocument(URL url) throws DocumentException {
SAXReader saxReader = new SAXReader();
return saxReader.read(url);
}
}

View File

@@ -1,29 +0,0 @@
package com.yomahub.liteflow.util;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
public class IOUtil {
public static String read(String path, String encoding) throws Exception {
String content = null;
InputStream inputStream = null;
try {
// 从Resource路径获取
inputStream = IOUtil.class.getClassLoader().getResourceAsStream(path);
if (inputStream == null) {
// 从文件路径获取
inputStream = new FileInputStream(path);
}
content = IOUtils.toString(inputStream, encoding);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
}
return content;
}
}