簡體   English   中英

以編程方式啟用遠程jmx監視

[英]Programmatically enabling remote jmx monitoring

我試圖通過JMX啟用我的核心Java應用程序以實現遠程訪問。 但是,有兩個限制使它變得比它應該更難。

a)我無權更改在linux機器上啟動應用程序的腳本。 因此,我無法將任何“jmxremote”參數傳遞給jvm。

b)我指定的遠程端口( com.sun.management.jmxremote.port = xxxx )很可能未打開,我無法修改腳本以嘗試另一個打開的端口。 我必須自動完成。

我試圖通過編寫一個類來解決這些限制,設置所有必需的jmxremote參數以及找到“免費”端口。

public class JmxRemoteConnectionHelper{

    @Override
    public void init( ) throws Exception{

        InetAddress address = InetAddress.getLocalHost();
        String ipAddress    = address.getHostAddress();
        String hostname     = address.getHostName();
        String port         = String.valueOf( getFreePort( ) );

        System.setProperty("java.rmi.server.hostname", ipAddress );
        System.setProperty("com.sun.management.jmxremote", "true" );
        System.setProperty("com.sun.management.jmxremote.authenticate", "false" );
        System.setProperty("com.sun.management.jmxremote.ssl", "false" );
        System.setProperty("com.sun.management.jmxremote.port", port  );

    }

    private final int getFreePort( ){

        **//seedPort is passed in the constructor**
        int freePort            = seedPort;
        ServerSocket sSocket    = null;

        for( int i=ZERO; i<PORT_SCAN_COUNTER; i++ ){

            try{

                freePort        = freePort + i;
                sSocket         = new ServerSocket( freePort );

               //FOUND a free port.             
                break;

            }catch( Exception e ){
                //Log

            }finally{

                if( sSocket != null ){
                    try{
                            sSocket.close();
                        sSocket = null;
                    }catch(Exception e ){
                    //Log
                    }

                }
            }

        }

        return freePort;
    }

 }

如下圖所示,I,然后通過spring初始化它。

<bean id="JmxRemoteConnection" class="JmxRemoteConnectionHelper" init-method="init" />

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" depends-on="JmxRemoteConnection" />   

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false" >
        <property name="assembler"      ref="assembler"/>
        <property name="namingStrategy" ref="namingStrategy"/>
        <property name="autodetect"     value="true"/>
        <property name="server"         ref="mbeanServer"/>
</bean>

<bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
        <property name="attributeSource" ref="jmxAttributeSource"/>
</bean>

<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy" lazy-init="true">
        <property name="attributeSource" ref="jmxAttributeSource"/>
</bean>

為了測試,我在我的Windows機器上啟動應用程序。 它正確啟動。 但是,當我在同一個盒子上調出JConsole並嘗試通過“遠程進程” (ip:port) 連接時 ,我在底部收到“連接被拒絕”消息。

我懷疑JMX代理沒有看到我正在設置的任何遠程系統屬性。

我使用的是JDK 1.6

由於您已經在使用Spring,我認為您應該看看使用ConnectorServerFactoryBean可以執行您要執行的操作。 我從來沒有必要啟動一個遠程JMX服務器,但它看起來就像那個對象可以為你做的。

請參閱此過程的答案,以便在此過程中啟用jmx:

是否可以通過編程方式啟用遠程jmx監控?

要查找一個空閑端口,只需將LocateRegistry.createRegistry()包裝在循環中,該循環將使用新端口號重試,直到成功為止。 當然,您必須將最終端口號傳達給任何需要連接的東西。 或者,在主機上運行jstatd應該可以發現它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM