001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package example.jmx.monitor;
009:
010: import java.util.Vector;
011: import java.util.Enumeration;
012: import javax.management.MBeanServerFactory;
013: import javax.management.ObjectName;
014: import javax.management.MalformedObjectNameException;
015: import javax.management.MBeanServer;
016: import javax.management.MBeanAttributeInfo;
017: import javax.management.monitor.CounterMonitor;
018:
019: import example.jmx.standard.SimpleStandard;
020: import example.jmx.standard.SimpleStandardMBean;
021:
022: /**
023: *
024: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
025: */
026:
027: public class MonitorAgent2 {
028:
029: /*
030: * ------------------------------------------
031: * PRIVATE VARIABLES
032: * ------------------------------------------
033: */
034:
035: private MBeanServer server = null;
036: private AgentListener2 listener = null;
037:
038: private StandardObservedObject standardObsObj = null;
039: private DynamicObservedObject dynamicObsObj = null;
040:
041: private CounterMonitor counterMonitor = null;
042:
043: // Object names.
044: //
045: ObjectName standardObsObjName = null;
046: ObjectName dynamicObsObjName = null;
047: ObjectName counterMonitorName = null;
048:
049: // Class names.
050: //
051: static String standardObsObjClass = StandardObservedObject.class
052: .getName();
053: static String dynamicObsObjClass = DynamicObservedObject.class
054: .getName();
055: static String counterMonitorClass = "javax.management.monitor.CounterMonitor";
056:
057: // Vector containing all the Simple MBeans names.
058: //
059: Vector simpleNameVector = null;
060:
061: /*
062: * ------------------------------------------
063: * CONSTRUCTORS
064: * ------------------------------------------
065: */
066:
067: public MonitorAgent2() {
068:
069: server = MBeanServerFactory.createMBeanServer();
070: listener = new AgentListener2();
071:
072: standardObsObj = new StandardObservedObject();
073: dynamicObsObj = new DynamicObservedObject();
074:
075: counterMonitor = new CounterMonitor();
076: }
077:
078: /*
079: * ------------------------------------------
080: * PUBLIC METHODS
081: * ------------------------------------------
082: */
083:
084: public static void main(String[] args) {
085:
086: trace("\n>>> CREATE and START the Agent...");
087: MonitorAgent2 agent = new MonitorAgent2();
088:
089: // Initialize the CounterMonitor MBean.
090: //
091: if (agent.initializeCounter() != 0) {
092: trace("\n>>> An error occurred when initializing the CounterMonitor MBean...");
093: System.exit(1);
094: }
095:
096: // MANAGEMENT OF A STANDARD MBEAN.
097: //
098:
099: // Initialize the Standard Observed MBean.
100: // Update and doStart the CounterMonitor MBean.
101: //
102: if (agent.initializeStandardMBean() != 0) {
103: trace("\n>>> An error occurred when initializing the Standard Observed MBean...");
104: System.exit(1);
105: }
106:
107: // Create 6 instances of Simple MBeans.
108: //
109: agent.populateTheAgent();
110:
111: // Delete the 6 instances of Simple MBeans.
112: //
113: agent.depopulateTheAgent();
114:
115: // Finalize the Standard Observed MBean.
116: // Stop the CounterMonitor MBean.
117: //
118: if (agent.finalizeStandardMBean() != 0) {
119: trace("\n>>> An error occurred when finalizing the Standard Observed MBean...");
120: System.exit(1);
121: }
122:
123: // MANAGEMENT OF A DYNAMIC MBEAN.
124: //
125:
126: // Initialize the Dynamic Observed MBean.
127: // Update and doStart the CounterMonitor MBean.
128: //
129: if (agent.initializeDynamicMBean() != 0) {
130: trace("\n>>> An error occurred when initializing the Dynamic Observed MBean...");
131: System.exit(1);
132: }
133:
134: // Create 6 instances of Simple MBeans.
135: //
136: agent.populateTheAgent();
137:
138: // Delete the 6 instances of Simple MBeans.
139: //
140: agent.depopulateTheAgent();
141:
142: // Finalize the Dynamic Observed MBean.
143: // Stop the CounterMonitor MBean.
144: //
145: if (agent.finalizeDynamicMBean() != 0) {
146: trace("\n>>> An error occurred when finalizing the Dynamic Observed MBean...");
147: System.exit(1);
148: }
149:
150: // Finalize the CounterMonitor MBean.
151: //
152: if (agent.finalizeCounter() != 0) {
153: trace("\n>>> An error occurred when finalizing the CounterMonitor MBean...");
154: System.exit(1);
155: }
156:
157: trace("\n>>> STOP the Agent...\n");
158: System.exit(0);
159: }
160:
161: /*
162: * ------------------------------------------
163: * PRIVATE METHODS
164: * ------------------------------------------
165: */
166:
167: private int initializeCounter() {
168:
169: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
170:
171: // Get the domain name from the MBeanServer.
172: //
173: String domain = server.getDefaultDomain();
174:
175: // Create a new CounterMonitor MBean and add it to the MBeanServer.
176: //
177: trace("\n>>> CREATE a new CounterMonitor MBean:");
178: try {
179: counterMonitorName = new ObjectName(domain + ":name="
180: + counterMonitorClass);
181: } catch (MalformedObjectNameException e) {
182: e.printStackTrace();
183: return 1;
184: }
185: trace("\tOBJECT NAME = " + counterMonitorName);
186: try {
187: server.registerMBean(counterMonitor, counterMonitorName);
188: } catch (Exception e) {
189: e.printStackTrace();
190: return 1;
191: }
192:
193: // Register a CounterMonitor notification listener with the CounterMonitor MBean,
194: // enabling the Agent to receive CounterMonitor notification emitted by the CounterMonitor.
195: //
196: trace("\n>>> ADD a listener to the CounterMonitor...");
197: try {
198: counterMonitor
199: .addNotificationListener(listener, null, null);
200: } catch (Exception e) {
201: e.printStackTrace();
202: return 1;
203: }
204:
205: return 0;
206: }
207:
208: private int initializeStandardMBean() {
209:
210: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
211:
212: // Get the domain name from the MBeanServer.
213: //
214: String domain = server.getDefaultDomain();
215:
216: // Create a new StandardObservedObject MBean and add it to the MBeanServer.
217: //
218: trace("\n>>> CREATE a new StandardObservedObject MBean:");
219: try {
220: standardObsObjName = new ObjectName(domain + ":name="
221: + standardObsObjClass);
222: } catch (MalformedObjectNameException e) {
223: e.printStackTrace();
224: return 1;
225: }
226: trace("\tOBJECT NAME = " + standardObsObjName);
227: try {
228: server.registerMBean(standardObsObj, standardObsObjName);
229: } catch (Exception e) {
230: e.printStackTrace();
231: return 1;
232: }
233:
234: // Access the attributes of the StandardObservedObject MBean and get the initial attribute values.
235: //
236: trace("\tATTRIBUTE \"NbObjects\" = "
237: + standardObsObj.getNbObjects());
238:
239: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
240:
241: // Initialize the attributes of the CounterMonitor MBean.
242: //
243: Integer threshold = new Integer(6);
244: Integer offset = new Integer(2);
245:
246: trace("\n>>> SET the attributes of the CounterMonitor:");
247:
248: try {
249: counterMonitor.addObservedObject(standardObsObjName);
250: trace("\tATTRIBUTE \"ObservedObject\" = "
251: + standardObsObjName);
252:
253: counterMonitor.setObservedAttribute("NbObjects");
254: trace("\tATTRIBUTE \"ObservedAttribute\" = NbObjects");
255:
256: counterMonitor.setNotify(true);
257: trace("\tATTRIBUTE \"Notify\" = true");
258:
259: counterMonitor.setInitThreshold(threshold);
260: trace("\tATTRIBUTE \"Threshold\" = " + threshold);
261:
262: counterMonitor.setOffset(offset);
263: trace("\tATTRIBUTE \"Offset\" = " + offset);
264:
265: counterMonitor.setGranularityPeriod(1000);
266: trace("\tATTRIBUTE \"GranularityPeriod\" = 1 second");
267: } catch (Exception e) {
268: e.printStackTrace();
269: return 1;
270: }
271:
272: trace("\n>>> START the CounterMonitor...");
273: counterMonitor.start();
274:
275: return 0;
276: }
277:
278: private int initializeDynamicMBean() {
279:
280: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
281: try {
282: trace("\n>>> PRESS <Enter> TO CREATE a new DynamicObservedObject");
283: System.in.read();
284: } catch (Exception e) {
285: e.printStackTrace();
286: System.exit(1);
287: }
288:
289: // Get the domain name from the MBeanServer.
290: //
291: String domain = server.getDefaultDomain();
292:
293: // Create a new DynamicObservedObject MBean and add it to the MBeanServer.
294: //
295: trace("\n>>> CREATE a new DynamicObservedObject MBean:");
296: try {
297: dynamicObsObjName = new ObjectName(domain + ":name="
298: + dynamicObsObjClass);
299: } catch (MalformedObjectNameException e) {
300: e.printStackTrace();
301: return 1;
302: }
303: trace("\tOBJECT NAME = " + dynamicObsObjName);
304: try {
305: server.registerMBean(dynamicObsObj, dynamicObsObjName);
306: } catch (Exception e) {
307: e.printStackTrace();
308: return 1;
309: }
310:
311: // Access the attributes of the DynamicObservedObject MBean and get the initial attribute values.
312: //
313: MBeanAttributeInfo[] attributes = dynamicObsObj.getMBeanInfo()
314: .getAttributes();
315: for (int i = 0; i < attributes.length; i++) {
316: try {
317: trace("\tATTRIBUTE \""
318: + attributes[i].getName()
319: + "\" = "
320: + dynamicObsObj.getAttribute(attributes[i]
321: .getName()));
322: } catch (Exception e) {
323: e.printStackTrace();
324: return 1;
325: }
326: }
327:
328: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
329:
330: // Initialize the attributes of the CounterMonitor MBean.
331: //
332: Integer threshold = new Integer(6);
333:
334: trace("\n>>> SET the attributes of the CounterMonitor:");
335:
336: try {
337:
338: counterMonitor.addObservedObject(dynamicObsObjName);
339: trace("\tATTRIBUTE \"ObservedObject\" = "
340: + dynamicObsObjName);
341:
342: counterMonitor
343: .setObservedAttribute(attributes[0].getName());
344: trace("\tATTRIBUTE \"ObservedAttribute\" = "
345: + counterMonitor.getObservedAttribute());
346:
347: trace("\tATTRIBUTE \"Notify\" = "
348: + counterMonitor.getNotify());
349:
350: counterMonitor.setInitThreshold(threshold);
351: trace("\tATTRIBUTE \"Threshold\" = " + threshold);
352:
353: trace("\tATTRIBUTE \"Offset\" = "
354: + counterMonitor.getOffset());
355:
356: trace("\tATTRIBUTE \"GranularityPeriod\" = 1 second");
357: } catch (Exception e) {
358: e.printStackTrace();
359: return 1;
360: }
361:
362: // trace("\n>>> START the CounterMonitor...");
363:
364: try {
365: trace("\n>>> PRESS <Enter> TO START the CounterMonitor");
366: System.in.read();
367: } catch (Exception e) {
368: e.printStackTrace();
369: System.exit(1);
370: }
371: counterMonitor.start();
372:
373: return 0;
374: }
375:
376: private int finalizeCounter() {
377:
378: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
379:
380: // Remove the CounterMonitor notification listener.
381: //
382: trace("\n>>> REMOVE the CounterMonitor listener");
383: try {
384: counterMonitor.removeNotificationListener(listener);
385: } catch (Exception e) {
386: e.printStackTrace();
387: return 1;
388: }
389:
390: // Delete the CounterMonitor MBean.
391: //
392: trace("\n>>> DELETE the CounterMonitor MBean:");
393: trace("\tOBJECT NAME = " + counterMonitorName);
394: try {
395: server.unregisterMBean(counterMonitorName);
396: } catch (Exception e) {
397: e.printStackTrace();
398: return 1;
399: }
400: return 0;
401: }
402:
403: private int finalizeStandardMBean() {
404:
405: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
406:
407: trace("\n>>> STOP the CounterMonitor...");
408: counterMonitor.stop();
409:
410: // Deletes the StandardObservedObject MBean.
411: //
412: trace("\n>>> DELETE the StandardObservedObject MBean:");
413: trace("\tOBJECT NAME = " + standardObsObjName);
414: try {
415: server.unregisterMBean(standardObsObjName);
416: } catch (Exception e) {
417: e.printStackTrace();
418: return 1;
419: }
420: return 0;
421: }
422:
423: private int finalizeDynamicMBean() {
424:
425: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
426:
427: trace("\n>>> STOP the CounterMonitor...");
428: counterMonitor.stop();
429:
430: // Deletes the DynamicObservedObject MBean.
431: //
432: trace("\n>>> DELETE the DynamicObservedObject MBean:");
433: trace("\tOBJECT NAME = " + dynamicObsObjName);
434: try {
435: server.unregisterMBean(dynamicObsObjName);
436: } catch (Exception e) {
437: e.printStackTrace();
438: return 1;
439: }
440: return 0;
441: }
442:
443: private void populateTheAgent() {
444:
445: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
446:
447: // Get the domain name from the MBeanServer.
448: //
449: String domain = server.getDefaultDomain();
450:
451: try {
452: trace("\n>>> PRESS <Enter> TO START THE CREATION OF 6 SIMPLE BEANS...");
453: System.in.read();
454: } catch (Exception e) {
455: e.printStackTrace();
456: System.exit(1);
457: }
458:
459: // Create 6 instances of Simple MBeans and add them to the MBeanServer.
460: //
461: simpleNameVector = new Vector();
462: for (int count = 0; count < 6; count++) {
463: SimpleStandardMBean simpleBean = new SimpleStandard();
464: ObjectName simpleName = null;
465: try {
466: simpleName = new ObjectName(domain
467: + ":name=Simple,number=" + count);
468: } catch (MalformedObjectNameException e) {
469: e.printStackTrace();
470: System.exit(1);
471: }
472: try {
473: server.registerMBean(simpleBean, simpleName);
474: } catch (Exception e) {
475: e.printStackTrace();
476: System.exit(1);
477: }
478: trace(">>> CREATE a new Simple MBean => " + getNbObjects()
479: + " NbObjects in the MBeanServer");
480:
481: simpleNameVector.addElement(simpleName);
482:
483: // Wait a few seconds for the monitor to update.
484: //
485: try {
486: Thread.sleep(1000);
487: } catch (InterruptedException e) {
488: e.printStackTrace();
489: System.exit(1);
490: }
491: }
492: }
493:
494: private void depopulateTheAgent() {
495:
496: trace("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
497:
498: try {
499: trace("\n>>> PRESS <Enter> TO START THE DELETION OF 6 SIMPLE BEANS...");
500: System.in.read();
501: }
502: catch(Exception e) {
503: e.printStackTrace();
504: System.exit(1);
505: }
506:
507: // Delete 6 instances of Simple MBeans.
508: //
509:// int count = 0;
510: for (Enumeration enum = simpleNameVector.elements(); enum.hasMoreElements(); ) {
511: ObjectName simpleName = (ObjectName)enum.nextElement();
512: try {
513: server.unregisterMBean(simpleName);
514: } catch(Exception e) {
515: e.printStackTrace();
516: System.exit(1);
517: }
518: trace(">>> DELETE a Simple MBean => " + getNbObjects() + " NbObjects in the MBeanServer");
519: }
520: }
521:
522: private int getNbObjects() {
523:
524: try {
525: return server.queryMBeans(new ObjectName("*:*"), null)
526: .size();
527: } catch (Exception e) {
528: return (-1);
529: }
530: }
531:
532: private static void trace(String msg) {
533: System.out.println(msg);
534: }
535:
536: }
|