关于JMX示例的问题 [英] Questions on JMX Example

查看:201
本文介绍了关于JMX示例的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据我对MBeans的理解编写了示例JMX MBean PoolMBean。我写这篇文章来管理和监控连接池。我的问题是,这是Mbeans写的方式吗?这个Mbean代码中是否有任何与连接池无关的问题?

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)Mbean方法可以返回什么样的对象?

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;   
    }


}






根据下面提供的yazan jber答案添加了这个。



1)Mbean方法可以返回什么样的对象?例如,如果 PoolMBean 具有 getStatistics(),则返回totalPool LinkedList 对象。在这种情况下,在 JConsole 中,该值显示不可用,但是当我尝试使用 HashMap 字符串它有效的对象?所以 JConsole 无法读取所有内容,这里可以读到的是我的问题吗?


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?

我经历过 Oracle MXBean批注API文档,说明这有点复杂。我从这个链接得到的是 OpenType,ArrayType CompositeType SimpleType TabularType 这些交易只有

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()

  • 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()

这些对象。 MBean 应返回此 OpenType 中的任何一个。

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

如果我们想要返回任何其他类型的新类型应该实现 CompositeData 接口,我没有太多了解这个实现如何帮助Jconsole读取打开的对象,这是另一个复杂的问题?

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?

要跟踪我的应用程序中的各个组件,我们应该有自己的MBean吗?如果我的理解是正确的,我可以使用简单的java类来实现这个目的,我在这里获得的额外好处是JConsole UI,不是吗?

推荐答案

回答这个问题可能已经太晚了 - 但是我想在我正在研究JMX时考虑到这一点。

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.

回答的问题:


  1. 是的,发布的代码看起来是在应用程序中编写MBean的正确方法。 / li>
  2. 建议使用自定义MBean来管理应用程序公开的资源。需要管理的资源通常是设计时决策。但是,从我的理解;我们希望管理会对系统的性能和稳定性产生影响的资源,从而希望进行管理。一个很好的例子是实现 Apache Solr 相关类lucene.apache.org/solr/4_5_0/solr-core/org/apache/solr/core/SolrInfoMBean.html\"rel =nofollow> SoltInfoMBean 管理界面,以便可以从Solr管理这些对象管理控制台。

  3. 虽然您可以拥有自己的自定义实现来跟踪系统的各个组件,但使用MBean执行跟踪的优势并不仅限于jConsole ** UI支持。通过使用标准JMX接口,您可以使用任何管理应用程序提供开箱即用的管理功能等设施,以确认JMX规范。此外,它还支持对各种通信协议(如RMI,SNMP等)的管理,而无需管理控制台和托管应用程序担心底层协议的细节。 页面提供了一组使用JMX界面的好理由为您的应用程序添加监控功能。

  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.

希望这会有所帮助。

这篇关于关于JMX示例的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