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

AKAI TSUKI

System development or Technical something

JMXの呼び出し

上のオブジェクトをJMXに登録してみる。

実行クラス

package jp.person.akaitsuki.sample;

public class JMXSampleMain {
	
	public static void main(String[] args) {
		
		ConnectionStatusManager mgr = ConnectionStatusManager.getInstance();
		
		
	}

}
package jp.person.akaitsuki.sample;

import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;


public class ConnectionStatusManager {

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

	private static ObjectTable objTable_ = null;
	private static ConnectionStatusManager mgr_ = null; 
	
	private ConnectionStatusManager()
	{
	}
	
	public static ConnectionStatusManager getInstance()
	{
		if( null == mgr_)
		{
			mgr_ = new ConnectionStatusManager();
			objTable_ = new ObjectTableInfo();
			objTable_.initTable(2, 3);
			
			try
			{
				//jmxに登録処理
				LocateRegistry.createRegistry(1099);

	            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
	            ObjectName name = new ObjectName("akaitsuki.jmx:type=ObjectTable");
	            server.registerMBean(objTable_, name);

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

	            connector.start();
	            System.out.println("JMX Start");
				
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
				
			}

		}
		
		return mgr_;
	}
	
	public void init(int id1Num, int id2Num)
	{
		objTable_.initTable(id1Num, id2Num);
	}

}