Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

EasyMX - An alternative to JMX

December 10, 2011 AT 12:00 AM
  • submit to reddit

Monitoring and getting information about the internals of application is always crucial. For this reason Java platform has introduced JMX infrastructure long time ago.
It allows you to access detailed information about the application in a standard way. Here, I'll show you another way of getting and displaying the details about the application. I prefer this way as it seems to be easier to use than JMX. I have opened a project called "EasyMX" in SourceForge, you can access codes at
http://easymx.sourceforge.net.

EasyMX uses Rest and Eclipse plugins. The management services are developed using Rest and accessed by the Eclipse plugin. I will explain it step by step below. First you have to write a service to get information about the application, say it is a time service displaying current server time. You have to configure Rest first.

in web.xml, add a servlet to configure Rest;

	<servlet>
		<servlet-name>Jersey REST Service</servlet-name>
		<servlet-class>
  			com.sun.jersey.spi.container.servlet.ServletContainer
		</servlet-class>

		<init-param>
    		<param-name>com.sun.jersey.config.property.packages</param-name>
    		<param-value>net.sourceforge.easymx.web.rest</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>	
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey REST Service</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping> 

 

It accepts all Rest implementations under the package "net.sourceforge.easymx.web.rest"

Second add an class, e.g SysInfo, under this package and write a service to get current time of the server;

@Path("/sysInfo")
public class SysInfo {
	
	@GET
	@Path("getTime")
	public String getTime() {
		return new Date().toString();
	}
}

 

Make sure that you can see time information by browsing to http://localhost:8080/easymx/rest/sysInfo/getTime. You should see something like this : Fri Nov 25 17:13:47 EET 2011
Now, the time service completed and you can see it using the Eclipse plugin also.

EasyMx has an Eclipse plugin which you can install just like other plugins by copying it to plugins folder of Eclipse install path. After copying and restarting the
Eclipse, choose EasyMX/Servers List from "Show View" menu item. You should see similar screen as below;

 

First thing you have to do is to add an server. To do that, right click on "EasyMX Servers" node and click "Add server". You should enter server name and uri of
rest services. In this example you should enter;
Server name : Test Server
Address        : http://localhost:8080/easymx/rest

Click "Ok" and server definition is added. 

Now, you should define services in the plugin to access the service you developed on the server. To add the service, right-click on server "Test Server" and click on "Add service". You should see similiar screen as below;

 

Service name      : sysInfo
Context URI         : /sysInfo/getTime
Result type          : text

You enter a name for service and uri you can access. Uri must starts with "/" and points to your Rest time service. Since your server address is http://localhost:8080/easymx/rest" and the service uri is "/sysInfo/getTime", complete address of your service is concatenation of these two addresses, "http://localhost:8080/easymx/rest/sysInfo/getTime". You set "text" as result type as you just return a simple string from time service. It can be "tabular" also, I will show the usage of that result type later.


To invoke the service, double click on the service name and a dialog screen is displayed. First part of the dialog allows entering parameters of the service. For this service, there is no need to enter any parameter so this part is empty. Second part is includes  button to invoke the service and an area to show the status of service invocation. Just click on "Invoke service" button, you should see server time;


 

Second service lists the properties of a Java application. The service implementation;

	@GET
	@Produces(MediaType.TEXT_XML)
	@Path("getSysProps")
	public String getSysProps() {
		Properties props = System.getProperties();
		
		List<Map<String,String>> propsNormalized = new ArrayList<Map<String,String>>();
		
		
		for(Map.Entry<Object, Object> entry:props.entrySet()) {
			Map<String, String> val  = new HashMap<String, String>();
			
			String name = "";
			if( entry.getKey() != null ) {
				name = entry.getKey().toString();
			}
			
			String value = "";
			
			if ( entry.getValue() != null) {
				value = entry.getValue().toString();
			}
			
			val.put("name", name);
			val.put("value", value);
			propsNormalized.add(val);
			
		}
		
		Collections.sort(propsNormalized, new Comparator<Map<String, String>>() {
			
			public int compare(Map<String, String> o1, Map<String, String> o2) {
				return o1.get("name").compareTo(o2.get("name"));
			}
		});
		
		return new EasyMxUtil().generateXml(propsNormalized, "name", "value");
	}

 This service lists the properties and its values in a tabular format. Data is received in xml format conforming the standard below;

