简体   繁体   中英

Error while publishing Bean through JMX

I am trying to register a bean on JMX. I am getting error on line mbs.registerMBean(metadataObj, name); . Error says

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

I have no idea what it is about.

This bean has basic metadata about request start/end time. Class

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

Interface

package test.performance;

public interface PerformanceMetadataMBean {

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

On

There's a lot of issues here.

Starting with msb = .... , there's no method.... it needs to be in a method. Next, new ObjectName(...) throws an exception, so you need to wrap it in a try/catch block. Also, are you sure you want to create another instance of RequestPerformanceMetadata inside of RequestPerformanceMetadata ? Perhaps you want to simply register the this instance.

Take a look at this code fragment:

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 ...........
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM