From 3aab9284395460d91a34bc7fdea4757c46988490 Mon Sep 17 00:00:00 2001 From: bryan31 Date: Thu, 18 Mar 2021 17:00:05 +0800 Subject: [PATCH] =?UTF-8?q?enchancement=20#I3BZFY=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=94=B9=E6=88=90hutool=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yomahub/liteflow/monitor/MonitorBus.java | 14 +-- .../liteflow/parser/LocalXmlFlowParser.java | 7 +- .../liteflow/parser/XmlFlowParser.java | 14 ++- .../yomahub/liteflow/util/Dom4JReader.java | 117 ------------------ .../com/yomahub/liteflow/util/IOUtil.java | 29 ----- 5 files changed, 16 insertions(+), 165 deletions(-) delete mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/util/Dom4JReader.java delete mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/util/IOUtil.java diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorBus.java b/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorBus.java index 5acb97347..0e01f8301 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorBus.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/monitor/MonitorBus.java @@ -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> statisticsMap = new ConcurrentHashMap>(); + private final ConcurrentHashMap> 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 queue = new LimitQueue(queueLimit); + BoundedPriorityQueue queue = new BoundedPriorityQueue<>(queueLimit); queue.offer(statistics); statisticsMap.put(statistics.getComponentClazzName(), queue); } @@ -81,7 +82,7 @@ public class MonitorBus { long totalTimeSpent = 0; - for(Entry> entry : statisticsMap.entrySet()){ + for(Entry> entry : statisticsMap.entrySet()){ for(CompStatistics statistics : entry.getValue()){ totalTimeSpent += statistics.getTimeSpent(); } @@ -90,12 +91,7 @@ public class MonitorBus { List> compAverageTimeSpentEntryList = new ArrayList<>(compAverageTimeSpent.entrySet()); - Collections.sort(compAverageTimeSpentEntryList,new Comparator>() { - @Override - public int compare(Entry o1, Entry 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"); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/LocalXmlFlowParser.java b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/LocalXmlFlowParser.java index 9a0f06ef2..d95e21503 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/LocalXmlFlowParser.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/LocalXmlFlowParser.java @@ -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); } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java index 4ebcf0537..c8ae15e5c 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/parser/XmlFlowParser.java @@ -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 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 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); diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/util/Dom4JReader.java b/liteflow-core/src/main/java/com/yomahub/liteflow/util/Dom4JReader.java deleted file mode 100644 index 80ae7be77..000000000 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/util/Dom4JReader.java +++ /dev/null @@ -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); - } -} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/util/IOUtil.java b/liteflow-core/src/main/java/com/yomahub/liteflow/util/IOUtil.java deleted file mode 100644 index 15b35bacc..000000000 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/util/IOUtil.java +++ /dev/null @@ -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; - } -}