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
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. JMX and Spring - Part 1

JMX and Spring - Part 1

Marco Tedone user avatar by
Marco Tedone
·
Aug. 10, 11 · Interview
Like (0)
Save
Tweet
Share
10.45K Views

Join the DZone community and get the full member experience.

Join For Free

this is the first of three articles which will show how to empower your spring applications with jmx support.

maven configuration

this is the maven pom.xml to setup the code for this example:

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>uk.co.jemos.experiments.jmx</groupid>
  <artifactid>jemos-jmx-experiments</artifactid>
  <version>0.0.1-snapshot</version>
  <name>jemos-jmx-experiments</name>
  <description>jemos jmx experiments</description>
  <build>
    <plugins>
      <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-compiler-plugin</artifactid>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>1.2.16</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context</artifactid>
      <version>3.0.5.release</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-core</artifactid>
      <version>3.0.5.release</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-jmx</artifactid>
      <version>2.0.8</version>
      <scope>compile</scope>
    </dependency>       
    <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-test</artifactid>
        <version>3.0.5.release</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
  </dependencies>
</project>

spring configuration

the spring configuration is pretty straight-forward:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">


    <context:property-placeholder location="classpath:jemos-jmx.properties" />

    <bean id="rmiregistry">
        <property name="port" value="${jemos.jmx.rmi.port}" />
    </bean>

    <bean
        depends-on="rmiregistry">
        <property name="objectname" value="connector:name=rmi" />
        <property name="serviceurl"
            value="service:jmx:rmi://localhost/jndi/rmi://localhost:${jemos.jmx.rmi.port}/jemosjmxconnector" />
        <property name="environment">
            <!-- the following is only valid when the sun jmx implementation is used -->
            <map>
                <entry key="jmx.remote.x.password.file" value="${user.home}/.secure/jmxremote.password" />
                <entry key="jmx.remote.x.access.file" value="${user.home}/.secure/jmxremote.access" />
            </map>
        </property>
    </bean>

</beans>

this configuration, although simple, covers all that's required for the following:

  • start up a jmx server from your spring application context
  • expose access to the jmx server through a remote rmi url
  • protect access to the jmx server though authentication and authorisation

few things to note about the above configuration:

  • you want to externalise some configurationĀ  information, such as the rmi registry port and the host where the application is running. although i externalised the rmi registry port to a property file in the classpath, i left "localhost" as host name. in a real production environment, especially when you want to scale your application horizontally, e.g. deploy it on different servers, the server part of the remote url should also be externalised.
  • because we are exposing a remote rmi url, in order to expose the jmx server to the rmi registry, we need to start an rmi registry if one is not already started; this happens by declaring the rmiregistryfactorybean. the port on which the registry is started must be the same as the exposed url.
  • the above configuration does not enable annotation-based mbean support; such configuration will be the subject of my next article in which i'll show how to code a simple mbean to change the logging level of your log4j-based application.

protecting access to the jmx server through authentication and authorisation

in the above configuration you might have noticed the following part:

<property name="environment">
            <!-- the following is only valid when the sun jmx implementation is used -->
            <map>
                <entry key="jmx.remote.x.password.file" value="${user.home}/.secure/jmxremote.password" />
                <entry key="jmx.remote.x.access.file" value="${user.home}/.secure/jmxremote.access" />
            </map>
        </property>

what the above snippet declares is the location of two files, one used for authorisation, one for authentication. i decided to put such files under ~/.secure but the location is ultimately up to you. the content of such files is simple:

jmx access file

jemosadmin readwrite

the above file contains the name of a user (jemosadmin) and its role (readwrite).

jmx password file

in the jmx password file you declare the user and its password:

jemosadmin secure

once these information are in place, you can start up the jmx server and then access it through either jconsole or jvisualvm (if you are using jdk6 or later).

jmx-remote-access

once authenticated, the rmi connector is actually available as a bean, as are all oracle's native mbeans.

jmx-accessed

in my next article i will show how to code a simple logging mbean service which can be used at runtime to change the logging level of a package (and all its subpackages). this service brings the advantage of increased uptime and helps troubleshooting applications.

from http://tedone.typepad.com/blog/2011/08/jmx-and-spring-part-1.html

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fargate vs. Lambda: The Battle of the Future
  • Microservices Testing
  • Introduction To OpenSSH
  • 10 Things to Know When Using SHACL With GraphDB

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
  • +1 (919) 678-0300

Let's be friends: