///定时器TimerTask
web.xml中配置自己写的也可以
<listener>
<listener-class>com.certusnet.nfv.mano.vim.usage_history.rest.Usage_historyListener</listener-class>
</listener>
//Usage_historyListener.java
package com.certusnet.nfv.mano.vim.usage_history.rest;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.certusnet.nfv.mano.vim.usage_history.rsclient.IUsage_historyRsClient;
public class Usage_historyListener implements ServletContextListener {
public static IUsage_historyRsClient usage_historyRsClient;
Timer timer = new Timer();
@Override
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
WebApplicationContext rwp = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
usage_historyRsClient = (IUsage_historyRsClient) rwp.getBean("usage_historyRsClient");
timer.schedule(new Usage_history_Timer(), 0, 40*1000);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// TODO Auto-generated method stub
timer.cancel();
}
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new Usage_history_Timer(), 0, 40*1000);
}
}
//Usage_history_Timer.java
package com.certusnet.nfv.mano.vim.usage_history.rest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.TimerTask;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.certusnet.nfv.mano.exception.ManoException;
import com.certusnet.nfv.mano.vim.ExceptionCode;
import com.certusnet.nfv.mano.vim.HttpsUtil;
import com.certusnet.nfv.mano.vim.usage_history.rsclient.IUsage_historyRsClient;
public class Usage_history_Timer extends TimerTask {
private static Logger logger = Logger.getLogger(Usage_history_Timer.class);
public static LinkedList<JSONObject> vcpus_queue = new LinkedList<JSONObject>();
public static LinkedList<JSONObject> memory_queue = new LinkedList<JSONObject>();
public static LinkedList<JSONObject> disk_queue = new LinkedList<JSONObject>();
@Autowired
private IUsage_historyRsClient usage_historyRsClient;
public void findUsage_history_timer() {
try {
if (usage_historyRsClient == null) {
usage_historyRsClient = Usage_historyListener.usage_historyRsClient;
}
String returnStr = usage_historyRsClient.findUsage_history();
// String url = "https://172.16.25.163/" + "/usage_api/get_hypervisor_statistics";
// String returnStr = new String(HttpsUtil.getMethod(url));
logger.info("usage_historyRsClient=" + returnStr);
returnStr = returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
float vcpus_used = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("vcpus_used");
float vcpus = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("vcpus");
float vcpus_usage_temp = vcpus_used / vcpus;
BigDecimal vcpus_usage_big = new BigDecimal(vcpus_usage_temp);
float vcpus_usage = vcpus_usage_big.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
float memory_mb_used = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("memory_mb_used");
float memory_mb = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("memory_mb");
float memory_usage_temp = memory_mb_used / memory_mb;
BigDecimal memory_usage_big = new BigDecimal(memory_usage_temp);
float memory_usage = memory_usage_big.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
float local_gb_used = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("local_gb_used");
float local_gb = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("local_gb");
float disk_usage_temp = local_gb_used / local_gb;
BigDecimal disk_usage_big = new BigDecimal(disk_usage_temp);
float disk_usage = disk_usage_big.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(ExceptionCode.FIND_Usage_History_FAILED);
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
JSONObject vcpusObject = new JSONObject();
vcpusObject.put("y", vcpus_usage);
vcpusObject.put("x", df.format(date));
vcpus_queue.addLast(vcpusObject);
if (vcpus_queue.size() > 15) {
vcpus_queue.removeFirst();
}
JSONObject memoryObject = new JSONObject();
memoryObject.put("y", memory_usage);
memoryObject.put("x", df.format(date));
memory_queue.addLast(memoryObject);
if (memory_queue.size() > 15) {
memory_queue.removeFirst();
}
JSONObject diskObject = new JSONObject();
diskObject.put("y", disk_usage);
diskObject.put("x", df.format(date));
disk_queue.addLast(diskObject);
if (disk_queue.size() > 15) {
disk_queue.removeFirst();
}
System.out.println("vcpus_queue = " + vcpus_queue);
System.out.println("memory_queue = " + memory_queue);
System.out.println("disk_queue = " + disk_queue);
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_Usage_History_FAILED, ex);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
findUsage_history_timer();
}
}
web.xml中配置自己写的也可以
<listener>
<listener-class>com.certusnet.nfv.mano.vim.usage_history.rest.Usage_historyListener</listener-class>
</listener>
//Usage_historyListener.java
package com.certusnet.nfv.mano.vim.usage_history.rest;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.certusnet.nfv.mano.vim.usage_history.rsclient.IUsage_historyRsClient;
public class Usage_historyListener implements ServletContextListener {
public static IUsage_historyRsClient usage_historyRsClient;
Timer timer = new Timer();
@Override
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
WebApplicationContext rwp = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
usage_historyRsClient = (IUsage_historyRsClient) rwp.getBean("usage_historyRsClient");
timer.schedule(new Usage_history_Timer(), 0, 40*1000);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// TODO Auto-generated method stub
timer.cancel();
}
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new Usage_history_Timer(), 0, 40*1000);
}
}
//Usage_history_Timer.java
package com.certusnet.nfv.mano.vim.usage_history.rest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.TimerTask;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.certusnet.nfv.mano.exception.ManoException;
import com.certusnet.nfv.mano.vim.ExceptionCode;
import com.certusnet.nfv.mano.vim.HttpsUtil;
import com.certusnet.nfv.mano.vim.usage_history.rsclient.IUsage_historyRsClient;
public class Usage_history_Timer extends TimerTask {
private static Logger logger = Logger.getLogger(Usage_history_Timer.class);
public static LinkedList<JSONObject> vcpus_queue = new LinkedList<JSONObject>();
public static LinkedList<JSONObject> memory_queue = new LinkedList<JSONObject>();
public static LinkedList<JSONObject> disk_queue = new LinkedList<JSONObject>();
@Autowired
private IUsage_historyRsClient usage_historyRsClient;
public void findUsage_history_timer() {
try {
if (usage_historyRsClient == null) {
usage_historyRsClient = Usage_historyListener.usage_historyRsClient;
}
String returnStr = usage_historyRsClient.findUsage_history();
// String url = "https://172.16.25.163/" + "/usage_api/get_hypervisor_statistics";
// String returnStr = new String(HttpsUtil.getMethod(url));
logger.info("usage_historyRsClient=" + returnStr);
returnStr = returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
float vcpus_used = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("vcpus_used");
float vcpus = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("vcpus");
float vcpus_usage_temp = vcpus_used / vcpus;
BigDecimal vcpus_usage_big = new BigDecimal(vcpus_usage_temp);
float vcpus_usage = vcpus_usage_big.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
float memory_mb_used = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("memory_mb_used");
float memory_mb = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("memory_mb");
float memory_usage_temp = memory_mb_used / memory_mb;
BigDecimal memory_usage_big = new BigDecimal(memory_usage_temp);
float memory_usage = memory_usage_big.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
float local_gb_used = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("local_gb_used");
float local_gb = jsonObj.getJSONObject("history_usage").getJSONObject("hypervisor_statistics").getInt("local_gb");
float disk_usage_temp = local_gb_used / local_gb;
BigDecimal disk_usage_big = new BigDecimal(disk_usage_temp);
float disk_usage = disk_usage_big.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(ExceptionCode.FIND_Usage_History_FAILED);
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
JSONObject vcpusObject = new JSONObject();
vcpusObject.put("y", vcpus_usage);
vcpusObject.put("x", df.format(date));
vcpus_queue.addLast(vcpusObject);
if (vcpus_queue.size() > 15) {
vcpus_queue.removeFirst();
}
JSONObject memoryObject = new JSONObject();
memoryObject.put("y", memory_usage);
memoryObject.put("x", df.format(date));
memory_queue.addLast(memoryObject);
if (memory_queue.size() > 15) {
memory_queue.removeFirst();
}
JSONObject diskObject = new JSONObject();
diskObject.put("y", disk_usage);
diskObject.put("x", df.format(date));
disk_queue.addLast(diskObject);
if (disk_queue.size() > 15) {
disk_queue.removeFirst();
}
System.out.println("vcpus_queue = " + vcpus_queue);
System.out.println("memory_queue = " + memory_queue);
System.out.println("disk_queue = " + disk_queue);
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_Usage_History_FAILED, ex);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
findUsage_history_timer();
}
}