Questions on JMX Example

2019-05-26 14:13发布

问题:

I have written sample JMX MBean "PoolMBean" based on my uderstanding of MBeans. I have written this to manage and monitor the connection pool. My question here is, is this the way Mbeans are written? Are there any issues in this Mbean code not related to connection pool?

1) What kind of objects can an Mbean method return?

 package pool;

    import java.util.Date;

    public class Connection {

        public Date createdAt;
        protected int usedCount;
        protected boolean isAvailable = true;

        public Connection newConnection(){
            Connection con= null;
            /**
             * Code for creating Connection
             */
             return con;
        }

        public void writeDate(){

            /**
             * Code to write data in the stream
             */
            usedCount++;        
        }   


    }

package pool;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.management.MBeanServer;
import javax.management.ObjectName;

public class ConnectionPool {
    public static int maxPoolSize = 20;
    public int currentPoolSize = 10;
    public LinkedList<Connection> totalPool = new LinkedList<Connection>();
    public LinkedList<Connection> availablePool = new LinkedList<Connection>();
    public static ConnectionPool cp = new ConnectionPool();
    private ConnectionPool(){

    }

    public synchronized Connection getConnection(){
        Connection con = null;
        /**
         * 
         */
        availablePool.remove(con);
        con.isAvailable = false;
        return con;
    }

    public synchronized void returnConnection(Connection con){

        /**
         * 
         */
        availablePool.addFirst(con);
        con.isAvailable = true;     
    }

    public static void main(String a[]){
        try{
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        Pool mbean = new Pool();
        ObjectName name = new ObjectName("test.conMbean:key1=Pool");
        server.registerMBean(mbean, name);
        System.out.println("Let me see out put");
        System.in.read();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}

package pool;


public interface PoolMBean {

    public int getCurrentPoolSize();

    public int getMaxPoolSize();

    public void setMaxPoolSize(int maxSize);    

}

package pool;


public class Pool implements PoolMBean {

    @Override
    public int getCurrentPoolSize() {

        return ConnectionPool.cp.currentPoolSize;
    }

    @Override
    public int getMaxPoolSize() {   
        return ConnectionPool.maxPoolSize;
    }

    @Override
    public void setMaxPoolSize(int maxSize) {
        ConnectionPool.maxPoolSize = maxSize;   
    }


}

Added this based on "yazan jber" answer provided below.

1) What kind of objects can a Mbean method return? For example if the PoolMBean hasgetStatistics() which returns totalPool LinkedList object. In this case in JConsole the value is displaying Unavailable, but when I tried with HashMap with String objects it worked? So the JConsole can't read everything, What it can read is my question here?

I have gone through the Oracle MXBean annotation API doc, the description here is bit complicated. What I got from this link is there are OpenType,ArrayType, CompositeType, SimpleType and TabularType these deals with only

  • java.lang.Void,
  • java.lang.Boolean,
  • java.lang.Character,
  • java.lang.Byte,
  • java.lang.Short,
  • java.lang.Integer,
  • java.lang.Long,
  • java.lang.Float,
  • java.lang.Double,
  • java.lang.String,
  • java.math.BigDecimal,
  • java.math.BigInteger,
  • java.util.Date,
  • javax.management.ObjectName,
  • CompositeData.class.getName(),
  • TabularData.class.getName()

these objects. MBean should return any one of this OpenType.

If we want to return any other type that new type should implement CompositeData interface, I didn't get much how this implementation will help Jconsole to read open objects, it is another complicated question?

To Track individual components in my application we should have our own MBeans? If my understanding is right I can use simple java class for this purpose, the extra benefit I'm getting here is the JConsole UI, Isn't it?

回答1:

Your CompositeData will have a method to return its CompositeType. The type defines the attribute names (keys) of your CompositeData. JConsole, and other JMX clients, may use these keys to access data from the CompositeData.



回答2:

It is perhaps too late to answer this question - however I wanted to take a shot at this given that I am currently studying JMX.

Questions answered :

  1. Yes, the posted code looks to me to be the correct way to write MBeans within an application.
  2. It is recommended to have custom defined MBeans to manage the resources exposed by an application. What resources warrant management is usually a design-time decision. However, from what I understand; we would want to manage resources that would have impact on the performance and stability of the system and hence would want to administer. A good example would be the Apache Solr related classes which implement the SoltInfoMBean management interface, so that these objects can be managed from the Solr Administration console.
  3. While you can have your own custom implementation to track individual components of the system, the advantage of using MBeans to perform the tracking is not limited to jConsole** UI support alone. With the use of standard JMX interfaces, you provide facilities like Out-of-the-box management capabilities with any management applications that confirms to the JMX spec. Also it would support management over various communication protocols such as RMI, SNMP etc without the management console and the managed applications worry about the nitty-gritties of underlying protocol. This page provides a good set of reasons to use JMX interfaces to add monitoring capabilities to your application.

Hope this helps.



回答3:

I didn't run the code but it looks fine, you can return any serializable object if the JMX client is Java and has access to the same serializable class, see this link



标签: java jmx