Jmx客户端抛出InstanceNotFoundException

时间:2013-03-12 10:13:42

标签: java intellij-idea client classpath jmx

我有一个Jmx客户端用于测试我编写的jmx bean。这是客户端的代码:

    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8686/jmxrmi");
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
    ObjectName mbeanName = new ObjectName("com.spmsoftware.processing.ping:type=ProcessingPing");

    ProcessingPing mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, ProcessingPing.class, true);
    System.out.println("\nResult = " + mbeanProxy.ping(346, 0).getResultCode());

    jmxc.close();

ProcessingPing及其所有依赖项都存在于IntelliJ库中。

我的jmx bean是:

    public interface ProcessingPing {

        public PingResult ping(Integer environmentId, Integer timeout);
    }

    @Service("ProcessingPing")
    @ManagedResource(description = "")
    public class ProcessingPingImpl implements ProcessingPing {

        private static final Integer DEFAULT_TIMEOUT = 5000;

        @Autowired
        private PingProcessService pingProcessService;

        @Override
        @ManagedOperation(description = "")
        public PingResult ping(Integer environmentId, Integer timeout) {
            return pingProcessService.run(environmentId, timeout);
        }            
    }

运行时,客户端,我在尝试调用ping方法时遇到异常:

    Caused by: javax.management.InstanceNotFoundException: com.spmsoftware.processing.ping:type=ProcessingPing
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getClassLoaderFor(DefaultMBeanServerInterceptor.java:1444)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.getClassLoaderFor(JmxMBeanServer.java:1308)
        at com.sun.enterprise.v3.admin.DynamicInterceptor.getClassLoaderFor(DynamicInterceptor.java:907)
        at javax.management.remote.rmi.RMIConnectionImpl$4.run(RMIConnectionImpl.java:1346)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.management.remote.rmi.RMIConnectionImpl.getClassLoaderFor(RMIConnectionImpl.java:1342)
        at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:795)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
        at sun.rmi.transport.Transport$1.run(Transport.java:177)
        at sun.rmi.transport.Transport$1.run(Transport.java:174)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
        at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
        at javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(Unknown Source)
        at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(RMIConnector.java:1017)
    Disconnected from the target VM, address: '127.0.0.1:56621', transport: 'socket'
        at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:305)
        ... 2 more

我不明白为什么jmx似乎能够获取bean,而不是类的实际实例。我猜这是一个类路径问题,但找不到它。作为一个方面,当使用JConsole进行测试时,它可以正常工作。

由于

3 个答案:

答案 0 :(得分:8)

看起来用于注册MBean的对象名称与尝试检索托管bean时使用的对象名称不同。

尝试在JConsole中检查对象名称。

答案 1 :(得分:0)

看起来mbean中定义的对象名与您在JMX客户端代码中提供的对象名不同。 对象名称应该与您在java类中定义的完全匹配,如下面的类m bean通过使用spring公开。

示例 - 写在课程顶部的下面一行 @ManagedResource(objectName =“com.spmsoftware.processing.ping:type=ProcessingPing”

或者您也可以检入jconsole.exe并转到选项卡mbeans并检查左窗格中的mbean名称。

答案 2 :(得分:0)

在我的情况下,我忘了创建Bean(在这些情况下是ProcessingPingImpl)。

注释解决方案: 您可以在bean上使用@Component,在@Configuration-Object上使用@ComponentScan("com.company")。 (见http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html

或者直接在@Configuration-Object中创建bean,如下所示:

@Bean
public ProcessingPing processingPing(){
    return new ProcessingPingImpl();
}

XML解决方案:

<context:component-scan base-package="com.company" />

(参见https://github.com/spring-by-example/spring-by-example/blob/master/enterprise/spring-jmx/src/test/resources/org/springbyexample/jmx/JmxTest-context.xml

或没有组件扫描:

<bean id="processingPing" class="com.company.ProcessingPingImpl" />