<result>
	<row> <propertyName value="propertyValue"/>  </row>
</result

EasyMxUtil class is used to generate the xml format. You don't need to use that class, generating xml data in proper format is enough.
Now it comes to defining this new service in the plugin. Right-click on the server and click on "Add service". Add the values below;

Service name : sysInfo-getSysProps
Context URI    : /sysInfo/getSysProps
Result type     : tabular

Since the data is in xml format you should set the result type as "tabular". When you click on "Invoke service" the similar screen like below displayed;

You can see the properties and values of you service result as listed tabularly. You can also view the raw xml formatted data returned. If you click any
cell of the table you see it is copied to area below.

Another feature of the plugin is passing parameters to the service. To give an example for this, a service to search a property by name is developed.

Service implementation;

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("getSysPropByName")
	public String getSysPropByName(@QueryParam("name") String name) {
		if ( name == null ) {
			return "";
		}
		
		Properties props = System.getProperties();
		
		return props.getProperty(name);
	}

Service definition in plugin;

Service name : sysInfo-getSysProps
Context URI    : /sysInfo/getSysPropByName
Result type     : text

 

You can add parameters by right-clicking on parameters(name-value pairs) area and selecting "Insert New Property". Enter "name" as name field, and "user.home" as value.

You should see similar screen;

 

All information related to the servers and services is stored in file ".easymx-servers.xml" under user home folder.

 Another screenshot listing the services;

 

There are some drawbacks of using this approach. First it has no notification mechanism as in JMX and no security issues considered. But it may be a different way of accessing detailed information about your application. You can download files from https://sourceforge.net/projects/easymx/files/1.0.0/

 

 

Article Type: 
Opinion/Editorial
Article Resources: 

Comments

Jonathan Fisher replied on Sat, 2011/12/10 - 11:52am

JMX is one of the golden parts about Java that separate it from the rest of the language hype. JMX2.0 would take this even farther, but everyone wants to make Java more like ruby and scala by adding stupid syntax features, rather than develop on it's existing strengths.

Andrew Gilmartin replied on Sun, 2011/12/11 - 11:44am

An advantages to using JMX is that the JMX client gets to the service via a different network path than the users. When the service is running well the path taken does not matter much. However, it is often the case that the main service path becomes inaccessible under adverse conditions. Your HTTP requests are not being serviced before timeouts kick in, for example. And, consequently, your monitoring is also inaccessible. JMX clients use RMI or direct socket pathways to connect to the service and so the JMX client can continue to moniter and manage the service.

As Mr Fisher says, JMX is one of the "golden parts" of the Java ecosystem. (JBose was built on top of it.) Current JMX coding practices are more sophisticated than in the early days. The "MBean" and "MXBean" interface suffix continue to be support for quick and dirty monitoring and publishing. And for those that with lots of monitoring and management touchpoints we too use sophisticated annotations processing to turn existing code into a touchpoint.

Roland Huß replied on Sun, 2011/12/11 - 12:16pm

It's true that RMI might provide an alternative network path dedicated for monitoring only. Nevertheless the choice of a Java-only protocol stack as the only mandatory JSR-160 connector is a real pain for all those monitoring tools out there which are everything but Java. Since JSR-262 (WS connector for JMX) is dead, alternative approaches like EasyMX or Jolokia are valuable additions to the the (deep-sleeping) standard.

And BTW, there are means to open a second HTTP channel dedicated for monitoring as well. See "Putting jmx4perl on the fast lane for Tomcat" for a suitable Tomcat setup.

Ash Mughal replied on Wed, 2012/01/25 - 7:12pm

I have started learning JMX and read another post on this site about JMX. I found that post very useful. What is the major and key difference between JMX and EasyMX. Is EasyMX is a wrapper on JMX or some alternate solution.

Also which one is the best to use? Should I switch on to EasyMX?

new java

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.