JMX を介してリモート アクセスできるようにコア Java アプリケーションを有効にしようとしています。ただし、2 つの制限により、必要以上に難しくなっています。
a) Linux ボックスでアプリを起動するスクリプトを自由に変更することはできません。したがって、「jmxremote」パラメーターを jvm に渡すことはできません。
com.sun.management.jmxremote.port = xxxx
b)指定したリモート ポート ( ) が開いていない可能性が非常に高く、スクリプトを変更して別の開いているポートを試すことができません。私は自動的にそれをしなければなりません。
必要なすべての 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を使用しています。