Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
JMX Notification

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

 

Mbean之间的通信是必不可少,Notification就起到了Mbean 之间沟通桥梁的作用。JMX notification model java event model 类似,将一些重要的信息,状态的转变,数据的变更传递给 notification listener。以使资源更容易管理。

 

JMX notification 由四部分组成:

1、  Notification

2、  Notification broadcaster

3、  Notification listerner

4、  Notification filter

 

 

通过简单的HelloWorld例子来说明 Notification的实现过程吧。

 

HelloWorldMBean.java

 

package notification;

 

public interface HelloWorldMBean {

    public void setGreeting(String greeting);

    public String getGreeting();

    public void printGreeting();

}

 

 

HelloWorld.java

 

/*

 * Created on 2004-12-27

 * @author roson

 */

package notification;

 

import java.io.*;

import javax.management.*;

public class HelloWorld extends NotificationBroadcasterSupport

                                          implements HelloWorldMBean

{

    private String greeting;

    public HelloWorld()

    {

        this.greeting = "Hello World! I am a Standard MBean";

    }

 

    public HelloWorld( String greeting )

    {

        this.greeting = greeting;

    }

    public void setGreeting( String greeting )

    {   //更改属性这里增加了notification 只要更改属性就可以看到。

        this.greeting = greeting;

        Notification notification = new Notification(

                "jmxbook.ch2.helloWorld.test"

                this,                                      -1,

                System.currentTimeMillis(),        

                greeting);

                      

        sendNotification( notification );

    }

 

    public String getGreeting()

    {

        return greeting;

    }

 

    public void printGreeting()

    {

        System.out.println( greeting );

    }

   

}

 

 

Notification的结构体一开始是觉的比较麻烦的,不知道各个参数代表什么意思。可以看下 sun 提供的关于jmxapi ,这里看下 notification 的结构体

1、  Notification(String type, Object source, long sequenceNumber)

2、  Notification(String type,Object source,long sequenceNumber,long timeStamp)

3、  Notification(String type,Object source,long sequenceNumber,long timeStamp, String message)

4、 Notification(String type,Object source,long sequenceNumber,String message)

 

type用来标示通知, source为产生通知的MBeansequenceNumber为一系列通知中的序号,timeStamp为通知创建的时间,message为具体的通知消息。

 

HelloAgent.java

 

/*

 * Created on 2004-12-27

 * @author roson

 */

package notification;

 

import javax.management.*;

import com.sun.jdmk.comm.HtmlAdaptorServer;

 

public class HelloAgent implements NotificationListener

{

    private MBeanServer mbs = null;

 

    public HelloAgent ( )

    {

        mbs = MBeanServerFactory.createMBeanServer("HelloWorld Server");

        HtmlAdaptorServer adapter = new HtmlAdaptorServer();

        HelloWorld hw = new HelloWorld();

 

        ObjectName adapterName = null;

        ObjectName helloWorldName = null;

 

        try

        {

            adapter.setPort(9092);   //8082是默认的端口可以不设

            adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092" );

            mbs.registerMBean(adapter,adapterName );

           

            helloWorldName = new ObjectName("HelloAgent:name=helloWorld1" );

            mbs.registerMBean( hw, helloWorldName );

 

            hw.addNotificationListener( this, null, null );

 

        }

        catch( Exception e )

        {

            e.printStackTrace();

        }

        adapter.start();

    }//constructor

 

    public void handleNotification(Notification notif, Object handback )

{

       //可以在这里顺便看下notification 结构体的参数。

        System.out.println( "Receiving notification..." );

        System.out.println( notif.getType());

         System.out.println(notif.getSource());

        System.out.println( notif.getMessage());

    }

 

    public static void main( String args[] )

    {

        System.out.println("Start agent");

        HelloAgent agent = new HelloAgent();

    }

}

 

 

好了可以运行了,打开浏览器 输入 http://localhost:9092 然后在注册 Mbean 的地方找到

 

List of registered MBeans by domain:

 

然后找到Mbean 的属性

 

List of MBean attributes:

Name

Type

Access

Value

Greeting

java.lang.String

RW

 

 

更改Value值,按 Apply ,搞定(如果下一个页面显示成功)。看下Console输出什么呢。
顺便说下 Access的值 有三个 RW (Read Write) ,RO(ReadOnly) ,WO(WriteOnly)







参考 :http://www.huihoo.com/java/jmx/jmx2.html 和 jmxremote-1_0_1-bin\doc\




相关文章

相关软件