繁体   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