简体   繁体   中英

javax.management.InstanceNotFoundException

I am relatively new to Managed Beans in java and i am trying to figure out why i am getting an exception.After running Main.java,i get the exception pasted at the bottom.I tried to view it on jconsole but it doesnt seem to be appearing.What iam i missing?

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


}

}

This is the exception i am getting

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

You have not registered your MBean. It's probably not a best practice to do so, but you could add the registration to your Worker constructor just to get things going. Add this line to your constructor after the creation of the ObjectName:

this.mbs.registerMBean(this, name);

(You will need to add catches for a handful of new exceptions)

_------

Sorry. Did not read carefully enough. You need to register an instance of Hello.

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