For the best experience, try the new Microsoft Edge browser recommended by Microsoft (version 87 or above) or switch to another browser � Google Chrome / Firefox / Safari
OK

The Java Management Extensions (JMX) technology is vastly used to build distributed Web-based, modular, and dynamic solutions for managing and monitoring devices, applications, and service-driven networks. In this blog, I will explain how one can enhance their application performance monitoring by using a JMX Connection Pool. By making use of connection pool you can boost over-all application performance.

Introduction

JMX is a JAVA technology framework for managing and monitoring applications, system objects, and devices that are hosted on the server. To monitor the state of the server, JMX uses the MBean(Managed Bean) servers. The MBean servers provide information with respect to the server’s resources such as memory details, garbage collection data, thread data, OS information, number of threads running on the server and available memory on the server. To read the state information of a server, a connection to the JMX server is required. Therefore, a pool of JMX connections can be shared among each client request.

JMX Connection pool is a cache of JMX connections, so that the JMX connections can be reused when future requests for the JMX connection arrive. For each client request, creating a new connection to the JMX server could be expensive in terms of system resource consumption. By reusing the JMX connections, we can improve the overall application performance.

These following sections discuss how a JMX Connection Pool is built using Apache-Commons Generic Pool API.

Solution

The solution uses the Apache Generic Object Pooling Framework which provides pooling functionality for any parameterized object. Here the parameterized object is the JMX connections.

The core components of the solution are listed as follows:

JmxPool

The com.xoriant.jmx.pool.JmxPool class provides the pooling functionality for JMX connections. The com.xoriant.jmx.pool.JmxPool uses the apache org.apache.commons.pool2.impl.GenericObjectPool class to implement the pool functionality.

The org.apache.commons.pool2.impl.GenericObjectPool class provides the following configurable parameters for the pool:

  • How many connections a pool can have?
  • How many idle objects can be allowed at a time in the pool?
  • Testing a connection on borrow from the pool
  • Testing a connection on return to the pool
  • Testing a connection on create etc.

The following code snippet shows how the com.xoriant.jmx.pool.JmxPool class uses the GenericObjectPool:

public class JmxPool<Q> extends GenericObjectPool<Q>
{
     public JmxPool(PooledObjectFactory<Q> factory) 
    {
            super(factory);
    }

    public JmxPool(PooledObjectFactory<Q>factory,GenericObjectPoolConfig config)
   {
    super(factory, config);
   }
}

The client of JmxPool pool passes the parameterized object for which the pool is created. For JMX connection, the client needs to pass javax.management.remote.JMXConnector as:

new JmxPool<JMXConnector>(new JmxPoolFactory<JMXConnector>(host, port));
JmxPoolFactory

The com.xoriant.jmx.pool.JmxPoolFactory provides the life-cycle methods for pooled object. The com.xoriant.jmx.pool.JmxPoolFactory extends class org.apache.commons.pool2.BasePooledObjectFactory and provides the following functionality for JMX connection:

  • Creation
  • Destruction
  • Passivation
  • Validation

These functionalities are implemented as follows:

Constructor
public JmxPoolFactory(Stringhost,Stringport)
{
   this.host = host;
   this.port = port;
}

This constructor initializes the data that is required to create a connection.

makeObject
@Override
public PooledObject<Q> makeObject() throws Exception 
{
   return wrap(create());
}

The apache generic pool API calls the makeObject method to initiate the pool object creation process.

Wrap
@Override
public PooledObject<Q>wrap(Q jmxConnector)
{
  return new DefaultPooledObject<Q>(jmxConnector);
}

The wrap method creates a new instance of the jmxConnector object so that the pool can track the state of the pooled object.

