通过JMX发布Bean时出错

时间:2013-04-24 13:24:15

标签: java jmx

我正在尝试在JMX上注册一个bean。我在第mbs.registerMBean(metadataObj, name);行收到错误。错误说

Multiple markers at this line
    - Syntax error on token "(", delete 
     this token
    - Syntax error on token ")", delete 
     this token

我不知道它是什么。

此bean具有有关请求开始/结束时间的基本元数据。 的

    package test.performance;

public class RequestPerformanceMetadata implements PerformanceMetadataMBean{

    private double startTime;
    private double endTime;
    private double timeTook;
    private String requestType;
    private int numOfRequests;

    PerformanceMetadataMBean metadataObj = new RequestPerformanceMetadata();

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("test.performace:type=PerformanceMetadataMBean");      

    mbs.registerMBean(metadataObj, name);

    public double getTimeTook() {
        return timeTook;
    }
    public void setTimeTook(double timeTook) {
        this.timeTook = timeTook;
    }

接口

package test.performance;

public interface PerformanceMetadataMBean {

    double getTimeTook();
    void setTimeTook(double timeTook);
    String getRequestType();
    void setRequestType(String requestType);

开启

1 个答案:

答案 0 :(得分:1)

这里有很多问题。

msb = .... 开始,没有方法......它需要在方法中。 接下来, new ObjectName(...)会抛出异常,因此您需要将其包装在try / catch块中。 此外,您确定要在 RequestPerformanceMetadata 的 中创建 RequestPerformanceMetadata 另一个实例吗?也许你只想注册这个实例。

看看这段代码片段:

class RequestPerformanceMetadata implements PerformanceMetadataMBean {
    private double startTime;
    private double endTime;
    private double timeTook;
    private String requestType;
    private int numOfRequests;
    private MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    private ObjectName name;

    public RequestPerformanceMetadata() {
        try {
            name = new ObjectName("test.performace:type=PerformanceMetadataMBean"); 
            mbs.registerMBean(this, name);     
        } catch (Exception ex) {
            throw new RuntimeException("Yo dog. Bad object name", ex);
        }       
    }    
   //........... snip ...........
}