JMX服務端和客戶端的代碼

 

JMX服務端和客戶端的代碼

服務端代碼如下
Java代碼 複製代碼 收藏代碼
  1. package com.rmi;   
  2.   
  3. import java.rmi.registry.LocateRegistry;   
  4. import java.util.HashMap;   
  5. import java.util.Map;   
  6.   
  7. import javax.management.MBeanServer;   
  8. import javax.management.MBeanServerFactory;   
  9. import javax.management.ObjectName;   
  10. import javax.management.remote.JMXConnectorServer;   
  11. import javax.management.remote.JMXConnectorServerFactory;   
  12. import javax.management.remote.JMXServiceURL;   
  13.   
  14. public class Server {   
  15.   
  16.     /**  
  17.      * @param args  
  18.      * @throws Exception  
  19.      */  
  20.     public static void main(String[] args) throws Exception {   
  21.         LocateRegistry.createRegistry(1234);//必須要這句,參數爲端口號   
  22.         ObjectName helloName = new ObjectName("QianYu:type=oamp,name=hello");   
  23.         HelloWorld hello = new HelloWorld();   
  24.         MBeanServer server = MBeanServerFactory.createMBeanServer();   
  25.         // provide a valid username and password (e.g., via program arguments)   
  26.         String user = "monitorRole";   
  27.         String pw = "password";   
  28.   
  29.         // place the username and password in a string array of credentials that   
  30.         // can be used when making the connection to the remote JMX agent   
  31.         String[] credentials = new String[] { user, pw };   
  32.         // the string array of credentials is placed in a map keyed against the   
  33.         // well-defined credentials identifier string   
  34.         Map<String, String[]> props = new HashMap<String, String[]>();   
  35.         props.put("jmx.remote.credentials", credentials);   
  36.         // supply the map of credentials to the connect call   
  37.         JMXServiceURL address =   
  38.           new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1234/jmxrmi");   
  39.   
  40.         JMXConnectorServer cserver =   
  41.             JMXConnectorServerFactory.newJMXConnectorServer(address, props, server);   
  42.         cserver.start();   
  43.         server.registerMBean(hello, helloName);   
  44.   
  45.     }   
  46.   
  47. }  
package com.rmi;

import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import java.util.Map;

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

public class Server {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		LocateRegistry.createRegistry(1234);//必須要這句,參數爲端口號
		ObjectName helloName = new ObjectName("QianYu:type=oamp,name=hello");
        HelloWorld hello = new HelloWorld();
        MBeanServer server = MBeanServerFactory.createMBeanServer();
		// provide a valid username and password (e.g., via program arguments)
	    String user = "monitorRole";
	    String pw = "password";

	    // place the username and password in a string array of credentials that
	    // can be used when making the connection to the remote JMX agent
	    String[] credentials = new String[] { user, pw };
	    // the string array of credentials is placed in a map keyed against the
	    // well-defined credentials identifier string
	    Map<String, String[]> props = new HashMap<String, String[]>();
	    props.put("jmx.remote.credentials", credentials);
	    // supply the map of credentials to the connect call
	    JMXServiceURL address =
	      new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1234/jmxrmi");

	    JMXConnectorServer cserver =
            JMXConnectorServerFactory.newJMXConnectorServer(address, props, server);
	    cserver.start();
	    server.registerMBean(hello, helloName);

	}

}



