创建RMI连接器客户端

时间:2013-05-02 05:27:57

标签: java jboss rmi mbeans

我正在尝试连接到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 =假

但我除了

  

线程“main”中的异常java.io.IOException:无法检索   RMIServer存根:javax.naming.ServiceUnavailableException [Root   异常是java.rmi.ConnectException:连接拒绝主机:   本地主机;嵌套异常是:java.net.ConnectException:   连接被拒绝:连接] at   javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:338)     在   javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)     在   javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:207)     在com.demo1.jmx.SystemConfigClient.main(SystemConfigClient.java:29)   引起:javax.naming.ServiceUnavailableException [根异常是   java.rmi.ConnectException:连接拒绝主机:localhost;   嵌套异常是: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)at   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:连接被拒绝   主持人:localhost;嵌套异常是:java.net.ConnectException:   连接被拒绝:连接在   sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)at at   sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)     在   sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)     在sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)at   sun.rmi.registry.RegistryImpl_Stub.lookup(未知来源)at   com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:97)     ... 8更多引起:java.net.ConnectException:拒绝连接:   在java.net.PlainSocketImpl.socketConnect(Native Method)连接   java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)at   java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)     在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)at   java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)at   java.net.Socket.connect(Socket.java:529)at   java.net.Socket.connect(Socket.java:478)at   java.net.Socket。(Socket.java:375)at   java.net.Socket。(Socket.java:189)at   sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)     在   sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)     at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)     ... 13更多Java结果:1

1 个答案:

答案 0 :(得分: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