ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 以编程方式启用远程jmx监视

2019-05-29 15:49:32  阅读:335  来源: 互联网

标签:java jmx spring-jmx


我试图通过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服务器,但它看起来就像那个对象可以为你做的.

标签:java,jmx,spring-jmx
来源: https://codeday.me/bug/20190529/1179071.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有