2014-09-27 41 views
0

我已經編寫了一個代碼,通過JMX Bean從我的JVM應用程序公開數據。我可以在JConsole中看到這些值。我如何從jconsole獲取這些值,是否需要編寫另一個程序?JMX和REST API。我怎樣才能在他們之間架起一座橋樑?

而且,我該如何使用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

看看一個Jolokia;它暴露的MBean爲JSON通過HTTP ...

...這是一個基於代理的方法,並立與JSR-160,但採用更開放的HTTP其運輸業務,其中有效載荷數據以JSON序列化。這爲不同的非Java客戶端打開了一個全新的世界。除了此協議開關外,Jolokia爲JMX遠程處理提供了新功能,這些功能在JSR-160連接器中不可用:批量請求允許通過單個遠程服務器往返進行多個JMX操作。細粒度的安全機制可以限制對特定JMX操作的JMX訪問。 JSR-160代理模式或歷史追蹤等其他功能也是Jolokia所特有的。

編輯:

您需要發出一個查詢;例如如果你的域名是測試(例如用對象名稱test:name=counter,發出該查詢http://127.0.0.1:7777/jolokia/read/test:name=counter

或者,使用http://127.0.0.1:7777/jolokia/read/test:*你會得到下test域所有MBean。

一個MBean見the documentation

+0

雖然這個鏈接可能會回答這個問題,最好在這裏包含答案的重要部分,並提供參考鏈接。如果鏈接頁面發生變化,鏈接專有的答案可能會失效。 – 2014-09-27 09:47:10

+0

我認爲這有點苛刻。完善的項目 – 2014-09-27 15:49:13

+0

該評論是ans的標準評論我們提供了一個鏈接而且很少有其他的東西。沒有對Jolokia暗示或打算的判斷。 – 2014-09-27 16:02:53