`
y806839048
  • 浏览: 1107643 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

listener 定时器TimerTask

 
阅读更多
///定时器TimerTask
Usage_history_Timer.java

Usage_historyListener.java

web.xml中配置自己写的也可以
<listener>
         <listener-class>com.certusnet.nfv.mano.vim.usage_history.rest.Usage_historyListener</listener-class>
    </listener>




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); 
}

   
}





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();

}

   
}
分享到:
评论

相关推荐

    定时器的原理及使用

    Java 提供了 `java.util.Timer` 类和 `java.util.TimerTask` 类来实现定时器的功能。 `java.util.Timer` 是一个线程安全的类,它允许开发者安排任务在后台线程中执行一次或周期性地重复执行。创建 `Timer` 实例后,...

    Tomcat中简易定时器的实现

    这个定时器的实现通常涉及使用Java的定时器类`java.util.Timer`和`java.util.TimerTask`。 首先,我们关注`MyTimerTask.java`这个文件,它应该包含了定时任务的逻辑。`MyTimerTask`通常会继承自`java.util....

    java定时器.pdf

    2. **使用监听器(Listener)**:在`web.xml`中配置一个监听器,监听器的初始化方法启动定时器,销毁方法销毁定时器。这种方式更加灵活,因为监听器可以在Web应用程序的整个生命周期中响应特定事件。 以下是一个在...

    java定时器

    Java定时器是Java编程语言中用于执行预定任务的工具,主要通过`java.util.Timer`类和`java.util.TimerTask`类来实现。这些类允许程序员安排在未来某个特定时间或者定期执行的任务,对于需要周期性执行的后台操作非常...

    Java后台定时器代码

    在实际项目中,监听器可以是基于事件驱动的架构,例如Servlet容器中的监听器,或者Spring框架中的ApplicationListener,它们可以在特定事件发生时触发定时任务。 总结起来,Java后台定时器提供了强大的功能,让你...

    JAVA定时器word文档

    总结一下,Java定时器(java.util.Timer)和TimerTask配合使用,可以方便地实现定时执行任务的需求。通过结合Servlet监听器,我们可以确保定时器在Web应用启动时启动,并在应用关闭时停止,确保任务的生命周期与Web...

    java定时器使用汇总.pdf

    总的来说,Java定时器是通过`Timer`和`TimerTask`类实现的,可以方便地在Web环境中进行配置,以实现定时任务的自动化执行。在设计定时任务时,需要考虑到任务的执行频率、任务间的依赖关系以及异常处理,以确保系统...

    java 定时器

    定时器的核心类有两个:`Timer`与`TimerTask`。 - **`Timer`**:提供调度机制,用于调度`TimerTask`。 - **`TimerTask`**:用户自定义的任务类,继承自`java.util.TimerTask`抽象类,并重写`run()`方法来定义具体的...

    Java定时器在Web中的应用.doc

    Java定时器在Web应用中的实现主要涉及到`java.util.Timer`类和`ServletContextListener`接口,它们在Spring、Struts、Hibernate等框架环境下同样适用。本文将详细解释如何使用这两个核心组件来实现在Web环境中定时...

    Java_Web定时器使用

    通过设置定时器的间隔时间,可以在指定的时间间隔后执行预先定义的任务,即`java.util.TimerTask`。 #### 二、定时器工作原理 `java.util.Timer`类负责管理一系列`TimerTask`实例,并在特定的时间点执行这些任务。...

    java定时器的实现

    - **`MyTask`类**:继承自`TimerTask`,重写了`run`方法,该方法将在定时器触发时被执行。在这个例子中,为了简化演示,没有具体实现任务逻辑,而是打印了一条消息。 ##### 3. web.xml 配置 ```xml &lt;listener&gt; ...

    java定时器定时调用任务

    在Servlet容器(如Tomcat)中,可以通过监听器(Listener)或者Servlet的初始化方法来创建和启动定时器,以实现在Web应用启动后自动开始执行定时任务。 此外,Java 5引入了`java.util.concurrent`包,其中的`...

    Java-Web定时器使用.doc

    Java Web定时器是一种在Web应用程序中定期执行特定任务的机制,它主要依赖于Java的`java.util.Timer`类和`java.util.TimerTask`类。在Java Web开发中,定时任务通常用于后台数据处理、定时备份、日志清理等场景。...

    servlet定时器

    在探讨“servlet定时器”的知识点时,我们深入解析如何在Java Web应用中实现定时任务,特别是通过Servlet容器来调度定期执行的任务。这涉及到Java的标准库`java.util.Timer`以及Servlet API中的`...

    23-Listener-源代码.rar

    案例-使用监听器完成定时生日祝福、,一、监听器Listener 二、邮箱服务器。 1.什么是监听器? 2.监听器有哪些? 3.监听三大域对象的创建与销毁的监听器 ...c、任务调度----定时器----Timer/TimerTask

    关于spring中定时器的使用教程

    在Spring框架中,定时任务的实现是通过Spring的定时器组件来完成的,这使得开发者无需深入了解底层定时机制,即可方便地实现定时任务。本文将详细介绍如何在Spring中使用XML配置来实现定时器。 首先,我们需要创建...

    java定时任务Timer和TimerTask使用详解

    `Timer` 类的构造函数通常用于初始化一个定时器对象,如 `new Timer()`。 2. **TimerTask类**: `TimerTask` 是一个抽象类,它代表要由 `Timer` 对象执行的任务。你需要继承 `TimerTask` 并覆盖它的 `run` 方法,...

Global site tag (gtag.js) - Google Analytics