Create
@Override
public Q create() throws Exception
{
   // code to create connection
   JMXServiceURLurl = null;
   StringBuildervUrlString = newStringBuilder(”service:jmx:rmi:///jndi/rmi://”);
   vUrlString.append(host);
   vUrlString.append(port);
   url = new JMXServiceURL(vUrlString.toString());
   return (Q)JMXConnectorFactory.connect(url, null);
}

The create method creates the JMX connection to the server by using the server host and the port.

destroyObject
@Override
public void destroyObject(PooledObject<Q>jmxPooledObject) throwsException 
{
   // close the connection here
   JMXConnectorjMXConnector =((JMXConnector)(jmxPooledObject.getObject()));
   jMXConnector.close();
}

The destroyObject method is called to close the JMX connection.

validateObject
@Override
public boolean validateObject(PooledObject<Q>jmxPooledObject)
{
   Boolean isValidConnection = Boolean.TRUE;
      try 
        {
          (((JMXConnector)(jmxPooledObject.getObject()))).getMBeanServerConnection();
        } 
     catch (IOExceptione e) 
       {
          isValidConnection = Boolean.FALSE;
       }
   return isValidConnection;

}

The validateObject method is called before the pooled object is served to the client. If the connection is valid, it returns true. If the connection is not valid, it returns false.

Using the JmxPool and JmxPoolFactory classes

The com.xoriant.jmx.pool.JmxPoolManager class uses the core classes for pool implementation. The following section describes the functionality of JmxPoolManager.

JmxPoolManager

The com.xoriant.jmx.pool.JmxPoolManager class provides the facade implementation to the client. The manager class uses the factory and the pool class uses the client request for JMX connection.

The following code snippet shows how to use the com.xoriant.jmx.pool.JmxPool object:

public class JmxPoolManager
{
 private JmxPool jmxPool;
 constructor
 public JmxPoolManager(String host, int port) 
 {
 jmxPool= new JmxPool<JMXConnector>(new JmxPoolFactory<JMXConnector>(host,port));
 }
}
getConnectionFromPool
public JMXConnector getConnectionFromPool() 
{
return pool.borrowObject();
}

This method requests a connection from the pool. If the connection is not available in the pool, it creates a new connection and provides it to the client. The method uses the borrowObject() method of the org.apache.commons.pool2.impl.GenericObjectPool.

returnConnectionToPool
public void returnConnectionToPool() 
{
pool.returnObject();
}

This method returns the connection object back to the pool. The method uses the returnObject() method of the org.apache.commons.pool2.impl.GenericObjectPool.

cleanup
public void cleanup()
{
pool.close();
}

This method closes the pool. Once the pool is closed, borrowObject() fails with IllegalStateException.

Source download

Download the complete source code from the following link: https://github.com/xoriantcorporation/jmxpool.git

Reference

 

Get Started

Your Information

13 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Your Information

4 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Your Information

3 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Globally Presence
Across Americas, Europe, and Asia
All Locations
Asia
Europe
North America
19 Locations
7
10
2
10 Locations
Ahmedabad
A-201, WestGate Business Bay, SG Road, Makarba, Ahmedabad 380015
Hyderabad
Block –B, Wing 1, 2nd Floor, Cyber Gateway, Hitech city, Hyderabad 500081
Gurugram
2nd Floor, Tower B, Unitech Cyber Park, Sector 39, Gurugram 122001
Singapore
70 Shenton Way, #13-03, Eon Shenton, Singapore 079118
Bengaluru
Subramanya Arcade SA Tower, 2nd floor, A-wing, Bannerghatta Main Road, BTM Layout, Bengaluru, Karnataka 560029
Chennai
8th Floor, Smartworks, Olympia National Tower, Block 3, A3 and A4, North Phase, Guindy Industrial Estate, Chennai 600032
Pune
7th Floor, IT-7 Building, Qubix Business Park Pvt. Ltd. SEZ, Phase - 1, Hinjawadi, Pune 411057
Mumbai - Thane
AWFIS 1st Floor, Nehru Nagar, Wagle Industrial Estate, Thane West, Thane Maharashtra 400604
Mumbai
7th Floor, Smartworks, Times Square, Tower C, Andheri Kurla Road, Marol, Andheri East, Mumbai 400059
Pune
6th Floor, Smartworks, Pan Card Club Road, Baner, Pune 411045
2 Locations
London
c/o SPACES, 12 Hammersmith Grove, London W67AP, UK
Ireland
Grove, Fethard, Co. Tipperary, E91 E282, Dublin, Ireland
7 Locations
Canada
55 York Street, Suite 401 Toronto, ON, Canada M5J 1R7
Mexico
Tomas A. Edison 1510-201 Ciudad Juárez, Chihuahua, Mexico 32300
Seattle
4030 Lake Wash Blvd NE, STE 210, Kirkland, WA 98033
Troy
6915 Rochester Road Suite 300 Troy, MI 48085
Sunnyvale
1248 Reamwood Avenue Sunnyvale, CA 94089
New Jersey
343 Thornall Street Suite 720 Edison, NJ 08837
Dallas
5851 Legacy Circle Suite 600 Plano, TX 75024
All Locations
19 Locations
7
10
2
10 Locations
Ahmedabad
A-201, WestGate Business Bay, SG Road, Makarba, Ahmedabad 380015
Hyderabad
Block –B, Wing 1, 2nd Floor, Cyber Gateway, Hitech city, Hyderabad 500081
Gurugram
2nd Floor, Tower B, Unitech Cyber Park, Sector 39, Gurugram 122001
Singapore
70 Shenton Way, #13-03, Eon Shenton, Singapore 079118
Bengaluru
Subramanya Arcade SA Tower, 2nd floor, A-wing, Bannerghatta Main Road, BTM Layout, Bengaluru, Karnataka 560029
Chennai
8th Floor, Smartworks, Olympia National Tower, Block 3, A3 and A4, North Phase, Guindy Industrial Estate, Chennai 600032
Pune
7th Floor, IT-7 Building, Qubix Business Park Pvt. Ltd. SEZ, Phase - 1, Hinjawadi, Pune 411057
Mumbai - Thane
AWFIS 1st Floor, Nehru Nagar, Wagle Industrial Estate, Thane West, Thane Maharashtra 400604
Mumbai
7th Floor, Smartworks, Times Square, Tower C, Andheri Kurla Road, Marol, Andheri East, Mumbai 400059
Pune
6th Floor, Smartworks, Pan Card Club Road, Baner, Pune 411045
2 Locations
London
c/o SPACES, 12 Hammersmith Grove, London W67AP, UK
Ireland
Grove, Fethard, Co. Tipperary, E91 E282, Dublin, Ireland
7 Locations
Canada
55 York Street, Suite 401 Toronto, ON, Canada M5J 1R7
Mexico
Tomas A. Edison 1510-201 Ciudad Juárez, Chihuahua, Mexico 32300
Seattle
4030 Lake Wash Blvd NE, STE 210, Kirkland, WA 98033
Troy
6915 Rochester Road Suite 300 Troy, MI 48085
Sunnyvale
1248 Reamwood Avenue Sunnyvale, CA 94089
New Jersey
343 Thornall Street Suite 720 Edison, NJ 08837
Dallas
5851 Legacy Circle Suite 600 Plano, TX 75024