以下是客戶端代碼:
Java代碼 複製代碼 收藏代碼
  1. package com.rmi;   
  2.   
  3. import java.util.HashMap;   
  4. import java.util.Hashtable;   
  5. import java.util.Map;   
  6.   
  7. import javax.management.JMX;   
  8. import javax.management.MBeanServerConnection;   
  9. import javax.management.ObjectInstance;   
  10. import javax.management.ObjectName;   
  11. import javax.management.remote.JMXConnector;   
  12. import javax.management.remote.JMXConnectorFactory;   
  13. import javax.management.remote.JMXServiceURL;   
  14.   
  15. public class Client {   
  16.   
  17.     /**  
  18.      * @param args  
  19.      * @throws Exception  
  20.      */  
  21.     public static void main(String[] args) throws Exception {   
  22.         // provide a valid username and password (e.g., via program arguments)   
  23.         String user = "monitorRole";   
  24.         String pw = "password";   
  25.   
  26.         // place the username and password in a string array of credentials that   
  27.         // can be used when making the connection to the remote JMX agent   
  28.         String[] credentials = new String[] { user, pw };   
  29.         // the string array of credentials is placed in a map keyed against the   
  30.         // well-defined credentials identifier string   
  31.         Map<String, String[]> props = new HashMap<String, String[]>();   
  32.         props.put("jmx.remote.credentials", credentials);   
  33.         // supply the map of credentials to the connect call   
  34.         JMXServiceURL address =   
  35.           new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1234/jmxrmi");   
  36.         JMXConnector connector = JMXConnectorFactory.connect(address, props);   
  37.         // it is a trivial matter to get a reference for the MBean server   
  38.         // connection to the remote agent   
  39.         MBeanServerConnection mbsc = connector.getMBeanServerConnection();   
  40.   
  41.         connector.connect();   
  42.   
  43.         ObjectName objectName=new ObjectName("hello:name=hello");   
  44.         if(!mbsc.isRegistered(objectName)){   
  45.             mbsc.createMBean("com.rmi.HelloWorld", objectName,null,null);   
  46.         }   
  47.   
  48.         System.out.println("\nMBean count = " + mbsc.getMBeanCount());   
  49.         for (ObjectInstance object : mbsc.queryMBeans(nullnull)) {//服務器上的所有MBean   
  50.             System.out.println("object.getObjectName="+object.getObjectName());   
  51.         }   
  52.   
  53.         //hello對象屬於遠程的對象,調用sayHello方法,則是調用服務器端的sayHello方法   
  54.         final HelloWorldMBean hello = JMX.newMBeanProxy(mbsc, objectName,   
  55.                     HelloWorldMBean.class);   
  56. //      hello.setName("Peter");   
  57.         hello.sayHello();   
  58.   
  59. //      ObjectName unObjectName=new ObjectName("hello:name=hello");   
  60. //      Hashtable map=new Hashtable();   
  61. //      map.put("name", "hello");   
  62.   
  63.         //unregister MBean   
  64. //      ObjectName unObjectName=new ObjectName("hello",map);   
  65. //      ObjectName unObjectName=ObjectName.getInstance("hello:name=hell*");   
  66. //      for (ObjectInstance object : mbsc.queryMBeans(unObjectName, null)) {//這裏可根據查詢結果進行註銷MBean   
  67. //           mbsc.unregisterMBean(object.getObjectName());   
  68. //        }   
  69.         System.out.println("\nMBean count = " + mbsc.getMBeanCount());   
  70.         System.out.println("end");   
  71.     }   
  72.   
  73. }  
package com.rmi;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

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

public class Client {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// provide a valid username and password (e.g., via program arguments)
	    String user = "monitorRole";
	    String pw = "password";

	    // place the username and password in a string array of credentials that
	    // can be used when making the connection to the remote JMX agent
	    String[] credentials = new String[] { user, pw };
	    // the string array of credentials is placed in a map keyed against the
	    // well-defined credentials identifier string
	    Map<String, String[]> props = new HashMap<String, String[]>();
	    props.put("jmx.remote.credentials", credentials);
	    // supply the map of credentials to the connect call
	    JMXServiceURL address =
	      new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1234/jmxrmi");
	    JMXConnector connector = JMXConnectorFactory.connect(address, props);
	    // it is a trivial matter to get a reference for the MBean server
	    // connection to the remote agent
	    MBeanServerConnection mbsc = connector.getMBeanServerConnection();

