【发布时间】: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 或我没有任何运行的事实无关。
-
nodemon 无法再重新启动节点。它只是显示:
[nodemon] restarting due to changes...但它实际上并没有重新启动。似乎 jmx 留下了未处理的东西,打开了资源或其他东西,因此 nodemon 无法正确关闭节点。
我还注意到,当直接使用 node 运行应用程序时,我必须发送一个 kill -9,而不是一个“正常”的 kill。如果我删除 jmxFun 调用,一切正常。那么jmx有什么问题呢?
谁能帮我弄清楚如何在nodeJs中正确使用jmx?这样我就可以进行干净的连接和断开连接(出现错误且没有任何错误)。
以下是一些示例输出:
-
在
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... -
在
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...
【问题讨论】: