10.9 Sample JSR 77 Code

The following code provides an example of using JSR 77 to perform some management of a J2EE application server product. In this example a list of servers is obtained and their states are displayed. If one of the servers is not already stopped , an operation is invoked to stop it. This code assumes that J2EEServer supports the StateManageable interface. Most instances of J2EEServer will support StateManageable because otherwise there would be little point in providing access to the server:

 // Obtain an MEJB reference  Context ic = new InitialContext(); java.lang.Object objref = ic.lookup("ejb/mgmt/MEJB"); javax.management.j2ee.ManagementHome mejbHome =        (ManagementHome)PortableRemoteObject.narrow(objref,ManagementHome.class); javax.management.j2ee.Management mejb = mejbHome.create(); // Query to obtain a J2EEDomain object representing a management domain ObjectName query = new ObjectName("*:j2eeType=J2EEDomain,*"); Set domainSet = mejb.queryNames(query); Iterator domains = domainSet.iterator(); ObjectName domain = (ObjectName)domains.next(); // Get the list of J2EEServer objects in the domain OBJECT_NAME[] servernames = (OBJECT_NAME[])mejb.getAttribute(domain, "servers"); // Iterate over the server list, print the state of the server, // and stop the server if it isn't already stopped for (int i=0; i<servernames.length; i++) {        ObjectName serverhandle = new ObjectName(servernames[i]);        int serverstate = mejb.getAttribute(serverhandle, "state");        System.out.println("The state of server: " + servernames[i] + " is: " +             serverstate);        if (serverstate != STOPPED) {                mejb.invoke(serverhandle, "stop", null, null);        } } 

The following code example iterates over the list of servers, registers to receive events generated by the servers, and starts each server (presumably resulting in a j2ee.state.starting event from each server):

 // Obtain the event listener registry  javax.management.j2ee.ListenerRegistration listenerReg = mejb.getListenerRegistry(); // Create event listener, filter, and handback instances javax.management.NotificationListener listener = new javax.management. graphics/ccc.gif NotificationListener() {        public void handleNotification(javax.management.Notification ntfyObj,            Object handback)        {                System.out.println("Got notification: " + ntfyObj);        } }; NotificationFilterSupport filter = new NotificationFilterSupport(); filter.enableType("j2ee.state"); String handbackTag = "myId"; // Iterate over the server list, register to receive events from the server, // and start the server for (int i=0; i<servernames.length; i++) {        ObjectName serverhandle = new ObjectName(servernames[i]);        listenerReg.addNotificationListener(serverhandle, listener, filter, handbackTag);        System.out.println("Starting server: " + servernames[i]);        mejb.invoke(serverhandle, "start", null, null); } 

The next code example iterates over the servers, obtains their associated Java Virtual Machine(s), and uses the StatisticsProvider interface to obtain and display the heap size for the JVM(s):

 // Iterate over the server list, get the JVM(s) for each server and  // display the heap size for each JVM for (int i=0; i<servernames.length; i++) {        ObjectName serverhandle = new ObjectName(servernames[i]);        // Get the list of JVMs in the server        OBJECT_NAME[] jvms =            (OBJECT_NAME[])mejb.getAttribute(serverhandle,"javaVMs");        // Iterate over the JVM list and print the heap size        for (int j=0; j<jvms.length; j++) {ObjectName jvm = new ObjectName(jvms[j]);                JVMStats jvmStats = (JVMStats)mejb.getAttribute(jvm, "stats");                BoundedRangeStatic heapSize = jvmStats.getHeapSize();                System.out.println("Heap size for JVM: " + jvm + " is: " + heapSize. graphics/ccc.gif getCurrent());        } } 


Java and JMX. Building Manageable Systems
Javaв„ў and JMX: Building Manageable Systems
ISBN: 0672324083
EAN: 2147483647
Year: 2000
Pages: 115

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net