About Peter Butkovic

Peter is a java developer living in the Munich area. Prototyping is for him the most favourite part of the project work (especially in the phase when nobody really knows if things are really gonna work :) He is also quite influenced by the ideas of open source software. And important things except that? Family, friends and Jesus Christ (the best Friend :)

Apache Ant tasks for JMX access

I wanted to invoke JMX operations from the Ant tasks. However finding usable ant tasks library as well as the usage was rather tricky. So let me share my experience to make things easier for others.

Ant tasks for JMX operations

I decided to follow Tomcat documentation and used ant tasks distributed with tomcat.

Just for the record the usage is not restricted to Tomcat deployed JMX mBeans. For me it worked for java process accessible via JConsole via Remote connection.

Retrieving the library

  1. As I wanted to get the latest version I used maven central repository “search by classname” feature and searched for: org.apache.catalina.ant.jmx.JMXAccessorTask (see the query)
  2. afterwards I went for the tomcat 8 jar file (called tomcat-catalina-ant-8.0.8.jar)
  3. and just copyied the latest available to my $ANT_HOME/lib dir.

Usage

I didn’t have a chance (or motivation?) to check all the tasks available, the full list of tasks available can be seen in the zipped file: org/apache/catalina/ant/jmx/antlib.xml, following were present for me:

<typedef
    name="open"
    classname="org.apache.catalina.ant.jmx.JMXAccessorTask" />
<typedef
    name="set"
    classname="org.apache.catalina.ant.jmx.JMXAccessorSetTask" />
<typedef
    name="get"
    classname="org.apache.catalina.ant.jmx.JMXAccessorGetTask" />
<typedef
    name="invoke"
    classname="org.apache.catalina.ant.jmx.JMXAccessorInvokeTask" />
<typedef
    name="query"
    classname="org.apache.catalina.ant.jmx.JMXAccessorQueryTask" />
<typedef
    name="create"
    classname="org.apache.catalina.ant.jmx.JMXAccessorCreateTask" />
<typedef
    name="unregister"
    classname="org.apache.catalina.ant.jmx.JMXAccessorUnregisterTask" />
<typedef
    name="equals"
    classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition" />
<typedef
    name="condition"
    classname="org.apache.catalina.ant.jmx.JMXAccessorCondition" />

out of these, I gave following a try:

org.apache.catalina.ant.jmx.JMXAccessorTask
org.apache.catalina.ant.jmx.JMXAccessorInvokeTask
org.apache.catalina.ant.jmx.JMXAccessorQueryTask

For the demonstration purposes I’m using Glassfish 4.0.

Example: Listing JMX MBeans

Let’s assume we want to retrieve the MBean by name (namely: java.lang:type=Memory). Please note username and password were not required for access (otherwise they should be specified via respective properties).

Noteworthy here is the resultproperty, which could hold array from which we could get a name. So having in ant build script:

<typedef 
    name="jmxQuery"
    classname="org.apache.catalina.ant.jmx.JMXAccessorQueryTask" />

<jmxQuery
    host="localhost"
    port="8686"
    echo="true"
    name="java.lang:type=Memory"
    resultproperty="memory" />

<echo>Retrieved MBeans count: ${memory.Length}</echo>
<echo>The 1.st one has name: ${memory.0.Name}</echo>

results for me in following output:

[jmxQuery] memory.Length=1
 [jmxQuery] memory.0.Name=java.lang:type=Memory
     [echo] Retrieved MBeans count: 1
     [echo] The 1.st one has name: java.lang:type=Memory

Example: Invoking operation via JMX

Here is a 2 step approach required:

  1. connect to remote server via JMX and afterwards
  2. invoke the operation on the particular MBean.

For demonstration purposes, let’s assume we want to call garbage collection (via invoking operation: gc() on MBean named: java.lang:type=Memory)

Sample ant build file chunk does the job (please note ref property value that has to be the same across these 2 tasks):

<typedef 
    name="jmxOpen"
    classname="org.apache.catalina.ant.jmx.JMXAccessorTask" />

<typedef 
    name="jmxInvoke"
    classname="org.apache.catalina.ant.jmx.JMXAccessorInvokeTask" />

<jmxOpen
    host="localhost"
    port="8686"
    ref="glassfish"
    failOnError="true" />

<jmxInvoke
    name="java.lang:type=Memory"
    operation="gc"
    echo="true"
    delimiter=" "
    failOnError="true"
    ref="glassfish" />

Further information

For more details, I recommend reading the official Tomcat documentation as well as Javadocs.

Reference: Apache Ant tasks for JMX access from our JCG partner Peter Butkovic at the pb’s blog about life and IT blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


× eight = 16



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books