gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:05 25 4
gpt4 key购买 nike

我正在尝试启用我的核心 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com