简体   繁体   English

Jmx客户端抛出InstanceNotFoundException

[英]Jmx client throwing InstanceNotFoundException

I have a Jmx Client that i use to test a jmx bean I've written. 我有一个Jmx客户端,用于测试我写的jmx bean。 Here's the code for the client: 这是客户端的代码:

    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 and all it's dependencies are present in a library in IntelliJ. ProcessingPing及其所有依赖项都存在于IntelliJ库中。

My jmx beans are: 我的jmx bean是:

    public interface ProcessingPing {

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

and

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

When running, the client, i get an exception when trying to invoke the ping method: 在运行客户端时,我在尝试调用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

I do not understand why jmx seems to be able to get the bean, but not the actual instance of the class. 我不明白为什么jmx似乎能够获取bean,而不是类的实际实例。 I am guessing it's sort of a classpath issue, but couldn't find it. 我猜这是一个类路径问题,但找不到它。 As a side not, when testing with JConsole, it works fine. 作为一个方面,当使用JConsole进行测试时,它可以正常工作。

Thanks 谢谢

It looks like the object name used to register the MBean differs from the object name you use when trying to retrieve the managed bean. 看起来用于注册MBean的对象名称与尝试检索托管bean时使用的对象名称不同。

Try to check the object name in JConsole. 尝试检查JConsole中的对象名称。

Looks like the objectname in mbean defined is different from the one you are providing in JMX client code. 看起来mbean中定义的对象名与您在JMX客户端代码中提供的对象名不同。 The object name should match exactly as what you define in java class like in below class m bean is exposed by using spring. 对象名称应该与您在java类中定义的完全匹配,如下面的类m bean通过使用spring公开。

Example - the below line written on top of class @ManagedResource(objectName = "com.spmsoftware.processing.ping:type=ProcessingPing" 示例 - 写在@ManagedResource类之上的以下行(objectName =“com.spmsoftware.processing.ping:type=ProcessingPing”

Or you can also check in jconsole.exe and go to tab mbeans and check the name of mbean on left pane. 或者您也可以检入jconsole.exe并转到选项卡mbeans并在左窗格中检查mbean的名称。

In my case I forgot to create the Bean (in these case ProcessingPingImpl). 在我的情况下,我忘了创建Bean(在这些情况下ProcessingPingImpl)。

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

Or you create the bean directly in the @Configuration-Object like this: 或者直接在@Configuration-Object中创建bean,如下所示:

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

XML solution: XML解决方案:

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

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

Or without component-scan: 或者没有组件扫描:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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