DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • From On-Prem to SaaS
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  • The SPACE Framework for Developer Productivity

Trending

  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • From On-Prem to SaaS
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  • The SPACE Framework for Developer Productivity

Apache Ant Tasks for JMX Access

Peter Butkovic user avatar by
Peter Butkovic
·
Jun. 15, 14 · Interview
Like (0)
Save
Tweet
Share
4.23K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<typedefname="open"classname="org.apache.catalina.ant.jmx.JMXAccessorTask"/><typedefname="set"classname="org.apache.catalina.ant.jmx.JMXAccessorSetTask"/><typedefname="get"classname="org.apache.catalina.ant.jmx.JMXAccessorGetTask"/><typedefname="invoke"classname="org.apache.catalina.ant.jmx.JMXAccessorInvokeTask"/><typedefname="query"classname="org.apache.catalina.ant.jmx.JMXAccessorQueryTask"/><typedefname="create"classname="org.apache.catalina.ant.jmx.JMXAccessorCreateTask"/><typedefname="unregister"classname="org.apache.catalina.ant.jmx.JMXAccessorUnregisterTask"/><typedefname="equals"classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"/><typedefname="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:

<typedefname="jmxQuery"classname="org.apache.catalina.ant.jmx.JMXAccessorQueryTask"/><jmxQueryhost="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]RetrievedMBeans count:1[echo]The1.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):

<typedefname="jmxOpen"classname="org.apache.catalina.ant.jmx.JMXAccessorTask"/><typedefname="jmxInvoke"classname="org.apache.catalina.ant.jmx.JMXAccessorInvokeTask"/><jmxOpenhost="localhost"port="8686"ref="glassfish"failOnError="true"/><jmxInvokename="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 asJavadocs.

Task (computing) Apache Ant

Published at DZone with permission of Peter Butkovic, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • From On-Prem to SaaS
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  • The SPACE Framework for Developer Productivity

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: