JMX和REST API。我怎样才能在它们之间搭建一座桥梁?

时间:2014-09-27 04:09:30

标签: java rest jmx spring-jmx

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

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

我使用过Jolokia,我收到了这个回复。我没有得到任何信息。

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

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

1 个答案:

答案 0 :(得分:1)

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

  

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

编辑:

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

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

请参阅the documentation