Skip to content
Snippets Groups Projects
Commit 2682efd2 authored by Aiolos's avatar Aiolos
Browse files

fix merge confliction

parents 6e484c14 a47aa994
No related branches found
No related tags found
No related merge requests found
......@@ -32,4 +32,6 @@ public class CallbackListenerImpl implements CallbackListener {
UserLoadBalance.weight_small = 6.0 * memory_small / memory_sum;
}
private String cachedServerCpu;
}
......@@ -3,11 +3,14 @@ package com.aliware.tianchi;
import org.apache.dubbo.rpc.listener.CallbackListener;
import org.apache.dubbo.rpc.service.CallbackService;
import java.util.Date;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* @author daofeng.xjf
......@@ -23,6 +26,7 @@ public class CallbackServiceImpl implements CallbackService {
@Override
public void run() {
if (!listeners.isEmpty()) {
String messageToPush = generateStatusMessage();
for (Map.Entry<String, CallbackListener> entry : listeners.entrySet()) {
try {
//entry.getValue().receiveServerMsg(System.getProperty("quota") + " " + new Date().toString());
......@@ -38,6 +42,20 @@ public class CallbackServiceImpl implements CallbackService {
private Timer timer = new Timer();
private String generateStatusMessage() {
try {
List<String> cpuLoadList = getProcessCpuLoad().stream().map(Object::toString).collect(Collectors.toList());
String cpuLoadString = String.join(",", cpuLoadList);
System.out.println("Server push " + cpuLoadString);
return "cpu=" + cpuLoadString;
}
catch(Exception ex) {
return "error";
}
}
/**
* key: listener type
* value: callback listener
......@@ -46,7 +64,30 @@ public class CallbackServiceImpl implements CallbackService {
@Override
public void addListener(String key, CallbackListener listener) {
System.out.println("Server: add Listener " + key);
listeners.put(key, listener);
listener.receiveServerMsg(new Date().toString()); // send notification for change
}
// System status impl
private static List<Double> getProcessCpuLoad() throws Exception {
List<Double> result = new ArrayList<>();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
AttributeList list = mbs.getAttributes(name, new String[]{ "ProcessCpuLoad" });
for(int i = 0; i < list.size(); ++i) {
Attribute att = (Attribute) list.get(i);
Double value = (Double)att.getValue();
if(value == -1.0)
// usually takes a couple of seconds before we get real values
result.add(Double.NaN);
else
// returns a percentage value with 1 decimal point precision
result.add(((int)(value * 1000) / 10.0));
}
return result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment