我在使用jmx nodeJs包與nodeJs中的Java應用程序橋接和通信時遇到問題(請參閱https://www.npmjs.com/package/jmx)。我的代碼看起來像這樣:nodeJs和jmx(npm: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無法正確關閉節點。
我也注意到,當直接使用節點運行應用程序時,我必須發送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...