gpt4 book ai didi

javax.management.InstanceNotFoundException

转载 作者:行者123 更新时间:2023-12-01 10:01:58 29 4
gpt4 key购买 nike

我对 java 中的托管 Bean 比较陌生,我试图找出为什么会出现异常。运行 Main.java 后,我得到粘贴在底部的异常。我尝试在 jconsole 上查看它,但它似乎没有出现。我错过了什么?

Hello.java

package com.mbeans;

public class Hello implements HelloMBean {

private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int
DEFAULT_CACHE_SIZE = 200;

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) {

this.cacheSize = size;
System.out.println("Cache size now " + this.cacheSize);
}

}

HelloMBean.java

package com.mbeans;

public interface HelloMBean {

public void sayHello();
public int add(int x, int y);

public String getName();

public int getCacheSize();
public void setCacheSize(int size);
}

Main.java

package com.mbeans;

import java.lang.management.*;
import javax.management.*;

public class Main {

public static void main(String[] args)
throws Exception {


Worker worker = new Worker();

}
}

Worker.java

package com.mbeans;

import java.lang.management.ManagementFactory;
import java.util.logging.Level;

import javax.management.MBeanServer;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;


public class Worker {


MBeanServer mbs;
HelloMBean mbean;

public Worker()
{

this.mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = null;
try
{
name = new ObjectName("com.mbeans:type=HelloMBean");
}
catch (MalformedObjectNameException ex)
{
ex.printStackTrace();
}

try
{

this.mbean = ((HelloMBean)MBeanServerInvocationHandler.newProxyInstance(this.mbs, name, HelloMBean.class, false));


}
catch(Exception e)
{
e.printStackTrace();
}


System.out.println("online : " +this.mbean.add(3, 4));


}

}

这是我遇到的异常

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy2.add(Unknown Source)
at com.mbeans.Worker.<init>(Worker.java:45)
at com.mbeans.Main.main(Main.java:12)
Caused by: javax.management.InstanceNotFoundException: com.mbeans:type=HelloMBean
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown Source)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source)
at javax.management.MBeanServerInvocationHandler.invoke(Unknown Source)
... 3 more

最佳答案

您还没有registered你的 MBean。这样做可能不是最佳实践,但您可以将注册添加到您的 Worker 构造函数中,以便让事情顺利进行。创建 ObjectName 后将此行添加到构造函数中:

this.mbs.registerMBean(this, name);

(您需要为一些新异常添加捕获)

_------

抱歉。读得不够仔细。您需要注册一个 Hello 实例。

关于javax.management.InstanceNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36740127/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com