JMX NOTIFICATION

xiaoxiao2021-03-06  40

Communication between MBeans is indispensable, and Notification has played the role of communication bridges between MBeans. JMX Notification Model and Java Event Model are similar to the transfer of some important information, status, and data change to the Notification Listener. To make resources easier to manage.

JMX Notification consists of four parts:

1, Notification

2, Notification Broadcast

3, NOTIFICATION Listerner

4, Notification Filter

Let's illustrate the implementation of the Notification through a simple HelloWorld example.

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 GRETING;

Public HelloWorld ()

{

THIS.GREETING = "Hello World! I am A standard mbean";

}

Public HelloWorld (String Greeting)

{

THIS.GREETING = Greeting;

}

Public void setgreeting (String Greeting)

{// Change Properties Here you can add notification as long as you change properties.

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 The structure of the NOTIFICATION is more troublesome, I don't know what the parameters represent what it means. You can look at Sun's API on JMX, here you can see the NOTIFICATION structure

1, Notification (String Type, Object Source, Long Sequencenumber)

2, Notification (String Type, Object Source, Long TimeStamp)

3, Notification (String Type, Object Source, Long Sequenceamp, String Message) 4, Notification (String Type, Object Source, Long Sequencenumber, String Message)

Type is used to indicate notifications, Source is the MBean generated by the notification, SequenceNumber is the serial number in a series of notifications, and timestamp is the time created, Message is a specific notification 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 is the default port can not be set

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)

{

// You can see the parameters of the Notification structure here.

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 ();

}

}

Ok, you can run, open your browser to enter http: // localhost: 9092 and then find list of registered mbeans by Domain:

HelloAgent

Name = helloworld1 // is here. Of course not here, click (on the browser)

Name = HTMLADAPTER, Port = 9092

JMIMPLEMENTATION

Type = mbeanserverdelegate

Then find the properties of MBean

List of mbean attributes:

Change the value value, press Apply to get it (if the next page is successful). What is the console output? By the way, the value of Access has three RW (Read Write), Ro (writonly), WO (WriteOnly) Reference: http://www.huihoo.com/java/jmx/jmx2.html and jmxremote-1_0_1-bin / doc /

转载请注明原文地址:https://www.9cbs.com/read-76057.html

New Post(0)