java - 以编程方式启用远程 jmx 监控

标签 java jmx spring-jmx

我正在尝试启用我的核心 Java 应用程序以通过 JMX 进行远程访问。 然而,两个限制使它变得比应该的更难。

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;
    }

 }

如下图,我,然后通过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 监控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560407/

相关文章:

java - 是否有用于 Java 的 HttpCahce 客户端库?

java - 从当前数据库 Java-Agent 调用另一个数据库的 java-Agent

java - 如何通过 JMS 进行 JMX?

java - JMX_exporter 与 Prometheus 和 Grafana

Spring框架监控ThreadPoolTask​​Executor队列大小

java - 避免在 JEditorPane 中使用 <p> 中的填充/边距

java - New Relic Heroku Java 安装失败

java - 无法获取 JMX 服务 URL

java - com.sun.jdmk.comm 包不存在

java - Spring boot devtools 无法重新启动应用程序