【问题标题】:nodeJs and jmx (npm: jmx)nodeJs 和 jmx (npm: jmx)
【发布时间】:2016-08-04 17:37:45
【问题描述】:

我在使用 jmx nodeJs 包与来自 nodeJs 的 Java 应用程序进行桥接和通信时遇到问题(请参阅https://www.npmjs.com/package/jmx)。我的代码如下所示:

var jmx = require('jmx');

var jmxTester = function(host, port) {
  var jmxrmi = 'service:jmx:rmi:///jndi/rmi://' + host + ':' + port + '/jmxrmi';
  winston.debug('jmxrmi is: ', jmxrmi);

  var client = jmx.createClient({
    service: jmxrmi
  });

  client.on('error', function (e) {
    winston.silly('Error: JMX at %s:%d', host, port, e);
    client.disconnect();
  });

  client.on('disconnect', function (err) {
    winston.silly('Successfully disconnected from client.');
  });

  client.on('connect', function() {
    winston.silly('Successfully connected to client.');
    client.disconnect();
  });

  client.connect();
}

module.exports = jmxTester;

使用它:

var jmxTester = require('jmxTester');

jmxTester('localhost', 20000);

遇到了几个问题,与我在 localhost:2000 上运行的 jmx 或我没有任何运行的事实无关。

  1. nodemon 无法再重新启动节点。它只是显示:

    [nodemon] restarting due to changes...
    

    但它实际上并没有重新启动。似乎 jmx 留下了未处理的东西,打开了资源或其他东西,因此 nodemon 无法正确关闭节点。

  2. 我还注意到,当直接使用 node 运行应用程序时,我必须发送一个 kill -9,而不是一个“正常”的 kill。如果我删除 jmxFun 调用,一切正常。那么jmx有什么问题呢?

谁能帮我弄清楚如何在nodeJs中正确使用jmx?这样我就可以进行干净的连接和断开连接(出现错误且没有任何错误)。

以下是一些示例输出:

  1. localhost:2000 上运行 JMX 应用程序(之后进行两次文件更改,不会重新启动):

    2016-08-04T17:20:06.423Z - debug: jmxrmi is: service:jmx:rmi:///jndi/rmi://localhost:2000/jmxrmi
    2016-08-04T17:20:06.433Z - silly: Successfully connected to client.
    2016-08-04T17:20:06.434Z - silly: Successfully disconnected from client.
    [nodemon] restarting due to changes...
    [nodemon] restarting due to changes...
    
  2. localhost:2000 上没有正在运行的 JMX 应用程序(之后进行两次文件更改,不会发生重新启动):

    2016-08-04T17:34:28.384Z - debug: jmxrmi is:  service:jmx:rmi:///jndi/rmi://localhost:2000/jmxrmi
    2016-08-04T17:34:28.390Z - silly: Error: JMX at localhost:2000 { [Error: Error running static method
    java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
        java.net.ConnectException: Connection refused]
        at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:369)
        at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:270)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
    Caused by: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
        java.net.ConnectException: Connection refused]
        at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:122)
        at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:205)
        at javax.naming.InitialContext.lookup(InitialContext.java:417)
        at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1955)
        at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1922)
        at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:287)
        ... 5 more
        [...]
    [nodemon] restarting due to changes...
    [nodemon] restarting due to changes...
    

【问题讨论】:

    标签: java node.js jmx


    【解决方案1】:

    我找到了我的问题的解决方案,这有两个原因,我很想与其他人分享,可能会找到这个页面:

    1. 问题与 node jmx 模块使用的 node java 实现有关。我找不到任何解释,但这里说明了一个解决方案:https://github.com/remy/nodemon/issues/573。所以我将以下内容添加到我的 main.js 中,以便在绑定到我的 process.on 之前,java 被“使用”:

      var java = require('java');
      java.import("javax.management.remote.JMXConnectorFactory");
      
    2. 应该减少 RMI 超时,否则节点会等待响应(感谢 node-jmx 的同步实现)。因此,您应该:

      var System = java.import('java.lang.System');
      System.setProperty('sun.rmi.transport.connectionTimeout', '500');
      System.setProperty('sun.rmi.transport.tcp.handshakeTimeout', '500');
      System.setProperty('sun.rmi.transport.tcp.responseTimeout', '500');
      System.setProperty('sun.rmi.transport.tcp.readTimeout', '500');
      

      第一次测试表明,对于某些超时,500 的值可能有点过于乐观。因为即使 JMX 可用,我有时也会超时(感谢 Java RMI - Client Timeout

    希望对其他人有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-03-30
      • 2020-02-24
      • 2020-02-26
      • 2011-07-03
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多