	    connector.connect();

	    ObjectName objectName=new ObjectName("hello:name=hello");
	    if(!mbsc.isRegistered(objectName)){
			mbsc.createMBean("com.rmi.HelloWorld", objectName,null,null);
		}

	    System.out.println("\nMBean count = " + mbsc.getMBeanCount());
	    for (ObjectInstance object : mbsc.queryMBeans(null, null)) {//服務器上的所有MBean
            System.out.println("object.getObjectName="+object.getObjectName());
        }

	    //hello對象屬於遠程的對象,調用sayHello方法,則是調用服務器端的sayHello方法
	    final HelloWorldMBean hello = JMX.newMBeanProxy(mbsc, objectName,
	    			HelloWorldMBean.class);
//	    hello.setName("Peter");
	    hello.sayHello();

//	    ObjectName unObjectName=new ObjectName("hello:name=hello");
//	    Hashtable map=new Hashtable();
//	    map.put("name", "hello");

	    //unregister MBean
//	    ObjectName unObjectName=new ObjectName("hello",map);
//	    ObjectName unObjectName=ObjectName.getInstance("hello:name=hell*");
//	    for (ObjectInstance object : mbsc.queryMBeans(unObjectName, null)) {//這裏可根據查詢結果進行註銷MBean
//	    	 mbsc.unregisterMBean(object.getObjectName());
//        }
	    System.out.println("\nMBean count = " + mbsc.getMBeanCount());
	    System.out.println("end");
	}

}


以下是HelloWorldMBean
Java代碼 複製代碼 收藏代碼
  1. package com.rmi;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. public interface HelloWorldMBean extends Serializable{   
  6.     void setName(String name);   
  7.   
  8.     String getName();   
  9.   
  10.     void sayHello();   
  11.   
  12.     String getHelloString();   
  13.   
  14.     int getId();   
  15.   
  16.     String toString();   
  17.   
  18.     int hashCode();   
  19.   
  20.     boolean equals( Object obj);   
  21.   
  22.     HelloWorldMBean getThis();   
  23. }  
package com.rmi;

import java.io.Serializable;

public interface HelloWorldMBean extends Serializable{
	void setName(String name);

    String getName();

    void sayHello();

    String getHelloString();

    int getId();

    String toString();

    int hashCode();

    boolean equals( Object obj);

    HelloWorldMBean getThis();
}



以下是HelloWorld.java類:
Java代碼 複製代碼 收藏代碼
  1. package com.rmi;   
  2.   
  3. import java.io.Serializable;   
  4. import java.util.concurrent.atomic.AtomicInteger;   
  5.   
  6. import javax.management.AttributeChangeNotification;   
  7. import javax.management.MBeanRegistration;   
  8. import javax.management.MBeanServer;   
  9. import javax.management.NotificationBroadcasterSupport;   
  10. import javax.management.ObjectName;   
  11.   
  12. public class HelloWorld implements HelloWorldMBean, Serializable{   
  13.   
  14.     static AtomicInteger count = new AtomicInteger();   
  15.     private static final long serialVersionUID = 1627976932729278650L;   
  16.     int id = 0;   
  17.     String name;   
  18.   
  19.     public synchronized void setName(String name) {   
  20.         this.name = name;   
  21.     }   
  22.   
  23.     public synchronized String getName() {   
  24.         return name;   
  25.     }   
  26.   
  27.     public synchronized void sayHello() {   
  28.         System.out.println(getHelloString());   
  29.     }   
  30.   
  31.     public synchronized String getHelloString() {   
  32. //      System.out.println("1111");   
  33.         return "Hello, " + name;   
  34.     }   
  35.   
  36.     public synchronized int getId() {   
  37.         return id;   
  38.     }   
  39.   
  40.     public HelloWorldMBean getThis() {   
  41.         return this;   
  42.     }   
  43.   
  44. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章