扩展 Spring 的 JMX 支持(2)(3)

2008-02-23 08:12:15来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折



清单 8. SpringJmxClient

 

     

package com.claudeduguay.jmx.demo.client; 



import java.util.*; 



import javax.management.*; 

import javax.management.remote.*; 



public class SpringJmxClient implements NotificationListener 

{ 

  public void handleNotification( 

    Notification notification, Object handback)  

  { 

    System.out.println( 

      "Notification: "   notification.getMessage()); 

  } 

   

  public static void main(String[] args) 

    throws Exception 

  { 

    SpringJmxClient listener = new SpringJmxClient(); 

     

    String address = 

      "service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi"; 

    JMXServiceURL serviceURL = new JMXServiceURL(address); 

    Map<String,Object> environment = null; 

    JMXConnector connector = 

      JMXConnectorFactory.connect(serviceURL, environment); 

    MBeanServerConnection mBeanConnection = 

      connector.getMBeanServerConnection(); 

     

    ObjectName exampleServiceName = 

      ObjectName.getInstance("Services:name=ExampleService"); 

    mBeanConnection.addNotificationListener( 

      exampleServiceName, listener, null, null); 

     

    mBeanConnection.invoke( 

      exampleServiceName, "startService", null, null); 

    mBeanConnection.setAttribute(exampleServiceName, 

      new Attribute("propertyValue", "new value")); 

    System.out.println(mBeanConnection.getAttribute( 

      exampleServiceName, "propertyValue")); 

    mBeanConnection.invoke( 

      exampleServiceName, "stopService", null, null); 

  } 

}  


  由于 HTTP 适配器也是可用的,可以试着使用 MX4J (通过一个到端口 8080 的浏览器连接)管理同样的方法和属性。如果同时让客户机代码运行,那么也会看到这些操作的通知。

  结束语

  在本文中,我展示了如何扩展 Spring 的 JMX 支持以满足应用程序的特定需求。在这里,我使用了 Spring 的基于容器的体系结构和 AOP 框架来为 JMX 方法和属性增加通知事件。当然,我只触及到了 Spring JMX 能力的皮毛。还可以有许多其他扩展,Spring 和 JMX 都是很大的主题,每一个都值得进一步研究。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:扩展 Spring 的 JMX 支持(1)

下一篇:JSP Ajax 添加、删除多选框