2

我正在尝试连接到 MBean 服务器。我需要编写 JMX 客户端应用程序。这是用于客户端应用程序的代码。但我有一个与此相关的例外

无法检索 RMIServer 存根:javax.naming.ServiceUnavailableException [根异常是 java.rmi.ConnectException:连接拒绝主机:localhost;嵌套异常是:

有人可以帮我解决这个问题。

import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;


public class SystemConfigClient {

    public static final String HOST = "localhost";
    public static final String PORT = "1099";


    public static void main(String[] args) throws IOException, MalformedObjectNameException {
        JMXServiceURL url =new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + HOST + ":" +PORT+ "/jmxrmi");

        JMXConnector jmxConnector = JMXConnectorFactory.connect(url);
        MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
        //ObjectName should be same as your MBean name
        ObjectName mbeanName = new ObjectName("ifs.demo1.jmx:type=SystemConfig");

        //Get MBean proxy instance that will be used to make calls to registered MBean
        SystemConfigMBean mbeanProxy =
            (SystemConfigMBean) MBeanServerInvocationHandler.newProxyInstance(
                mbeanServerConnection, mbeanName, SystemConfigMBean.class, true);

        //let's make some calls to mbean through proxy and see the results.
        System.out.println("Current SystemConfig::" + mbeanProxy.doConfig());

        mbeanProxy.setSchemaName("NewSchema");
        mbeanProxy.setThreadCount(5);

        System.out.println("New SystemConfig::" + mbeanProxy.doConfig());

        //let's terminate the mbean by making thread count as 0
        mbeanProxy.setThreadCount(0);

        //close the connection
        jmxConnector.close();
    }

}

我已经使用以下参数运行了此代码。

Dcom.sun.management.jmxremote Dcom.sun.management.jmxremote.port=1099 Dcom.sun.management.jmxremote.authenticate=false Dcom.sun.management.jmxremote.ssl=false

但我得到了例外

线程“主”java.io.IOException 中的异常:无法检索 RMIServer 存根:javax.naming.ServiceUnavailableException [根异常是 java.rmi.ConnectException:连接拒绝主机:localhost;嵌套异常是:java.net.ConnectException: Connection denied: connect] at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:338) at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248 ) at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:207) at com.demo1.jmx.SystemConfigClient.main(SystemConfigClient.java:29) 引起:javax.naming.ServiceUnavailableException [根异常是 java.rmi .ConnectException:连接拒绝主机:本地主机;嵌套异常是:java.net.ConnectException:连接被拒绝:连接] 在 com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:101) 在 com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:185) 在 javax.naming.InitialContext .lookup(InitialContext.java:392) 在 javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1886) 在 javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1856) 在 javax。 management.remote.rmi.RMIConnector.connect(RMIConnector.java:257) ... 3 更多原因:java.rmi.ConnectException: Connection denied to host: localhost; 嵌套异常是:java.net.ConnectException:连接被拒绝:在 sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java: 198) 在 sun.rmi。

4

1 回答 1

2

您是否注意到您正在使用带有“D”而不是“-D”的 VM 参数?或者它只是一个错字?正确的是:

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=1099 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false
于 2013-07-04T18:49:40.207 回答