Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36278
  • 博文数量: 12
  • 博客积分: 1503
  • 博客等级: 上尉
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-04 12:16
文章分类

全部博文(12)

文章存档

2012年(1)

2011年(3)

2010年(2)

2009年(6)

我的朋友

分类: Java

2009-03-14 10:27:11

   开博已经十天了,还没有怎么发文章,最近有点忙
   最近因为接触了一个新项目,不得不摸索JMX。Java这语言,规范一大堆,以前也没有做过JAVA方面的东西,现在都在从0开始了。
   JMX,是SUN公司提出来的一个管理体系结构,规定了规范、设计模式与接口等,它可以对网络、应用程序、服务及系统资源等进行监控与管理。JMX体系结构主要分为三层:装配层、代理层和分布式服务层。其它基础及介绍性文字在这里就不多介绍,可以在GOOGLE到很详细的资料。现在主要是讨论一下jmx-in-action RMI连接子的用不了的问题(第三章)。
   在学习jmx-in-action的第三章时,你会发现按书本那样做你是无法编译通过的,主要原因就是它里面提供的类RmiConnectonServer及RmiConnectorClient等类已经不被现在的API支持了,所以要换其它的类或是接口来实现。先来看一下RMIClientFactory类:
package jmxbook.ch3;

import javax.management.*;
import com.sun.jdmk.comm.*;
//红色部分是加进来的
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.MBeanServerConnection;
import java.rmi.*;
public class RMIClientFactory
{

  public static MBeanServerConnection getClient()
  {
    /**这块代码就不要了
    RmiConnectorClient client = new RmiConnectorClient();
    RmiConnectorAddress address = new RmiConnectorAddress();
    address.setPort( 2099 );
    System.out.println("\t\tTYPE\t= " + address.getConnectorType ());
    System.out.println("\t\tPORT\t= " + address.getPort());
    System.out.println("\t\tHOST\t= " + address.getHost());
    System.out.println("\t\tSERVER\t= " + address.getName());
     **/
   //这是加进来的代码
   MBeanServerConnection mbsc = null;
   
    try
    {
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2099/server");  //JMX连接子服务端的地址
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null); //创建到服务端的连接
     mbsc = jmxc.getMBeanServerConnection();  获得代表远程MBean server的一个MBeanServerConnection 对象
//client.connect( address );
    }
    catch( Exception e )
    {
      ExceptionUtil.printException( e );
    }

    return mbsc;
  }

}

在JMXBookAgent类中也加入
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.MBeanServerConnection;
然后
修改startRMIConnector()方法为:

  protected void startRMIConnector()
  {
    //MBeanServerConnection connector = new MBeanServerConnection();
   // ObjectName connectorName = null;

    try
    {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2099/server");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url,null,server);//根据给出的地址创建一个服务端,但并不马启动服务,得调用START方法来启动它。
cs.start();
/*************  这是书本给出的代码,现在已经不支持了   
connector.setPort( 2099 );
      connectorName = new ObjectName( "JMXBookAgent:name=RMIConnector" );
      server.registerMBean( connector, connectorName );
      connector.start();
*****/
    }
    catch(Exception e)
    {
      ExceptionUtil.printException( e );
    }

  }


HelloWorldSetup类也要修改一下

 public HelloWorldSetup()
  {

    try
    {
MBeanServerConnection client = RMIClientFactory.getClient();
ObjectName hwName = new ObjectName("JMXBookAgent:name=helloWorld");
client.createMBean("jmxbook.ch2.HelloWorld",hwName);
client.invoke(hwName,"printGreeting",null,null);  //调用hellowaord MBean里的pirntGreating方法
client.unregisterMBean(hwName);     
/***RmiConnectorClient client = RMIClientFactory.getClient();
      ObjectName hwName = new ObjectName( "JMXBookAgent:name=helloWorld");
      client.createMBean( "jmxbook.ch2.HelloWorld", hwName );
      client.invoke( hwName, "printGreeting", null, null );
****/   
}

最后,编译和运行时注意加入远程API的类库,即jmxremote.jar和jmxri.jar,最后运行就可以看一到以下字样了:(在运行前要运行rmiregistry 2099打开端口服务)
CREATE the agent...
        CREATE the MBeanServer.
Agent is Ready for Service...
运行客服端后:
CREATE the agent...
        CREATE the MBeanServer.
Agent is Ready for Service...
Hello World! I am a Standard MBean.
阅读(2071) | 评论(0) | 转发(0) |
0

上一篇:开博之庆

下一篇:啊 生活

给主人留下些什么吧!~~