The JMX technology provides a simple, standard way of managing resources such as applications, devices, and services. Because the JMX technology is dynamic, you can use it to monitor and manage resources as they are created, installed and implemented. You can also use the JMX technology to monitor and manage the Java Virtual Machine (Java VM).
The Java Management Extensions (JMX) technology is a standard part of the Java Platform, Standard Edition (Java SE platform). The JMX technology was added to the platform in the Java 2 Platform, Standard Edition (J2SE) 5.0 release.
The JMX specification defines the architecture, design patterns, APIs, and services in the Java programming language for management and monitoring of applications and networks.
Using the JMX technology, a given resource is instrumented by one or more Java objects known as Managed Beans, or MBeans. These MBeans are registered in a core-managed object server, known as an MBean server. The MBean server acts as a management agent and can run on most devices that have been enabled for the Java programming language.
The specifications define JMX agents that you use to manage any resources that have been correctly configured for management. A JMX agent consists of an MBean server, in which MBeans are registered, and a set of services for handling the MBeans. In this way, JMX agents directly control resources and make them available to remote management applications.
Architecture of the JMX Technology:
The JMX technology can be divided into three levels, as follows:
Instrumentation : Contains MBeans and their manageable resources.
JMX agent : Contains the JMX agents used to exposed the MBeans.
Remote management : Contains components that enable management
applications to communicate with JMX agents.
Standard MBeans :
A standard MBean is defined by writing a Java interface called
MBean Interface:
package com.example;
public interface HelloWorldMBean{
public void setGreeting(String greetings);
public String getGreeting();
public void print();
}
By convention, an MBean interface takes the name of the Java class that implements it, with the suffix MBean added. In this case, the interface is called HelloWorldMBean. The HelloWorld class that implements this interface is described in the next section.
According to the JMX specification, an MBean interface consists of named and typed attributes that are readable and possibly writable, in addition to the named and typed operations that can be invoked by the applications that are managed by the MBean.
MBean Implementation
The HelloWorld Java class that follows implements the HelloWorldMBean MBean interface:
package com.example;
public class HelloWorld implements HelloWorldMBean {
String greetings = null;
public HelloWorld(){
this.greeting = "Hello World! I am a Standard MBean";
}
public HelloWorld(String greeting){
this.greeting = greeting;
}
public void setGreeting(String greetings){
this.greetings = greetings;
}
public String getGreeting(){
return greetings;
}
public void print(){
System.out.println("Wish you " + greetings);
}
And with that, we have created first MBean. Now, in order to test the MBean, you need to create a JMX agent to contain it. The next section discusses the creating of the HelloAgent class. After creating your agent, you can begin using the MBean.
Creating the JMX Agent:
Now that we have the first MBean we need to make it avialable for use. To do so, you must register it in a JMX Agent. We will create the HelloWorldAgent class, which is a simple JMX agent class.
As described before, JMX agents are JMX components in the Agent layer of JMX and are containers for MBeans.
The Agent class performs 3 tasks.
- It creates an MBeanServer instance to contain MBeans.
- It creates an adapter to handle connections from clients .(In our case we will use HTML Adapter.
- It registers a new instance of the (HelloWorld) MBean.
Writing the HelloAgent class
package com.examples;
import javax.management.*;
import com.sun.jdmk.com.*;
public class HelloAgent{
public MBeanServer mbs = null;
public HelloAgent(){
mbs = MBeanServerFactory.createMBeanServer("HelloAgent");
HtmlAdaptorServer adaptor = new HtmlAdaptorServer();
HelloWorld hw = new HelloWorld();
ObjectName adapterName = null;
ObjectName helloWorldName = null;
try{
helloWorldName = new ObjectName("HelloWorldAgent:name=helloWorld");
mbs.registerMBean(hw,helloWorldName);
adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092");
adaptor.setPort(9092);
mbs.registerMBean(adaptor,adapterName);
}catch(Exception ex){
e.printStackTrace();
}
}
public static void main(String args[]){
HelloAgent agent = new HelloAgent();
System.out.println(" Agent is started");
}
}
Now that the agent is running and the MBean and the HtmlAdapter is regitered to it, it is exposed for invocation through http request. Using your webbrowser, type http://localhost:9092.You will be listed with all the MBean registered and running in local host at port 9092.
You can invoke the methods on HelloWorld directly from the browser.
JMX Notifications:
JMX notifications are Java objects used to send information from MBean and agents to other objects that have registered to recieve them.Objects interested in recieving events are notification listeners - they implement the javax.management.NotificationListener interface.
Adding Notification to HelloWorld MBean
For the HelloWorld MBean to send notifications, it needs to allow objects interested in receiving notifications to register them. JMX supports 2 mechanisms for MBeans to provide listeners to register for notifications:
- Implement the javax.management.NotificationBroadcaster interface.
- Extend the javax.management.NotificationBroadcasterSupport class.
Let us change our HelloWorld MBean to support for notificatoins extending NotificationBroadcasterSupport class.
package com.example;
public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
String greetings = null;
public HelloWorld(){
this.greeting = "Hello World! I am a Standard MBean";
}
public HelloWorld(String greeting){
this.greeting = greeting;
}
public void setGreeting(String greetings){
this.greetings = greetings;
Notification notification = new Notification(" test notification" ,this, -1,System.currentMillis (),greeting); // creating the notification object.
sendNotification(notification); // sending the notification
}
public String getGreeting(){
return greetings;
}
public void print(){
System.out.println("Wish you " + greetings);
}
}
Creating the NotificatoinListener class : HelloWorldListener.java
import javax.management.*;
public class HelloWorldListener implements NotificationListener{
public HelloWorldListener(){
}
public void handleNotification(Notification notif,Object handle){
System.out.println("Receiving notifications .....);
System.out.println(notif.getType());
System.out.println(notif.getMessage());
}
}
Enabling the Agent to register Listeners to send notifications:
package com.examples;
import javax.management.*;
import com.sun.jdmk.com.*;
public class HelloAgent{
public MBeanServer mbs = null;
public HelloAgent(){
mbs = MBeanServerFactory.createMBeanServer("HelloAgent");
HtmlAdaptorServer adaptor = new HtmlAdaptorServer();
HelloWorld hw = new HelloWorld();
ObjectName adapterName = null;
ObjectName helloWorldName = null;
try{
helloWorldName = new ObjectName("HelloWorldAgent:name=helloWorld");
hw.addNotificatioListener(new HelloWorldListener(),null,null); //enabling listeners
mbs.registerMBean(hw,helloWorldName);
adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092");
adaptor.setPort(9092);
mbs.registerMBean(adaptor,adapterName);
}catch(Exception ex){
e.printStackTrace();
}
}
public static void main(String args[]){
HelloAgent agent = new HelloAgent();
System.out.println(" Agent is started");
}
}
Run we have added the capability of listening notificatoins when property of HelloWorld changes. Using your browser navigate to http://localhost:9092/ and invoke the setGreetings method. In the console of your program running HelloWorldAgent, you would see the notifications messages.
Summary :
This article will help to understand the basics of JMX and standard MBean with a working example. Other than StandardMBean we also have :
Dynamic MBeans
Open MBeans
Model MBeans
MXBeans
We will see in later articles more upon the other types of MBean.
If you have any questions/suggestions please mail at pritam.dewan@gmail.com
-----------------------------------***----------------------------------
1 comment:
Hopefully this will benefit someone.
Post a Comment