From 6a39a6771eb18739a2a93613b1d0b7c8bd432899 Mon Sep 17 00:00:00 2001 From: "bryan.zhang" Date: Thu, 21 Mar 2019 16:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=A0=E4=B8=BA=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E4=BF=A1=E6=81=AFMap=E6=98=AFstatic=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84jvm=E5=9B=9E=E6=94=B6=E4=B8=8D=E4=BA=86=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/core/NodeComponent.java | 36 ++++++------- .../liteflow/monitor/MonitorBus.java | 50 +++++++++++-------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/liteflow/src/main/java/com/thebeastshop/liteflow/core/NodeComponent.java b/liteflow/src/main/java/com/thebeastshop/liteflow/core/NodeComponent.java index 46c1f3882..a13dae2c8 100644 --- a/liteflow/src/main/java/com/thebeastshop/liteflow/core/NodeComponent.java +++ b/liteflow/src/main/java/com/thebeastshop/liteflow/core/NodeComponent.java @@ -24,34 +24,34 @@ import com.thebeastshop.liteflow.flow.FlowBus; import com.thebeastshop.liteflow.monitor.MonitorBus; public abstract class NodeComponent { - + private static final Logger LOG = LoggerFactory.getLogger(NodeComponent.class); - + private InheritableThreadLocal slotIndexTL = new InheritableThreadLocal(); - + private String nodeId; - + public void execute() throws Exception{ Slot slot = this.getSlot(); LOG.info("[{}]:[O]start component[{}] execution",slot.getRequestId(),this.getClass().getSimpleName()); slot.addStep(new CmpStep(nodeId, CmpStepType.START)); StopWatch stopWatch = new StopWatch(); stopWatch.start(); - + process(); - + stopWatch.stop(); long timeSpent = stopWatch.getTime(); - + slot.addStep(new CmpStep(nodeId, CmpStepType.END)); - + //性能统计 CompStatistics statistics = new CompStatistics(); statistics.setComponentClazzName(this.getClass().getSimpleName()); statistics.setTimeSpent(timeSpent); - MonitorBus.addStatistics(statistics); - - + MonitorBus.load().addStatistics(statistics); + + if(this instanceof NodeCondComponent){ String condNodeId = slot.getCondResult(this.getClass().getName()); if(StringUtils.isNotBlank(condNodeId)){ @@ -64,26 +64,26 @@ public abstract class NodeComponent { } } } - + LOG.debug("[{}]:componnet[{}] finished in {} milliseconds",slot.getRequestId(),this.getClass().getSimpleName(),timeSpent); } - + protected abstract void process() throws Exception; - + /** * 是否进入该节点 */ protected boolean isAccess(){ return true; } - + /** * 出错是否继续执行 */ protected boolean isContinueOnError() { return false; } - + /** * 是否结束整个流程(不往下继续执行) */ @@ -95,11 +95,11 @@ public abstract class NodeComponent { this.slotIndexTL.set(slotIndex); return this; } - + public Integer getSlotIndex() { return this.slotIndexTL.get(); } - + public T getSlot(){ return DataBus.getSlot(this.slotIndexTL.get()); } diff --git a/liteflow/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java b/liteflow/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java index c25973c5a..2e73c1dfb 100644 --- a/liteflow/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java +++ b/liteflow/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java @@ -32,23 +32,29 @@ import com.thebeastshop.liteflow.entity.monitor.CompStatistics; import com.thebeastshop.liteflow.util.LimitQueue; public class MonitorBus { - - private static final int QUEUE_LIMIT_SIZE = 200; - - private static final Logger LOG = LoggerFactory.getLogger(MonitorBus.class); - - private static ConcurrentHashMap> statisticsMap = new ConcurrentHashMap>(); - static{ - Timer timer = new Timer(); - timer.schedule(new TimerTask() { - public void run() { - MonitorBus.printStatistics(); - } - }, 5*60*1000L, 5*60*1000L); + private static final int QUEUE_LIMIT_SIZE = 200; + + private final Logger LOG = LoggerFactory.getLogger(this.getClass()); + + private ConcurrentHashMap> statisticsMap = new ConcurrentHashMap>(); + + private static MonitorBus monitorBus; + + public static MonitorBus load(){ + if(monitorBus == null){ + monitorBus = new MonitorBus(); + Timer timer = new Timer(); + timer.schedule(new TimerTask() { + public void run() { + monitorBus.printStatistics(); + } + }, 5*60*1000L, 5*60*1000L); + } + return monitorBus; } - - public static void addStatistics(CompStatistics statistics){ + + public void addStatistics(CompStatistics statistics){ if(statisticsMap.containsKey(statistics.getComponentClazzName())){ statisticsMap.get(statistics.getComponentClazzName()).offer(statistics); }else{ @@ -57,29 +63,29 @@ public class MonitorBus { statisticsMap.put(statistics.getComponentClazzName(), queue); } } - - public static void printStatistics(){ + + public void printStatistics(){ try{ Map compAverageTimeSpent = new HashMap(); - + long totalTimeSpent = 0; - + for(Entry> entry : statisticsMap.entrySet()){ for(CompStatistics statistics : entry.getValue()){ totalTimeSpent += statistics.getTimeSpent(); } compAverageTimeSpent.put(entry.getKey(), new BigDecimal(totalTimeSpent).divide(new BigDecimal(entry.getValue().size()), 2, RoundingMode.HALF_UP)); } - + 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()); } }); - + StringBuilder logStr = new StringBuilder(); logStr.append("以下为LiteFlow中间件统计信息:\n"); logStr.append("======================================================================================\n");