hushowfly 2008-09-02 17:54
浏览 358
已采纳

JMX监控weblogic问题

工作须要,得监控weblogic的指标,例如:连接池、线程数、JMS明细、EJB明细,等指标!在网上了解了一下JMX原理,也做了一个JMX的Hello例子,但是要监控weblogic不知道如何动手,得到了与weblogic的Mbeanserverconnection,但是不知道怎么得到我要的监控指标,是还要注册相应的Mbean?该Mbean要怎么写呀?又怎么从Mbean上得到相应的监控数据,自己做的JMX例子好像太浅,希望哪位JMX老手指点一下,能否提供点JMX资料!我得到Mbeanserverconnection的代码如下:
private static MBeanServerConnection lookupMBeanServer9(String ip, String port, String name, String pass)
{
String protocol;
String s5;
protocol = "t3";
s5 = "/jndi/weblogic.management.mbeanservers.runtime";
MBeanServerConnection mbeanserverconnection;
try
{
//JarLoader jarloader = new JarLoader((new StringBuilder()).append(".").append(File.separator).append("working").append(File.separator).append("classes").append(File.separator).append("weblogicclient9.jar").toString());
//Thread.currentThread().setContextClassLoader(jarloader);
JMXServiceURL jmxserviceurl = new JMXServiceURL(protocol,ip, Integer.parseInt(port), s5);
Hashtable hashtable = new Hashtable();
hashtable.put("java.naming.security.principal", name);
hashtable.put("java.naming.security.credentials", pass);
hashtable.put("jmx.remote.protocol.provider.pkgs", "weblogic.management.remote");
JMXConnector jmxconnector = JMXConnectorFactory.connect(jmxserviceurl, hashtable);
mbeanserverconnection = jmxconnector.getMBeanServerConnection();

return mbeanserverconnection;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}

  • 写回答

2条回答 默认 最新

  • weixin_42298775 2008-09-02 20:04
    关注

    [align=left]需要了解weblogic的MBean结构,你可以去bea网站查看相关资料,了解其结构后就很容易得到这些指标了,以下给出简单的示例代码:

    package demo.jmx.weblogic;

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Hashtable;

    import javax.management.MBeanServerConnection;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    import javax.naming.Context;

    @SuppressWarnings("unused")
    public class WeblogicJmxTest {
    private static MBeanServerConnection connection;
    private static JMXConnector connector;
    private static final ObjectName service;

    /* 
     *  实例化 DomainRuntimeServiceMBean 对象名,这样可以通过类使用此对象名.
     */
    static {
        try {
            service = new ObjectName(
                    "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
        } catch (MalformedObjectNameException e) {
            throw new AssertionError(e.getMessage());
        }
    }
    
    public static void main(String[] args) throws Exception {
        String hostname = "localhost";
        String portString = "7001[align=left][/align][align=left][/align]";
        String username = "weblogic";
        String password = "weblogic";
        WeblogicJmxTest demo = new WeblogicJmxTest();
        demo.initConnection(hostname, portString, username, password);
    
        demo.printNameAndState(demo.getServerRuntimes());
    

    // demo.getServletData();

    // //得到运行时信息
    // MBeanInfo runMBeanInfo = connection.getMBeanInfo(ObjectName.getInstance("com.bea:Location=AdminServer,Name=consoleapp,ServerRuntime=AdminServer,Type=ServerRuntime"));
    // MBeanAttributeInfo[] attr = runMBeanInfo.getAttributes();
    // for(int i=0;i // if("WorkManagerRuntimes".equals(attr[i].getName())){
    // demo.print("Runtime Info", attr[i].getName());
    // demo.print("Runtime Desc", attr[i].getDescription());
    // }
    // }
    //
    connector.close();
    }
    /*
    * 实例化与 Domain Runtime MBean Server 的连接。
    */
    private void initConnection(String hostname, String portString,
    String username, String password) throws IOException,
    MalformedURLException {
    String protocol = "t3";
    Integer portInteger = Integer.valueOf(portString);
    int port = portInteger.intValue();
    String jndiroot = "/jndi/";
    String mserver = "weblogic.management.mbeanservers.domainruntime";
    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
    jndiroot + mserver);
    Hashtable h = new Hashtable();
    h.put(Context.SECURITY_PRINCIPAL, username);
    h.put(Context.SECURITY_CREDENTIALS, password);
    h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
    "weblogic.management.remote");
    connector = JMXConnectorFactory.connect(serviceURL, h);
    connection = connector.getMBeanServerConnection();
    }

    // private void initConnectionByJDK(String hostname, String portString,
    // String username, String password) throws IOException,
    // MalformedURLException {
    // String protocol = "rmi";
    // Integer portInteger = Integer.valueOf(portString);
    // int port = portInteger.intValue();
    // String jndiroot = "/jndi/iiop://" + hostname + ":" + port +"/";
    // String mserver = "weblogic.management.mbeanservers.domainruntime";
    // JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
    // jndiroot + mserver);
    // Hashtable h = new Hashtable();
    // h.put(Context.SECURITY_PRINCIPAL, username);
    // h.put(Context.SECURITY_CREDENTIALS, password);
    // h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");
    // connector = JMXConnectorFactory.connect(serviceURL, h);
    // connection = connector.getMBeanServerConnection();
    // }

    /*
     * 打印一组 ServerRuntimeMBeans.此 MBean 是运行时 MBean 层次的根,此域中的每个服务器承载自己的实例.
     */
    public ObjectName[] getServerRuntimes() throws Exception {
        return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
    }
    
    /*
     * 迭代 ServerRuntimeMBean,获取名称和状态
     */
    public void printNameAndState(ObjectName[] p_objNames) throws Exception {
        ObjectName[] serverRT = p_objNames;
        System.out.println("got server runtimes");
        int length = (int) serverRT.length;
        for (int i = 0; i < length; i++) {
            print("===================Weblogic运行信息====================","");
            //域名称
            String name = (String) connection.getAttribute(serverRT[i], "Name");
            System.out.println("Server name: " + name);
            //运行状态
            String state = (String) connection.getAttribute(serverRT[i],"State");
            System.out.println("Server state: " + state);
            //开始时间
            Long activationTime = (Long) connection.getAttribute(serverRT[i], "ActivationTime");
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            date.setTime(activationTime);
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strDateTime = formater.format(date);
            System.out.println("Start running time: " + strDateTime);
            //weblogic 的版本
            String weblogicVersion = (String) connection.getAttribute(serverRT[i], "WeblogicVersion");
            System.out.println("Weblogic Version: " + weblogicVersion);
    
    
            //OS信息
            ObjectName jvmServerRT = (ObjectName)connection.getAttribute(serverRT[i], "JVMRuntime");
            print("=======================OS信息==========================","");
            print(" 操作系统",connection.getAttribute(jvmServerRT, "OSName").toString());
            print(" 操作系统版本",connection.getAttribute(jvmServerRT, "OSVersion").toString());
            print(" Java版本",connection.getAttribute(jvmServerRT, "JavaVersion").toString());
            print(" Java提供商",connection.getAttribute(jvmServerRT, "JavaVMVendor").toString());
    
            long runTime = (Long)connection.getAttribute(jvmServerRT,"Uptime")/1000;
            long day = runTime/(24*60*60);
            long hour = runTime%(24*60*60)/(60*60);
            long minute = runTime%(60*60)/60;
            long second = runTime%60;
            System.out.println(" 系统已经运行:"+day+"天"+hour+"小时"+minute+"分"+second+"秒");
    
        }
    }
    
    /*
     * 获取一组 WebApplicationComponentRuntimeMBean
     */
    public void getServletData() throws Exception {
        ObjectName[] serverRT = getServerRuntimes();
        int length = (int) serverRT.length;
        for (int i = 0; i < length; i++) {
            ObjectName[] appRT = (ObjectName[]) connection.getAttribute(serverRT[i], "ApplicationRuntimes");
            int appLength = (int) appRT.length;
            for (int x = 0; x < appLength; x++) {
                System.out.println("Application name: " + (String) connection.getAttribute(appRT[x], "Name"));
                ObjectName[] compRT = (ObjectName[]) connection.getAttribute(appRT[x], "ComponentRuntimes");
                int compLength = (int) compRT.length;
                for (int y = 0; y < compLength; y++) {
                    System.out.println("  Component name: " + (String) connection.getAttribute(compRT[y],"Name"));
                    String componentType = (String) connection.getAttribute(compRT[y], "Type");
                    System.out.println("            type: " + componentType.toString());
    

    // if (componentType.toString().equals("WebAppComponentRuntime")) {
    // ObjectName[] servletRTs = (ObjectName[]) connection.getAttribute(compRT[y], "Servlets");
    // int servletLength = (int) servletRTs.length;
    // for (int z = 0; z < servletLength; z++) {
    // System.out.println(" Servlet name: " + (String) connection.getAttribute(servletRTs[z], "Name"));
    // System.out.println(" Servlet context path: " + (String) connection.getAttribute(servletRTs[z], "ContextPath"));
    // System.out.println(" Invocation Total Count : " + (Object) connection.getAttribute(servletRTs[z],"InvocationTotalCount"));
    // }
    // }
    }
    }
    }
    }

    public void print(String prefix,String content){
        System.out.println(prefix + ": " + content);
    }
    

    }
    [/align]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名