JMX Architecture & "Hello Word" the JMX way

JMX Architecture Overview:

JMX technology provides a standard API for management and monitoring of resources. As http://kylinsoong.iteye.com/admin/blogs/794995  Part 3 depicted, the JMX architecture can be broken down into three levels: Instrumentation, Agent, Remote Management, As the following figure described:

      Instrumentation Layer contains MBeans representing their manageable resources, such as applications, devices, or services, are instrumented using Java objects called Managed Beans (MBeans). MBeans expose their management interfaces, composed of attributes and operations, through a JMX agent for remote management and monitoring;

      Agent Layer is the main component of a JMX agent is the MBean server. This is a core managed object server in which MBeans are registered. A JMX agent also includes a set of services for handling MBeans. JMX agents directly control resources and make them available to remote management agents;

      Distributed Layer also named Remote Management layer, contain adapters and connectors make a JMX agent accessible from remote management applications outside the agent’s Java Virtual Machine (Java VM).

HelloWorld JMX way

1. Complete Instrumentation layer dev

      The first step in developing the HelloWorld MBean is to write its Java interface. The interface declares three methods: one getter, one setter, and an additional method for printing the HelloWorld MBean’s greeting. Code view:

public interface HelloWorldMBean {
	public void setGreeting( String greeting );
	public String getGreeting();
	public void printGreeting();
}

 Before we start HelloWroldMBean implemetation, we introduce JMX notification first, notification  is the most imprtant feature of JMX, any managment system need notification mechanism, to add notification to our Helloworld mbean we need to extends the avax.management.NotificationBroadcasterSupport. Our MBean class:

public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
	
	private String greeting = null;

	public HelloWorld() {
		this.greeting = "Hello World! I am a Standard MBean";
	}

	public HelloWorld(String greeting) {
		this.greeting = greeting;
	}

	public void setGreeting(String greeting) {
		this.greeting = greeting;
		Notification notification = new Notification("jmx.demo.helloWorld.test"
													, this
													, -1
													, System.currentTimeMillis()
													, greeting );
		sendNotification(notification);
	}

	public String getGreeting() {
		return greeting;
	}

	public void printGreeting() {
		System.out.println(greeting);
	}
}

2 Complete Agent Code

      As step 1, we have our HelloWorldMbean, we need to make it available to use, to do so we must register it in a JMX Agent. Therefore we create HelloAgent class JMX agents are JMX components in the agent layer of JMX and are the containers for MBeans. The HelloAgent class performs 4 important tasks:

      1> It creates an MBeanServer instance to contain MBeans.

      2> It creates an HTML adapter to handle connections from HTML clients.

      3> It registers a new instance of the HelloWorld MBean.

      4> It handles the notification that HelloWorldMBean sent.

public class HelloAgent implements NotificationListener {

	private MBeanServer mbs = null;
	
	public HelloAgent() {
//		mbs = MBeanServerFactory.createMBeanServer("jmx.demo.HelloAgent");
		mbs = ManagementFactory.getPlatformMBeanServer();
		
		HtmlAdaptorServer adapter = new HtmlAdaptorServer();
		
		HelloWorld hw = new HelloWorld();
		
		ObjectName adapterName = null;
		ObjectName helloWorldName = null;
		
		try {
			helloWorldName = new ObjectName( "jmx.demo.HelloAgent:type=helloWorld,name=helloWorld" );
			mbs.registerMBean( hw, helloWorldName );
			hw.addNotificationListener(this, null, null);
			
			adapterName = new ObjectName("jmx.demo.HelloAgent:type=htmlAdapter,name=htmlAdapter,port=8000");
			adapter.setPort(8000);
			mbs.registerMBean( adapter, adapterName );
			adapter.start();
		}  catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]) throws InterruptedException {
		System.out.println("HelloAgent is running");
		HelloAgent agent = new HelloAgent();
		Thread.sleep(Long.MAX_VALUE);
	}

	public void handleNotification(Notification notification, Object handback) {
		System.out.println( "Receiving notification..." );
		System.out.println( notification.getType() );
		System.out.println( notification.getMessage() );
	}
}

 3. Compete Distibuted layer Code

      In this simple HelloWorld example we only Use  HTML adapter to implement remote management, however, RMI connector implement remote management is more popular, I will explore into it in the later blogs. In this blog we will show Html adapter implement remote management in Test section.

HelloWorld Test

      After starting the main method, we can through 2 methods test and manage HelloWrold

Method one:

      Start a new command line, input 'jconsole' the following widget came out:



 Clicking HelloAgent then you will get into Jconsole control interface and find both HelloWorld and htmlAdapter selection folder bar in the left menu tree:



 Clicking reference method or change the attributes, the corresponding action will take place.

Method two:

      Through http://127.0.0.1:8000/ we could find jmx.demo.HelloAgent from All Agent View List 



 Cliking helloWord we also can find the operation and attributes, through HTML interface we also can triger the response notification event.



 

猜你喜欢

转载自kylinsoong.iteye.com/blog/1214097
JMX