JMX和REST API.我如何在它们之间架起一座桥梁? [英] JMX and REST API. How can I build a bridge between them?

查看:81
本文介绍了JMX和REST API.我如何在它们之间架起一座桥梁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码,该代码通过JMX Bean公开了来自JVM应用程序的数据.我可以在JConsole中看到这些值.我如何从jconsole中获取这些值,我是否需要编写另一个程序.

I have written a code which exposes data from my JVM application via JMX Bean. I can see these values in JConsole. How can I grab these values from jconsole, do I need to write another program.

而且,如何使用REST API将这些JMX Bean数据显示为Rich UI格式?

And also, how can I use REST API to display these JMX Bean data to Rich UI format?

我使用了Jolokia,并且得到了这个回复.我没有任何信息.

I have used Jolokia and I am getting this reply. I am not getting any info.

我在代码中使用了jolokia作为JVM参数.但是我得到的唯一答复是这个

I used the jolokia as JVM argument in my code. But the only reply I am getting is this

{
timestamp: 1411988073,
status: 200,
request: {
type: "version"
},
value: {
protocol: "7.2",
config: {
maxDepth: "15",
maxCollectionSize: "1000",
maxObjects: "0",
discoveryEnabled: "true",
agentContext: "/jolokia",
historyMaxEntries: "10",
agentId: "10.91.240.11-4524-5f2e712f-jvm",
agentType: "jvm",
debug: "false",
debugMaxEntries: "100"
},
agent: "1.2.2",
info: { }
}
}

为什么没有信息?

我的代码如下:

/*
 * Main.java - main class for the Hello MBean and QueueSampler MXBean example.
 * Create the Hello MBean and QueueSampler MXBean, register them in the platform
 * MBean server, then wait forever (or until the program is interrupted).
 */

package com.example;

public class Main implements HelloMBean {
public static void main(String[] args) throws Exception {
    // Get the Platform MBean Server
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    // Construct the ObjectName for the Hello MBean we will register
    ObjectName mbeanName = new ObjectName(
            "com.example:type=Tiger, name=Info");

    // Create the Hello World MBean
    Hello mbean = new Hello();
    System.out.println(mbean);
    System.out.println(mbeanName);
    // Register the Hello World MBean
    mbs.registerMBean(mbean, mbeanName);
    if (System.getProperty("com.sun.management.jmxremote") == null) {
        System.out.println("JMX remote is disabled");
    } else {
        String portString = System.getProperty("com.sun.management.jmxremote.port");
        if (portString != null) {
            System.out.println("JMX running on port "
                + Integer.parseInt(portString));
        }}


    // Wait forever
    System.out.println("Waiting for incoming requests...");
    Thread.sleep(Long.MAX_VALUE);
}

/*
 * private final String name = "Reginald"; private int cacheSize =
 * DEFAULT_CACHE_SIZE; private static final int DEFAULT_CACHE_SIZE = 200;
 */
@Override
public void sayHello() {
    // TODO Auto-generated method stub

}

@Override
public int add(int x, int y) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public String getName() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getCacheSize() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void setCacheSize(int size) {
    // TODO Auto-generated method stub

}
}

界面是:

package com.example;

public interface HelloMBean {
public void sayHello();
public int add(int x, int y);
public String getName();

// a read-write attribute called CacheSize of type int
public int getCacheSize();
public void setCacheSize(int size);
}

实现如下:

package com.example;

import javax.management.*;

public class Hello
extends NotificationBroadcasterSupport implements HelloMBean {

public void sayHello() {
System.out.println("hello, world");
}

public int add(int x, int y) {
return x + y;
}


public String getName() {
return this.name;
}


public int getCacheSize() {
return this.cacheSize;
}

public synchronized void setCacheSize(int size) {
int oldSize = this.cacheSize;
this.cacheSize = size;


System.out.println("Cache size now " + this.cacheSize);


Notification n =
    new AttributeChangeNotification(this,
                    sequenceNumber++,
                    System.currentTimeMillis(),
                    "CacheSize changed",
                    "CacheSize",
                    "int",
                    oldSize,
                    this.cacheSize);


sendNotification(n);
}

@Override
public MBeanNotificationInfo[] getNotificationInfo() {
String[] types = new String[] {
    AttributeChangeNotification.ATTRIBUTE_CHANGE
};
String name = AttributeChangeNotification.class.getName();
String description = "An attribute of this MBean has changed";
MBeanNotificationInfo info =
    new MBeanNotificationInfo(types, name, description);
return new MBeanNotificationInfo[] {info};
}

private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int DEFAULT_CACHE_SIZE = 200;


private long sequenceNumber = 1;
}

推荐答案

看看 Jolokia ;它通过HTTP将MBeans公开为JSON ...

Take a look a Jolokia; it exposes MBeans as JSON over HTTP...

...这是一种基于代理的方法,与JSR-160并存,但是将更为开放的HTTP用于其传输业务,在该业务中,数据有效负载以JSON序列化.这为不同的非Java客户端打开了一个全新的世界.除了此协议开关之外,Jolokia还提供了JMX远程处理的新功能,这些功能在JSR-160连接器中不可用:批量请求允许通过一次远程服务器往返进行多个JMX操作.细粒度的安全性机制可以限制对特定JMX操作的JMX访问. JSR-160代理模式或历史记录跟踪等其他功能也是Jolokia特有的.

... It is an agent based approach, living side by side with JSR-160, but uses the much more open HTTP for its transport business where the data payload is serialized in JSON. This opens a whole new world for different, non-Java clients. Beside this protocol switch, Jolokia provides new features for JMX remoting, which are not available in JSR-160 connectors: Bulk requests allow for multiple JMX operations with a single remote server roundtrip. A fine grained security mechanism can restrict the JMX access on specific JMX operations. Other features like the JSR-160 proxy mode or history tracking are specific to Jolokia, too.

您需要发出查询;例如如果您的域是经过测试的(例如,对象名称为test:name=counter的MBean,请发出此查询http://127.0.0.1:7777/jolokia/read/test:name=counter.

You need to issue a query; e.g. if your domain is test (e.g. an MBean with object name test:name=counter, issue this query http://127.0.0.1:7777/jolokia/read/test:name=counter.

或者,使用http://127.0.0.1:7777/jolokia/read/test:*,您将在test域下获得所有MBean.

Or, use http://127.0.0.1:7777/jolokia/read/test:* and you'll get all MBeans under the test domain.

请参见文档.

这篇关于JMX和REST API.我如何在它们之间架起一座桥梁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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