MBean

Spring MBean的官方文档路径

http://docs.spring.io/spring/docs/3.0.x/reference/jmx.html

参考文档

http://lionbule.iteye.com/blog/771727

 

1, spring bean 文件中加命名空间

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

2,配置mbeanServer

<context:mbean-server id="mbeanServer" />

<context:mbean-export server="mbeanServer" />\

3, 代码中配置

  1. @ManagedResource(objectName="bean:name=lionbuleTest", description="My Managed Bean")  
  2. public class AnnotationTestMBean{  
  3.     private String name;  
  4.     private int age;  
  5.       
  6.     @ManagedAttribute(description="The Name Attribute")  
  7.     public void setName(String name) {  
  8.         this.name = name;  
  9.     }  
  10.   
  11.     @ManagedAttribute()  
  12.     public String getName() {  
  13.         return name;  
  14.     }    

     
  15.     public int getAge() {  
  16.         return age;  
  17.     }  
  18.     public void setAge(int age) {  
  19.         this.age = age;  
  20.     }  
  21.       
  22.     @ManagedOperation(description="Add two numbers")  
  23.     @ManagedOperationParameters({  
  24.     @ManagedOperationParameter(name = "x", description = "The first number"),  
  25.     @ManagedOperationParameter(name = "y", description = "The second number")})  
  26.     public int add_1(int x, int y) {  
  27.         return x + y;  
  28.     }  
  29.   
  30.     @ManagedOperation  
  31.     public int add_2(int x, int y){  
  32.         return x + y;  
  33.     }  
  34.     
  35.     public void dontExposeMe() {  
  36.         throw new RuntimeException();  
  37.     }  
  38.   
  39. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMX(Java Management Extensions)是Java平台的一个管理和监控API,可以让开发人员开发Mbean(Managed Bean)来管理和监控Java应用程序。Mbean是一种Java对象,通过JMX可以公开其属性和方法,以便对其进行管理和监控。 下面是一个简单的示例,演示如何使用JMX开发一个Mbean: 1. 定义一个实现了DynamicMBean接口的类,该类表示一个Mbean: ```java public class SimpleMBean implements DynamicMBean { private String name; public SimpleMBean(String name) { this.name = name; } @Override public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { if (attribute.equals("Name")) { return name; } else { throw new AttributeNotFoundException(attribute); } } @Override public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { throw new AttributeNotFoundException(attribute.getName()); } @Override public AttributeList getAttributes(String[] attributes) { AttributeList result = new AttributeList(); for (String attr : attributes) { try { result.add(new Attribute(attr, getAttribute(attr))); } catch (Exception ex) { // ignore } } return result; } @Override public AttributeList setAttributes(AttributeList attributes) { return new AttributeList(); } @Override public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { throw new ReflectionException(new NoSuchMethodException(actionName)); } @Override public MBeanInfo getMBeanInfo() { MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[] { new MBeanAttributeInfo("Name", "java.lang.String", "name", true, false, false) }; MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[] { new MBeanConstructorInfo("SimpleMBean()", "Default constructor", new MBeanParameterInfo[0]) }; return new MBeanInfo(getClass().getName(), "SimpleMBean", attrs, ctors, null, null); } } ``` 2. 创建一个MBeanServer对象,并将Mbean注册到MBeanServer中: ```java MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.example:type=SimpleMBean"); SimpleMBean mbean = new SimpleMBean("hello"); mbs.registerMBean(mbean, name); ``` 3. 使用JConsole或其他JMX客户端连接到MBeanServer,可以查看和修改Mbean的属性。 这是一个非常简单的示例,实际上,Mbean可以包含更复杂的逻辑和更多的属性。Mbean也可以使用注解来简化Mbean的开发和管理。JMX是一个强大的工具,可以帮助开发人员监控和管理Java应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值