読者です 読者をやめる 読者になる 読者になる

AKAI TSUKI

System development or Technical something

JMXを利用するテスト


利用したのはJava6u16。

まず、MXBeanを準備する。

package jp.person.suto.logic;

import javax.management.MXBean;

@MXBean
public interface Counter
{

    public int up();
    
    public int showNum();
    
    public void reset();

}

Counterインタフェースを実装したのが以下。

package jp.person.suto.logic;

public class CounterImpl implements Counter
{
    private int num_ = 0;

    public int up()
    {
        this.num_++;
        return this.num_;
    }
    
    public int showNum()
    {
        return this.num_;
    }
    
    public void reset()
    {
        this.num_ = 0;
    }

}

さらに、JMXサーバにMXBeanを登録するところ。

package jp.person.suto;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import jp.person.suto.logic.Counter;
import jp.person.suto.logic.CounterImpl;

public class JMXTestMain
{
    public static String ADDRESS = "service:jmx:rmi:///jndi/rmi://localhost/hello";

    public static void main(String[] args)
    {
        try
        {
            Counter target = new CounterImpl();

            LocateRegistry.createRegistry(1099);

            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            ObjectName name = new ObjectName("suto.jmx:type=Counter");
            server.registerMBean(target, name);

            JMXServiceURL url = new JMXServiceURL(ADDRESS);
            JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url,
                    null, server);

            connector.start();
            System.out.println("JMX Start");

            while (true)
            {
                System.out.println("--------------------");
                System.out.println("please, input key");
                System.out.println("    up     ->  (u)");
                System.out.println("    reset  ->  (r)");
                System.out.println("    show   ->  (s)");
                System.out.println("    end    ->  (e)");
                System.out.print(" : ");

                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                try
                {
                    String str = reader.readLine();

                    if ("u".equals(str))
                    {
                        System.out.println("--------------------");
                        System.out.println("カウントアップします。");
                        System.out.println("・・・");
                        int result = target.up();
                        System.out.println("値:" + result + "になりました。");
                    }
                    else if ("r".equals(str))
                    {
                        System.out.println("--------------------");
                        System.out.println("リセットします。");
                        target.reset();
                    }
                    else if ("s".equals(str))
                    {
                        System.out.println("--------------------");
                        int num = target.showNum();
                        System.out.println("現在の値は" + num + "です。");
                    }
                    else if ("e".equals(str))
                    {
                        System.out.println("--------------------");
                        System.out.println("終了します。");
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        catch (RemoteException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        catch (MalformedObjectNameException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (NullPointerException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (InstanceAlreadyExistsException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MBeanRegistrationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (NotCompliantMBeanException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

あれ〜JMXに繋がらない。。。

jconsoleで上のjMXにアクセスを試みましたがつながらない。。。

JMXクライアントのテストサンプル

どっかで誰かが作ってたやつを真似してます。

package jp.person.suto;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.sun.corba.se.spi.activation.Server;

public class TestClient
{
    public static String ADDRESS = "service:jmx:rmi:///jndi/rmi://localhost/hello";

    public static void main(String[] args) throws Exception
    {
        JMXServiceURL url = new JMXServiceURL(ADDRESS); // サーバへのURL
        JMXConnector connector = JMXConnectorFactory.connect(url); // URLから接続してくれる人を作る
        MBeanServerConnection conn = connector.getMBeanServerConnection(); // コネクションを取得

        ObjectName name = new ObjectName("suto.jmx:type=Counter"); // 登録されてるオブジェクトに対して
        Integer ret = (Integer) conn.invoke(name, "showNum", null, null); // shoNumを呼び出し!
        System.out.println(ret);
    }

}

テストサンプルを書いて試してみた。。。

下のような例外が発生しましたよ。。。orz

Exception in thread "main" java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
	java.net.SocketTimeoutException: Read timed out]
	at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:338)
	at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)
	at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:207)
	at jp.person.suto.TestClient.main(TestClient.java:18)
Caused by: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
	java.net.SocketTimeoutException: Read timed out]
	at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:101)
	at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:185)
	at javax.naming.InitialContext.lookup(InitialContext.java:392)
	at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1886)
	at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1856)
	at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:257)
	... 3 more
Caused by: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
	java.net.SocketTimeoutException: Read timed out
	at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:286)
	at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
	at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
	at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
	at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:97)
	... 8 more
Caused by: java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:129)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
	at java.io.DataInputStream.readByte(DataInputStream.java:248)
	at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:228)
	... 12 more