我正在尝试在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);
上
最佳答案
这里有很多问题。
从msb = ....开始,没有方法....它必须存在于方法中。
接下来,新的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 ...........
}