修复因为统计信息Map是static导致的jvm回收不了的情况

This commit is contained in:
bryan.zhang
2019-03-21 16:20:57 +08:00
parent d6ca28a659
commit 6a39a6771e
2 changed files with 46 additions and 40 deletions

View File

@@ -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<Integer> slotIndexTL = new InheritableThreadLocal<Integer>();
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 extends Slot> T getSlot(){
return DataBus.getSlot(this.slotIndexTL.get());
}

View File

@@ -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<String, LimitQueue<CompStatistics>> statisticsMap = new ConcurrentHashMap<String, LimitQueue<CompStatistics>>();
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<String, LimitQueue<CompStatistics>> statisticsMap = new ConcurrentHashMap<String, LimitQueue<CompStatistics>>();
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<String, BigDecimal> compAverageTimeSpent = new HashMap<String, BigDecimal>();
long totalTimeSpent = 0;
for(Entry<String, LimitQueue<CompStatistics>> 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<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());
}
});
StringBuilder logStr = new StringBuilder();
logStr.append("以下为LiteFlow中间件统计信息\n");
logStr.append("======================================================================================\n");