0

我们的架构中有几个服务器端组件。每个组件都使用 JMX 来公开各种内部属性。初始化如下:

try {       
        Registry registry = null;
        for(int i = _serverInfo.getJMXStartPort(); i <= _serverInfo.getJMXEndPort(); i++) {         
            try {
                registry = LocateRegistry.createRegistry(i);
                if(registry != null) {
                    _statusPort = i;
                    logger.info("Using JMX port: "+_statusPort);
                    break;
                }
            } catch (Exception e) {
                _statusPort++;
            }
        }                               

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

        _abstractServiceController = new AbstractServiceController(this);
        ObjectName mbeanName = new ObjectName("MyServer:name=MyServer Service");

        mbs.registerMBean(_abstractServiceController, mbeanName);

        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:"+_statusPort+"/jmxrmi");
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, System.getenv(), mbs);
        cs.start();

    } catch (Throwable e) {
        logger.error("Unable to register MBean with JMX");
        e.printStackTrace();
    }

我想我有两个问题。

  1. 这看起来对吗?

  2. 更大的问题是,虽然这在 java 1.6 上运行良好(主机上的每个后续服务器都使用下一个可用端口,因为如果端口不可用 LocateRegistry.createRegistry(i) 会抛出异常),但在 1.7 上则不然。结果,当第二个服务器尝试 JMXConnectorServer.start() 时,我们得到以下异常。有谁知道 createRegistry 的行为是否发生了变化?如果是这样,我们还应该做些什么吗?

    2013-02-07 15:34:28,451 INFO  [main] Using JMX port: 9500
    2013-02-07 15:34:28,929 ERROR [main] Unable to register MBean with JMX
    java.io.IOException: Cannot bind to URL [rmi://:9500/jmxrmi]: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]
            at javax.management.remote.rmi.RMIConnectorServer.newIOException(RMIConnectorServe.java:826)
    at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:431)
    at com.theatre.services.framework.AbstractService.run(AbstractService.java:306)
    at com.theatre.services.reporttree.TreeServerImpl.run(TreeServerImpl.java:690)
    at com.theatre.services.framework.Launcher.main(Launcher.java:99)
    Caused by: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]
    at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:139)
    at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:226)
    at javax.naming.InitialContext.bind(InitialContext.java:419)
    at javax.management.remote.rmi.RMIConnectorServer.bind(RMIConnectorServer.java:643)
    at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:426)
    ... 3 more
    Caused by: java.rmi.AlreadyBoundException: jmxrmi
    at sun.rmi.registry.RegistryImpl.bind(RegistryImpl.java:131)
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:390)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
    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:377)
    at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
    at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:137)
    ... 7 more
    
4

1 回答 1

0

这看起来对吗?

  • 不可以。创建注册表失败的原因有很多,不仅仅是因为端口正在使用中。
  • registry之后不能为空createRegistry(),因此对其进行测试毫无意义。
  • 如果您试图找到一个空闲端口,只需打开(和关闭)一个ServerSocket(). 然后在该端口上创建注册表(如果它有效)。

更大的问题是,虽然这在 java 1.6 上运行良好(主机上的每个后续服务器都使用下一个可用端口,因为如果端口不可用 LocateRegistry.createRegistry(i) 会抛出异常),但在 1.7 上则不然。

看上面。如果在任何 JDK 中的该端口上已经运行了一个注册表,则创建注册表也可能会失败。在早期的 JDK 中,如果在同一 JVM 的任何端口上运行一个 JDK,它将失败。

于 2013-02-08T00:34:32.293 回答