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

自己实现的listener监听器

 
阅读更多
///定时器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();

}

   
}
分享到:
评论

相关推荐

    Listener监听器1

    Listener监听器1 Listener监听器是一种特殊的类,用于监听 web 应用程序中的 ServletContext、HttpSession、ServletRequest 等域对象的创建和销毁事件,以及监听这些域对象中的属性发生修改的事件。Listener监听器...

    listener监听器demo

    标题"listener监听器demo"和描述"listener监听器小例子"暗示我们将探讨的是一个关于`Listener`的示例代码。 首先,让我们从Java GUI编程的角度来看`Listener`。在Swing或AWT中,我们经常使用各种类型的`Listener`,...

    JavaWeb开发技术-Listener监听器.pptx

    在实际开发中,Listener监听器广泛应用于各种场景,例如统计在线用户数量、实现自定义登录验证、管理缓存、记录日志等。理解并熟练掌握监听器的使用,对于提升JavaWeb应用的灵活性和健壮性具有重要意义。

    STRUTS:listener监听器

    ### STRUTS:Listener监听器详解 #### 一、引言 在Java Web开发中,监听器(Listener)是十分重要的组成部分,它们主要用于监听特定事件的发生,并执行相应的处理逻辑。Struts框架作为早期流行的MVC架构之一,充分...

    java监听器的实现和原理详解

    Java监听器的实现和原理详解 Java监听器是一种非常重要的设计模式,在Java中广泛应用于事件驱动编程。监听器模式的主要思想是将事件源和事件处理器分离,使得事件源可以独立于事件处理器,提高了系统的灵活性和可...

    Oracle_LISTENER监听文件参数详

    1. LISTENER:指出一个监听器定义的起始点。它实际上是正被定义的当前监听器的名称。默认的名称是 LISTENER。 2. DESCRIPTION_LIST:描述每个监听位置。 3. DESCRIPTION:描述每个监听位置的详细信息。 4. ...

    JAVALISTENER监听器教程及实例借鉴.pdf

    JAVALISTENER监听器教程及实例借鉴.pdf

    监听器访问计数过滤非法字符

    在IT行业中,监听器(Listener)和过滤器(Filter)是两种非常重要的组件,它们在Web应用程序中扮演着不可或缺的角色。本文将详细讲解如何利用监听器进行访问计数以及通过过滤器来过滤非法字符,以确保系统的安全性...

    java_listener监听器教程及实例.pdf

    java_listener监听器教程及实例.pdf

    Spring Boot的listener(监听器)简单使用实例详解

    "Spring Boot的listener(监听器)简单使用实例详解" 在Spring Boot中,listener(监听器)是一种非常重要的组件,它可以帮助我们在应用程序启动和停止时执行一些特定的任务。今天,我们将详细介绍Spring Boot的...

    JavaWebServlet+JSP+EL表达式+JSTL标签库+Filter过滤器+Listener监听器

    JavaWeb技术是构建基于Java平台的Web应用程序的重要框架,它包括了Servlet、JSP、EL表达式、JSTL标签库、Filter过滤器以及Listener监听器等多个关键组件。这些组件协同工作,使得开发者能够创建动态、交互式的Web...

    Oracle 监听器 Listener资料.docx

    Oracle 监听器 Listener 资料 Oracle 监听器 Listener 是一个重要的数据库服务器组件,在整个 Oracle 体系结构中,扮演着重要的作用。它负责管理 Oracle 数据库和客户端之间的通讯,它在一个特定的网卡端口(默认是...

    Oracle数据库监听器(LISTENER)和本地服务名(Tnsname)配置.docx

    Oracle 数据库监听器(LISTENER)和本地服务名(Tnsname)配置 本文主要介绍 Oracle 数据库监听器(LISTENER)和本地服务名(Tnsname)的配置,旨在帮助读者了解 Oracle 网络连接配置的主要组件和配置方法。 一、监听器...

    Comet4Listener监听器

    java消息推送技术中的监听器类,用于创建和监听通道数据

    关于监听器Listener和过滤器的小demo

    在Java Web开发中,监听器(Listener)和过滤器(Filter)是两个非常重要的概念,它们主要用于增强应用程序的功能和性能。下面将详细讲解这两个概念及其应用。 ### 监听器(Listener) 监听器是实现特定接口的Java...

    监听器Listener

    Spring MVC并未提供自己的监听器接口,但它与标准的Java Servlet API紧密集成,可以利用Servlet API中的监听器来扩展和定制应用程序的行为。 1. **HttpSessionListener** 和 **HttpSessionAttributeListener**: ...

    Spring ApplicationListener监听器用法详解

    本文主要介绍了Spring框架中的ApplicationListener监听器的用法详解,通过示例代码对 listeners 的实现和使用进行了详细的讲解,对大家的学习或者工作具有一定的参考学习价值。 一、什么是ApplicationListener? ...

    过滤器filter和监听器listener的应用总结

    `&lt;listener-class&gt;`属性用于指定监听器的实现类。 2. **常见的监听器类型**: - `ServletRequestListener`:监听请求的创建和销毁。 - `ServletRequestAttributeListener`:监听请求属性的变化。 - `...

    JAVA Servlet监听器listener学习资料

    Java Servlet监听器(Listener)是Java Web开发中的一个重要组件,它们允许开发者在特定事件发生时执行自定义逻辑,而无需直接修改原有的业务代码。监听器是Servlet规范的一部分,通过实现特定的接口,开发者可以...

Global site tag (gtag.js) - Google Analytics