2016-08-04 66 views
0

我在使用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。

  1. nodemon無法重新啓動節點。它只是顯示:

    [nodemon] restarting due to changes... 
    

    但它並不實際重新啓動。似乎jmx正在將未處理的東西,資源打開或某些東西留下,以至於nodemon無法正確關閉節點。

  2. 我也注意到,當直接使用節點運行應用程序時,我必須發送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... 
    

回答

0

我找到了解決方案,我的問題,這有兩方面的原因,我很樂意與他人分享,也可以找到這個頁面:

  1. 問題與節點java做實現,由節點jmx模塊使用。我找不到任何解釋,但在這裏陳述了一個解決方案: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

希望幫助別人!