Hyperic插件开发不完全指南(三)--Mbean插件(一)
1. hyperic JMX插件
作为java为服务端语言的web项目,JMX MBean是监控管理很好的选择。Hyperic可以很容易地mbean集成进来。Hyperic可以自动发现定义好的Mbean服务类型的服务。
JMX监控插件开发步骤
(1) 设计好自己所要监控的业务逻辑,开发Mbean
我们可以借助Spring JMX来开发Mbean。Spring可以将spring bean暴露成model mbean。Spring Jmx可以定制assembler来过滤需要开放的属性和服务,支持annotation的方式。Connector可以更改服务的协议。具体可以参考spring 文档关于JMX一节。
package com.nali.monitor;
import org.apache.log4j.Logger;
public class TestMbean { private static Logger logger = Logger.getLogger(TestMbean.class);
private int avalibility;
private int number;
private int throughput;
public int getAvalibility() { return avalibility; }
public int getNumber() { return number; }
public int getThroughput() { return throughput; }
public boolean start() { logger.info("start"); return true; }
public boolean end() { logger.info("end"); return true; } } |
我们可以通过Mbean观察availability,throughput,number三个属性值的变化,start,end为两个管理(control)操作。
Spring 配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd " default-lazy-init="false"> <context:annotation-config /> <context:component-scan base-package="com.nali.monitor" /> <bean id="testMBean" class="com.nali.monitor.TestMbean"></bean> <bean class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="com.nali.monitor:name=testMbean" value-ref="testMBean" /> </map> </property> </bean> </beans> |
这里直接用了tomcat的mbean server,就不需要在配置一个了。
Tomcat启动配置
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8849 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false |
(2) 用Jconsole 测试,