About Me

My photo
Rohit is an investor, startup advisor and an Application Modernization Scale Specialist working at Google.

Monday, June 29, 2015

Hey Coud Foundry Where my MBeans @ ?

If you have pushed a java application to Cloud Foundry you have experienced the pain of NOT having an accessible Mbean server.  Cloud Foundry by default opens only ONE ingress port over the HTTP protocol.

The Java Management Extensions Instrument and Agent Specification defines the concept of connectors. A connector makes a Java Management Extensions (JMX) technology MBean server accessible to remote Java technology-based clients. The client end of a connector exports essentially the same interface as the MBean server.

Typical connectors connect to the MBean server over RMI. Herein lies the problem because the RMI-IIOP port is NOT open on the container running the CF app. Therefore an alternative mechanism is needed that allows a client to talk to the Mbean server over HTTP.

This is where Jolokia comes to the rescue. Jolokia is an agent based approach for remote JMX access. It is an alternative to standard JSR 160 connectors. The communication between client and agent goes over HTTP (either GET or POST), where the request and response payload is represented in JSON.


The best way to incorporate Jolokia in your app is by incorporating the Jolokia agent into the application by adding the following to the pom.xml or build.gradle

//Jolokia
runtime 'org.jolokia:jolokia-core:1.3.1'
runtime 'org.jolokia:jolokia-jsr160:1.3.1'

The Agent Servlet is also listed in the web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">

  <display-name>Spring-Music</display-name>
  
  <servlet>
    <servlet-name>jolokia-agent</servlet-name>
    <servlet-class>org.jolokia.http.AgentServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jolokia-agent</servlet-name>
    <url-pattern>/jolokia/*</url-pattern>
  </servlet-mapping>

</web-app>

Thereafter rebuild the app and then the metrics can be accessed over the browser using requests like these: 

Request:
http://spring-music-nonhesitant-cornetist.cfapps.pez.pivotal.io/jolokia/read/java.lang:type=Memory/HeapMemoryUsage

Response
{
  • request
    {
    • mbean"java.lang:type=Memory",
    • attribute"HeapMemoryUsage",
    • type"read"
    },
  • value
    {
    • init805306368,
    • committed771751936,
    • max771751936,
    • used203904200
    },
  • timestamp1435594883,
  • status200
}


The JMX metrics can then be easily incorporated into a dashboard. In addition to Java there are other Jolokia clients available as well. The Javascript Jolokia client is full-featured and supports charting with Cubism, a D3.js plugin for plotting time series data.

Please note that some of the commercial app servers like Glassfish, WebSphere Liberty Profile, WebLogic provide JMX HTTP connectors.  The advantage of using these connectors over Jolokia is that , existing JMXClients like Jconsole and VisualVM plugins work as-is out of the box with JMX over HTTP. There is no need to do the querying for individual request/responses or write custom code. You can leverage the existing ecosystem of JMX client tooling.


No comments:

Post a Comment