1 / 59

Chapter 24 – Java Management Extensions (JMX)

Chapter 24 – Java Management Extensions (JMX).

adlai
Télécharger la présentation

Chapter 24 – Java Management Extensions (JMX)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 24 – Java Management Extensions (JMX) Outline24.1 Introduction24.2 Installation24.3 Case Study 24.3.1 Instrument Resources 24.3.2 Implement the JMX Management Agent 24.3.3 Broadcasting and Receiving Notifications 24.3.4 Management Application 24.3.5 Compiling and Running the example24.4 Internet and World Wide Web Resources

  2. 24.1 Introduction • Network management problems • Current network management solutions • agents • Recent technological advances • JMX • Three-level management architecture • Instrumentation level • Agent level • Management level

  3. 24.1 Introduction • JMX provides • Platform-independence • Protocol-independence • Reusable • Intelligent agent • Scalability

  4. Manager Level Management Application Agent Level Java Dynamic Management Agent Instrumentation Level Managed Resource Managed Resource Managed Resource 24.1 Introduction

  5. 24.2 Installation • Implementation of JMX • Java Dynamic Management Kit (JDMK) • Download JDMK • www.sun.com/software/java-dynamic/try.html • Set CLASSPATH • jdmkrt.jar and jdmktk.jar

  6. 24.3 Case Study • Management solution • Resource • Agent • Application • MBean • MBean server

  7. Management Application RmiConnectorClient: 5555 RmiConnectorServer: 5555 MBeanServer Printer MBean PrinterEventBroadcaster MBean PrinterSimulator 24.3 Case Study

  8. 24.3.1 Instrument Resources • Instrument a resource • Standard MBean • MBean interface • MBean class • MBean interface design pattern • Naming • Expose attributes of a management interface • Methods • Operations • Serializable

  9. 24.3.1 Instrument Resources • MBean class design pattern • Implement MBean interface • Public and concrete • Public constructor

  10. Interface name with MBean suffix Management interface Return value is serializable is, get, set methods 1 // Fig. 22.3: PrinterMBean.java 2 // This class specifies the interface that will be implemented 3 // by Printer, which will function as an MBean. 4 5 // deitel package 6 package com.deitel.advjhtp1.jmx.PrinterSimulator; 7 8 publicinterface PrinterMBean { 9 10 // is it printing? 11 public Boolean isPrinting(); 12 13 // is it online? 14 public Boolean isOnline(); 15 16 // is paper jammed? 17 public Boolean isPaperJam(); 18 19 // returns paper amount in tray 20 public Integer getPaperTray(); 21 22 // returns ink level in toner cartridge 23 public Integer getToner(); 24 25 // returns ID of print job that is currently printing 26 public String getCurrentPrintJob(); 27 28 // returns array of all queued up print jobs 29 public String [] getPendingPrintJobs(); 30 31 // sets availability status of printer 32 publicvoid setOnline ( Boolean online ); 33 InterfacePrinterMBean1. Declarations

  11. operations 34 // fills up paper tray again with paper 35 publicvoid replenishPaperTray(); 36 37 // cancel pending print jobs 38 publicvoid cancelPendingPrintJobs(); 39 40 // start printing process 41 publicvoid startPrinting(); 42 } InterfacePrinterEventListener 1 // Fig. 22.4: PrinterEventListener.java2 // The listener interface for printer events.3 4 // Deitel package5 package com.deitel.advjhtp1.jmx.Printer;6 7 publicinterface PrinterEventListener {8 9 publicvoid outOfPaper();10 11 publicvoid lowToner();12 13 publicvoid paperJam();14 }

  12. MBean class implements responding MBean interface Connect to printer device Public constructor 1 // Fig. 22.5: Printer.java 2 // This class provides implementation for PrinterMBean 3 // interface and registers a managing MBean for the Printer 4 // device, which is simulated by PrinterSimulator.java. 5 6 // deitel package 7 package com.deitel.advjhtp1.jmx.PrinterSimulator; 8 9 // Java core package 10 import java.lang.Thread; 11 import java.util.ArrayList; 12 13 // JMX core packages 14 import javax.management.*; 15 16 // Deitel packages 17 import com.deitel.advjhtp1.jmx.Printer.*; 18 19 publicclass Printer implements PrinterMBean, 20 PrinterEventListener { 21 22 private PrinterSimulator printerSimulator; 23 private static final int PAPER_STACK_SIZE = 50; 24 private ObjectInstance eventBroadcasterInstance; 25 private ObjectName eventBroadcasterName; 26 private ObjectName printerName; 27 private MBeanServer mBeanServer; 28 29 public Printer() 30 { 31 // connect to the printer device 32 printerSimulator = new PrinterSimulator( this ) ; 33 Thread myThread = new Thread( printerSimulator ) ; 34 myThread.start(); 35 ClassPrinter1. Declarations2. Constructor2.1 connect to printer device

  13. Find MBean servers Specify MBean server requirement Identify MBean server 36 // find all MBean servers in current JVM 37 ArrayList arrayList = 38 MBeanServerFactory.findMBeanServer( null ); 39 40 // retrieve the MBeanServer reference 41 if ( arrayList.size() == 0) 42 System.out.println( "Cannot find a MBeanServer!" ); 43 44 else { 45 46 // get the MBeanServer that has the 47 // PrinterEventBroadcaster MBean registered with it 48 for ( int i = 0; i < arrayList.size(); i++ ) { 49 MBeanServer foundMBeanServer = 50 ( MBeanServer )arrayList.get( i ); 51 52 // obtain the object name for the 53 // PrinterEventBroadcaster MBean 54 try { 55 String name = foundMBeanServer.getDefaultDomain() 56 + ":type=" + "PrinterEventBroadcaster"; 57 eventBroadcasterName = new ObjectName( name ); 58 } 59 60 // handle exception when creating ObjectName 61 catch ( MalformedObjectNameException exception ) { 62 exception.printStackTrace(); 63 } 64 65 // check whether the PrinterEventBroadcaster MBean is 66 // registered with this MBeanServer 67 if ( foundMBeanServer.isRegistered( 68 eventBroadcasterName ) ) { 69 mBeanServer = foundMBeanServer; 70 break; 71 } 2.2 find MBean servers2.3 specify MBean server requirement2.4 identify MBean server

  14. 72 73 } // end for loop 74 75 } // end if-else to get the MBeanServer reference 76 77 } // end PrinterSimulator constructor 78 79 // will stop the printer thread from executing 80 // once execution should stop. 81 publicvoid stop() 82 { 83 printerSimulator.stop(); 84 } 85 86 // Is it printing? 87 public Boolean isPrinting() 88 { 89 returnnew Boolean( printerSimulator.isPrinting() ); 90 } 91 92 // is online? 93 public Boolean isOnline() 94 { 95 return printerSimulator.isOnline(); 96 } 97 98 // is paper jammed? 99 public Boolean isPaperJam() 100 { 101 return printerSimulator.isPaperJam(); 102 } 103 104 // is paper tray empty? 105 public Integer getPaperTray() 106 { 107 return printerSimulator.getPaperTray(); 108 } 3. Implement management solution

  15. 109 110 // is toner low? 111 public Integer getToner() 112 { 113 return printerSimulator.getToner(); 114 } 115 116 // returns ID of print job that is currently printing 117 public String getCurrentPrintJob() 118 { 119 return printerSimulator.getCurrentPrintJob(); 120 } 121 122 // returns array of all queued up print jobs 123 public String[] getPendingPrintJobs() 124 { 125 return printerSimulator.getPendingPrintJobs(); 126 } 127 128 // sets status availability of printer 129 publicvoid setOnline( Boolean online ) 130 { 131 if ( online.booleanValue() == true ) 132 printerSimulator.setOnline(); 133 else 134 printerSimulator.setOffline(); 135 } 136 137 // fills up the paper tray again with paper. 138 publicvoid replenishPaperTray() 139 { 140 printerSimulator.replenishPaperTray ( 141 Printer.PAPER_STACK_SIZE ); 142 } 143 3. Implement management solution

  16. Specify notification Send notification 144 // cancel pending print jobs 145 publicvoid cancelPendingPrintJobs() 146 { 147 printerSimulator.cancelPendingPrintJobs(); 148 } 149 150 // start the printing process 151 publicvoid startPrinting() 152 { 153 printerSimulator.startPrintingProcess(); 154 } 155 156 // send out of paper event to JMX layer 157 publicvoid fireOutOfPaperEvent() 158 { 159 // construct parameters and signatures 160 Object[] parameter = new Object[ 1 ]; 161 parameter[ 0 ] = new Notification( 162 "PrinterEvent.OUT_OF_PAPER", this, 0L ); 163 String[] signature = new String[ 1 ]; 164 signature[ 0 ] = "javax.management.Notification"; 165 166 // invoke notification 167 try { 168 mBeanServer.invoke( eventBroadcasterName, 169 "sendNotification", parameter, signature ); 170 } 171 172 // handle exception when invoking method 173 catch ( ReflectionException exception ) { 174 exception.printStackTrace(); 175 } 176 4. Send out-of-paper notification4.1 specify notification4.2 send notification

  17. Specify notification Send notification 177 // handle exception when communicating with MBean 178 catch ( MBeanException exception ) { 179 exception.printStackTrace(); 180 } 181 182 // handle exception if MBean not found 183 catch ( InstanceNotFoundException exception ) { 184 exception.printStackTrace(); 185 } 186 187 } // end method outOfPaperEvent 188 189 // send low toner event to JMX layer 190 publicvoid fireLowTonerEvent() 191 { 192 // construct parameters and signatures 193 Object[] parameter = new Object[ 1 ]; 194 parameter[ 0 ] = new Notification( 195 "PrinterEvent.LOW_TONER", this, 0L ); 196 String[] signature = new String[ 1 ]; 197 signature[ 0 ] = "javax.management.Notification"; 198 199 // invoke notification 200 try { 201 mBeanServer.invoke( eventBroadcasterName, 202 "sendNotification", parameter, signature ); 203 } 204 205 // handle exception when invoking method 206 catch ( ReflectionException exception ) { 207 exception.printStackTrace(); 208 } 209 5. Send low-toner notification5.1 specify notification5.2 send notification

  18. Specify notification Send notification 210 // handle exception communicating with MBean 211 catch ( MBeanException exception ) { 212 exception.printStackTrace(); 213 } 214 215 // handle exception if MBean not found 216 catch ( InstanceNotFoundException exception ) { 217 exception.printStackTrace(); 218 } 219 220 } // end method lowTonerEvent 221 222 // send paper jam event to JMX layer 223 publicvoid firePaperJamEvent() 224 { 225 // construct parameters and signatures 226 Object[] parameter = new Object[ 1 ]; 227 parameter[ 0 ] = new Notification( 228 "PrinterEvent.PAPER_JAM", this, 0L ); 229 String[] signature = new String[ 1 ]; 230 signature[ 0 ] = "javax.management.Notification"; 231 232 // invoke notification 233 try { 234 mBeanServer.invoke( eventBroadcasterName, 235 "sendNotification", parameter, signature ); 236 } 237 238 // handle exception when invoking method 239 catch( ReflectionException exception ) { 240 exception.printStackTrace(); 241 } 242 6. Send paper-jam notification6.1 specify notification

  19. Interface implementation 243 // handle exception communicating with MBean 244 catch( MBeanException exception ) { 245 exception.printStackTrace(); 246 } 247 248 // handle exception if MBean not found 249 catch( InstanceNotFoundException exception ) { 250 exception.printStackTrace(); 251 } 252 253 } // end method paperJamEvent 254 255 // interface implementation 256 publicvoid outOfPaper() 257 { 258 // delegate call 259 fireOutOfPaperEvent(); 260 } 261 262 // interface implementation 263 publicvoid lowToner() 264 { 265 // delegate call 266 fireLowTonerEvent(); 267 } 268 269 // interface implementation 270 publicvoid paperJam() 271 { 272 // delegate call 273 firePaperJamEvent(); 274 } 275 } 6.2 send notification

  20. Constructor takes PrinterEventListener as input 1 // Fig. 22.6: PrinterSimulator.java 2 // This class simulates a printer device on a network. 3 4 // Deitel package 5 package com.deitel.advjhtp1.jmx.Printer; 6 7 // java core package 8 import java.util.Stack; 9 10 publicclass PrinterSimulator implements Runnable { 11 12 private Stack printerStack = new Stack(); 13 privateboolean isOnline = true; 14 privateboolean isPrinting = false; 15 privateboolean isPaperJam = false; 16 17 // 50 sheets of paper in tray 18 private Integer paperInTray = new Integer( 50 ); 19 20 // 100% full of ink 21 private Integer tonerCartridge = new Integer( 100 ); 22 23 private String currentPrintJob; 24 private boolean isAlive = true; 25 private PrinterEventListener eventListener; 26 27 // default public constructor 28 public PrinterSimulator( 29 PrinterEventListener listener ) 30 { 31 eventListener = listener; 32 } 33 ClassPrinterSimulator1. Declarations2. Constructor

  21. 34 // stops execution of thread 35 publicvoid stop() 36 { 37 isAlive = false; 38 } 39 40 // main life-cycle of the printer. 41 // prints one job from print job stack 42 // 1) if offline, it pauses and waits. 43 // 2) if online, handles one print job 44 publicvoid run() 45 { 46 // main loop within thread 47 while ( isAlive ) { 48 49 // pause if offline 50 if ( !isOnline ) { 51 synchronized ( this ) { 52 53 // waits for printer become online 54 try { 55 wait(); 56 } 57 58 // if interrupt occurs 59 catch ( InterruptedException exception ) { 60 exception.printStackTrace(); 61 System.exit( -1 ); 62 } 63 64 } // end synchronized 65 66 } // end if 67 3. Simulate printer activities

  22. Simulate printing process 68 // prints one job from print job stack 69 startPrintingProcess(); 70 71 } // end while 72 } 73 74 publicvoid startPrintingProcess() 75 { 76 // warm up the printer, print top print job from print 77 // stack and adjust paper values and toner values 78 try { 79 // warm up printer for incoming batch of print jobs 80 Thread.sleep( 1000 * 5 ); 81 82 if ( ( paperInTray.intValue() > 0 ) && 83 ( tonerCartridge.intValue() > 10 ) && 84 ( !isPaperJam ) ) { 85 86 // start the printing process 87 currentPrintJob = getNextPrintJob(); 88 isPrinting = true; 89 90 // 12 seconds to print a normal document 91 Thread.sleep( 1000 * 12 ); 92 93 // each print job uses 10 pages 94 updatePaperInTray( paperInTray.intValue() - 10 ); 95 updateToner(); 96 updatePaperJam(); 97 isPrinting = false; 98 99 // make sure no references are left dangling 100 currentPrintJob = null; 101 } 102 } 3. Simulate printer activities

  23. Add paper to paper tray 103 104 // if interrupt occurs 105 catch ( InterruptedException exception ) { 106 exception.printStackTrace(); 107 System.exit( -1 ); 108 } 109 110 } // end method startPrintingProcess 111 112 // returns current printed job 113 public String getCurrentPrintJob() 114 { 115 return currentPrintJob; 116 } 117 118 // is printer online? 119 public Boolean isOnline() 120 { 121 returnnew Boolean ( isOnline ); 122 } 123 124 // update amount of paper in paper tray 125 publicsynchronizedvoid updatePaperInTray( int newValue ) 126 { 127 paperInTray = new Integer ( newValue ); 128 129 // fire event if paper tray low 130 if ( paperInTray.intValue() <= 0 ) { 131 eventListener.outOfPaperEvent(); 132 } 133 } 134 135 // is paper jammed? 136 public Boolean isPaperJam() 137 { 138 returnnew Boolean( isPaperJam ); 139 } 3. Simulate printer activities

  24. Update toner in toner cartridge Issue paper jam event randomly 140 141 // cancel pending print jobs 142 publicvoid cancelPendingPrintJobs() 143 { 144 synchronized ( printerStack ) { 145 printerStack.clear(); 146 } 147 } 148 149 // update amount of toner available in toner cartridge 150 publicsynchronizedvoid updateToner() 151 { 152 // after every print job, toner levels drop 1% 153 tonerCartridge = new Integer ( 154 tonerCartridge.intValue() - 1 ); 155 156 // fire event if toner is low 157 if ( tonerCartridge.intValue() <= 10 ) { 158 eventListener.lowTonerEvent(); 159 } 160 } 161 162 publicsynchronizedvoid updatePaperJam() 163 { 164 if ( Math.random() > 0.9 ) { 165 isPaperJam = true; 166 eventListener.paperJamEvent(); 167 } 168 } 169 170 // returns number of pages in paper tray 171 publicsynchronized Integer getPaperTray() 172 { 173 return paperInTray; 174 } 175 3. Simulate printer activities

  25. Generate print jobs 176 // returns amount of toner in toner cartridge 177 publicsynchronized Integer getToner() 178 { 179 return tonerCartridge; 180 } 181 182 // generates a random number of print jobs with varying IDs 183 publicvoid populatePrintStack() 184 { 185 int numOfJobs = ( int ) ( Math.random ( ) * 10 ) + 1; 186 187 // generate print jobs 188 synchronized ( printerStack ) { 189 for ( int i = 0 ; i < numOfJobs ; i++ ) { 190 printerStack.add ( "PRINT_JOB_ID #" + i ); 191 } 192 } 193 } 194 195 // returns next print job in stack, populating the stack 196 // if it is empty 197 public String getNextPrintJob() 198 { 199 if ( printerStack.isEmpty() ) { 200 populatePrintStack ( ); 201 202 // simulates absence of print jobs 203 try { 204 Thread.sleep ( 205 ( int ) ( Math.random() * 1000 * 10 ) ); 206 } 207 3. Simulate printer activities

  26. Get all jobs to be printed 208 // if interrupt occurs 209 catch ( InterruptedException exception ) { 210 exception.printStackTrace() ; 211 System.exit ( -1 ) ; 212 } 213 } 214 215 // Remove topmost queued resource. 216 String printJob; 217 218 synchronized ( printerStack ) { 219 printJob = ( String ) printerStack.pop(); 220 } 221 222 return printJob; 223 224 } // end method getNextPrintJob 225 226 // returns all jobs yet to be printed 227 public String[] getPendingPrintJobs() 228 { 229 String[] pendingPrintJobs; 230 231 // create array of pending print jobs 232 synchronized ( printerStack ) { 233 Object[] temp = printerStack.toArray() ; 234 pendingPrintJobs = new String[ temp.length ] ; 235 236 for ( int i = 0 ; i < pendingPrintJobs.length ; i++ ) { 237 pendingPrintJobs [ i ] = ( String )temp[ i ]; 238 } 239 } 240 241 return pendingPrintJobs; 242 243 } // end method getPendingPrintJobs 3. Simulate printer activities

  27. When printer becomes online, notify all waiting states 244 245 // sets printer status to online 246 publicvoid setOnline() 247 { 248 isOnline = true; 249 250 // notify all waiting states 251 synchronized ( this ) { 252 notifyAll() ; 253 } 254 } 255 256 // sets printer status to offline 257 publicvoid setOffline() 258 { 259 isOnline = false; 260 } 261 262 // replenishes amount of paper in paper tray to specified 263 // value 264 publicvoid replenishPaperTray ( int paperStack ) 265 { 266 updatePaperInTray( paperStack ) ; 267 } 268 269 // is printer printing? 270 publicboolean isPrinting() 271 { 272 return isPrinting; 273 } 274 } 3. Simulate printer activities

  28. 24.3.2 Implementation of the JMX Management Agent • JMX agent • MBean server • MBeans (managed resource) • Protocol adaptor or connector

  29. Create MBean server Create RMI connector server Create broaddcaster MBean 1 // Fig. 22.8: PrinterManagementAgent.java 2 // This application creates an MBeanServer and starts an RMI 3 // connector MBean service. 4 5 // deitel package 6 package com.deitel.advjhtp1.jmx.PrinterManagement; 7 8 // JMX core packages 9 import javax.management.*; 10 11 publicclass PrinterManagementAgent { 12 13 publicstaticvoid main( String[] args ) 14 { 15 ObjectInstance rmiConnectorServer = null; 16 ObjectInstance printer = null; 17 ObjectInstance broadcaster = null; 18 ObjectName objectName = null; 19 20 // create an MBeanServer 21 MBeanServer server = 22 MBeanServerFactory.createMBeanServer(); 23 24 // create an RMI connector service, a printer simulator 25 // MBean and a broadcaster MBean 26 try { 27 28 // create an RMI connector server 29 rmiConnectorServer = server.createMBean ( 30 "com.sun.jdmk.comm.RmiConnectorServer", null ); 31 32 // create a broadcaster MBean 33 String name = server.getDefaultDomain() 34 + ":type=" + "PrinterEventBroadcaster"; 35 String className = "com.deitel.advjhtp1.jmx." 36 + "PrinterSimulator.PrinterEventBroadcaster" ClassPrinterManagementAgent1. main1.1 create MBeanServer1.2 create RMI connector server1.3 create broadcaster MBean

  30. Create printer simulator MBean 37 38 objectName = new ObjectName( name ); 39 printer = server.createMBean( 40 className, objectName ); 41 42 // create a printer simulator MBean 43 name = server.getDefaultDomain() 44 + ":type=" + "Printer"; 45 className = "com.deitel.advjhtp1.jmx." 46 + "PrinterSimulator.Printer"; 47 48 objectName = new ObjectName( name ); 49 broadcaster = server.createMBean( 50 className, objectName ); 51 52 } // end try 53 54 // handle class not JMX-compliant MBean exception 55 catch ( NotCompliantMBeanException exception ) { 56 exception.printStackTrace(); 57 } 58 59 // handle MBean constructor exception 60 catch ( MBeanException exception ) { 61 exception.printStackTrace(); 62 } 63 64 // handle MBean already exists exception 65 catch ( InstanceAlreadyExistsException exception ) { 66 exception.printStackTrace(); 67 } 68 69 // handle MBean constructor exception 70 catch ( ReflectionException exception ) { 71 exception.printStackTrace(); 72 } 1.4 create printer simulator MBean1.5 handle exceptions

  31. Specify RMI connector server port Set RMI connector server port Start RMI connector server 73 74 // handle invalid object name exception 75 catch ( MalformedObjectNameException exception) { 76 exception.printStackTrace(); 77 } 78 79 // set port number 80 Object[] parameter = new Object[ 1 ]; 81 parameter[ 0 ] = new Integer( 5555 ); 82 String[] signature = new String[ 1 ]; 83 signature[ 0 ] = "int"; 84 85 // invoke method setPort on RmiConnectorServer MBean 86 // start the RMI connector service 87 try { 88 server.invoke( 89 rmiConnectorServer.getObjectName(), "setPort", 90 parameter, signature ); 91 server.invoke( 92 rmiConnectorServer.getObjectName(), "start" , 93 new Object[ 0 ], new String[ 0 ] ); 94 } 95 96 // handle exception when executing method 97 catch ( ReflectionException exception ) { 98 exception.printStackTrace(); 99 } 100 101 // handle exception communicating with MBean 102 catch ( MBeanException exception ) { 103 exception.printStackTrace(); 104 } 105 1.6 specify RMI connector server port1.7 set RMI connector server port1.8 start RMI connector server

  32. 106 // handle exception if MBean not found 107 catch ( InstanceNotFoundException exception ) { 108 exception.printStackTrace(); 109 } 110 111 } // end method main 112 } ____________________________________________________________________ 1 // Fig. 22.9: PrinterEventBroadcasterMBean.java 2 // This class defines the MBean interface. 3 4 // deitel package 5 package com.deitel.advjhtp1.jmx.PrinterSimulator; 6 7 // JMX core packages 8 import javax.management.Notification; 9 10 publicinterface PrinterEventBroadcasterMBean { 11 12 publicvoid sendNotification( Notification notification ); 13 } InterfacePrinterEventBroadcasterMBean1. Declarations

  33. 24.3.3 Broadcasting and Receiving Notifications • Broadcast notifications • Notification broadcaster MBean • Receive notifications

  34. 1 // Fig. 22.10: PrinterEventBroadcaster.java 2 // This class defines an MBean that 3 // provides events information. 4 5 // deitel package 6 package com.deitel.advjhtp1.jmx.PrinterSimulator; 7 8 // JMX core packages 9 import javax.management.MBeanNotificationInfo; 10 import javax.management.NotificationBroadcasterSupport; 11 12 // extends NotificationBroadcasterSupport to adopt its 13 // functionality. 14 publicclass PrinterEventBroadcaster 15 extends NotificationBroadcasterSupport 16 implements PrinterEventBroadcasterMBean { 17 18 privatestaticfinal String OUT_OF_PAPER = 19 "PrinterEvent.OUT_OF_PAPER"; 20 privatestaticfinal String LOW_TONER = 21 "PrinterEvent.LOW_TONER"; 22 privatestaticfinal String PAPER_JAM = 23 "PrinterEvent.PAPER_JAM"; 24 ClassPrinterEventBroadcaster1. Declarations

  35. Implement getNotificationInfo to describe events Specify event types Event description 25 // provide information about deliverable events 26 public MBeanNotificationInfo[] getNotificationInfo() 27 { 28 // array containing descriptor objects 29 MBeanNotificationInfo[] descriptorArray = 30 new MBeanNotificationInfo[ 1 ]; 31 32 // different event types 33 String[] notificationTypes = new String[ 3 ]; 34 notificationTypes[ 0 ] = 35 PrinterEventBroadcaster.OUT_OF_PAPER; 36 notificationTypes[ 1 ] = 37 PrinterEventBroadcaster.LOW_TONER; 38 notificationTypes[ 2 ] = 39 PrinterEventBroadcaster.PAPER_JAM; 40 41 // notification class type 42 String classType = "javax.management.Notification"; 43 44 // description of MBeanNotificationInfo 45 String description = 46 "Notification types for PrinterEventBroadcaster"; 47 48 // populate descriptor array 49 descriptorArray[ 0 ] = new MBeanNotificationInfo( 50 notificationTypes, classType, description ); 51 52 return descriptorArray; 53 54 } // end method getNotificationInfo 55 } 2. Implement getNotificationInfo to describe events

  36. Inner class NotificationListener 1 // Fig. 22.11: PrinterEventHandler.java 2 // The class adds a listener to the broadcaster MBean and 3 // defines the event handlers when event occurs. 4 5 // deitel package 6 package com.deitel.advjhtp1.jmx.Client; 7 8 // JMX core packages 9 import javax.management.*; 10 11 // JDMK core packages 12 import com.sun.jdmk.comm.RmiConnectorClient; 13 import com.sun.jdmk.comm.ClientNotificationHandler; // Deitel packages importcom.deitel.advjhtp1.jmx.Printer.*; 18 publicclass PrinterEventHandler { 19 20 private RmiConnectorClient rmiClient; 21 private PrinterEventListener eventTarget; 22 23 // notification listener annonymous inner class 24 private NotificationListener notificationListener = 25 new NotificationListener() { 26 publicvoid handleNotification( 27 Notification notification, Object handback ) 28 { 29 // retrieve notification type 30 String notificationType = notification.getType(); 31 32 // handle different notifications 33 if ( notificationType.equals( 34 "PrinterEvent.OUT_OF_PAPER" ) ) { 35 handleOutOfPaperEvent(); 36 return; 37 } ClassPrinterEventhandler1. Inner class NotificationListener

  37. Set notification mode Register listener to RMI connector client 39 if ( notificationType.equals( 40 "PrinterEvent.LOW_TONER" ) ) { 41 handleLowTonerEvent(); 42 return; 43 } 44 45 if ( notificationType.equals( 46 "PrinterEvent.PAPER_JAM" ) ) { 47 handlePaperJamEvent(); 48 return; 49 } 50 51 } // end method handleNotification 52 53 }; // end annonymous inner class 54 55 // default constructor 56 public PrinterEventHandler( 57 RmiConnectorClient inputRmiClient, 58 ManagerSidePrinterEventListener inputEventTarget ) 59 { 60 rmiClient = inputRmiClient; 61 eventTarget = inputEventTarget; 62 63 // set notification push mode 64 rmiClient.setMode( ClientNotificationHandler.PUSH_MODE ); 65 66 // register listener 67 try { 68 ObjectName objectName = new ObjectName( 69 rmiClient.getDefaultDomain() 70 + ":type=" + "PrinterEventBroadcaster" ); 71 72 rmiClient.addNotificationListener( objectName, 73 notificationListener, null, null ); 74 } 2. Constructor2.1 set notification mode2.2 register listener to RMI connector client

  38. Delegate printer events 75 76 // if MBean does not exist in the MBean server 77 catch ( InstanceNotFoundException exception) { 78 exception.printStackTrace(); 79 } 80 81 // if the format of the object name is wrong 82 catch ( MalformedObjectNameException exception ) { 83 exception.printStackTrace(); 84 } 85 86 } // end PrinterEventHandler constructor 87 88 // delegate out of paper event 89 privatevoid handleOutOfPaperEvent() 90 { 91 eventTarget.outOfPaper(); 92 } 93 94 // delegate low toner event 95 privatevoid handleLowTonerEvent() 96 { 97 eventTarget.lowToner(); 98 } 99 100 // delegate paper jam event 101 privatevoid handlePaperJamEvent() 102 { 103 eventTarget.paperJam(); 104 } 105 } 3. Delegate printer events

  39. 24.3.4 Management Application • Management application for the case study • GUI for managing the printer • ClientPrinterManagement • ManagerSidePrinterEventListener • PrinterManagementGUI

  40. Get RMI connector client 1 // Fig. 22.12: ClientPrinterManagement.java 2 // This application establishes a connection to the MBeanServer 3 // and creates an MBean for PrinterSimulator. 4 5 // deitel package 6 package com.deitel.advjhtp1.jmx.Client; 7 8 // Java core packages 9 import java.awt.*; 10 import java.awt.event.*; 11 12 // JMX core packages 13 import javax.management.*; 14 15 // JDMX core packages 16 import com.sun.jdmk.comm.RmiConnectorClient; 17 import com.sun.jdmk.comm.RmiConnectorAddress; 18 19 publicclass ClientPrinterManagement { 20 21 private RmiConnectorClient rmiClient; 22 23 // instantiate client connection 24 public ClientPrinterManagement() 25 { 26 // create connector client instance 27 rmiClient = new RmiConnectorClient(); 28 29 // create address instance 30 RmiConnectorAddress rmiAddress = 31 new RmiConnectorAddress(); 32 ClassClientPrinterManagement1. Constructor1.1 get RMI connector client

  41. Set RMI client port Establish connection Invoke GUI 33 // specify port 34 rmiAddress.setPort( 5555 ); 35 36 // establish connection 37 rmiClient.connect( rmiAddress ); 38 39 } // end ClinetPrinterManagement constructor 40 41 // return RmiConnectorClient reference 42 public RmiConnectorClient getClient() 43 { 44 return rmiClient; 45 } 46 47 publicstaticvoid main( String[] args ) 48 { 49 // instantiate client connection 50 ClientPrinterManagement clientManager = 51 new ClientPrinterManagement(); 52 53 // get RMIConnectorClient handle 54 RmiConnectorClient client = clientManager.getClient(); 55 56 // start GUI 57 PrinterManagementGUI printerManagementGUI = 58 new PrinterManagementGUI( client ); 59 60 // display the output 61 printerManagementGUI.setSize( 62 new Dimension( 500, 500 ) ); 63 printerManagementGUI.setVisible( true ); 64 65 } // end method main 66 } 1.2 set RMI client port1.3 establish connection2. Get RmiConnectorClient reference3. main3.1 invoke constructor3.2 invoke GUI

  42. Inner class TextAppender 1 // Fig. 22.14: PrinterManagementGUI.java 2 // This class defines the GUI for the 3 // printer management application. 4 5 // deitel package 6 package com.deitel.advjhtp1.jmx.Client; 7 8 // Java AWT core package 9 import java.awt.*; 10 import java.awt.event.*; 11 12 // Java standard extensions 13 import javax.swing.*; 14 15 // JMX core packages 16 import javax.management.*; 17 18 // JDMX core packages 19 import com.sun.jdmk.comm.RmiConnectorClient; 20 import com.sun.jdmk.comm.RmiConnectorAddress; // Deitel packages import com.deitel.advjhtp1.jmx.Printer.*; 25 publicclass PrinterManagementGUI extends JFrame 26 implements PrinterEventListener { 27 28 // TextAppender appends text to a JTextArea. This Runnable 29 // object should be executed only using SwingUtilities 30 // methods invokeLater or invokeAndWait as it modifies 31 // a live Swing component. 32 private class TextAppender implements Runnable { 33 34 private String text; 35 private JTextArea textArea; 36 ClassPrinterManagementGUI1. Import packages2. Inner class TextAppender

  43. Define status panel 37 // TextAppender constructor 38 public TextAppender( JTextArea area, String newText ) 39 { 40 text = newText; 41 textArea = area; 42 } 43 44 // display new text in JTextArea 45 public void run() 46 { 47 // append new message 48 textArea.append( text ); 49 50 // move caret to end of messageArea to ensure new 51 // message is visible on screen 52 textArea.setCaretPosition( 53 textArea.getText().length() ); 54 } 55 56 } // end TextAppender inner class 57 58 private ObjectName objectName; 59 private RmiConnectorClient client; 60 private JTextArea printerStatusTextArea = new JTextArea(); 61 private JTextArea printerEventTextArea = new JTextArea(); 62 63 public PrinterManagementGUI( RmiConnectorClient rmiClient ) 64 { 65 super( "JMX Printer Management Example" ); 66 67 Container container = getContentPane(); 68 69 // status panel 70 JPanel printerStatusPanel = new JPanel(); 71 printerStatusPanel.setPreferredSize( 72 new Dimension( 512, 200 ) ); 3. Constructor3.1 define status panel

  44. Define buttons panel Define actions for the buttons 73 JScrollPane statusScrollPane = new JScrollPane(); 74 statusScrollPane.setAutoscrolls( true ); 75 statusScrollPane.setPreferredSize( 76 new Dimension( 400, 150 ) ); 77 statusScrollPane.getViewport().add( 78 printerStatusTextArea, null ); 79 printerStatusPanel.add( statusScrollPane, null ); 80 81 // buttons panel 82 JPanel buttonPanel = new JPanel(); 83 buttonPanel.setPreferredSize( 84 new Dimension( 512, 200 ) ); 85 86 // define action for Check Status button 87 JButton checkStatusButton = 88 new JButton( "Check Status" ); 89 checkStatusButton.addActionListener( 90 91 new ActionListener() { 92 93 publicvoid actionPerformed( ActionEvent event ) { 94 checkStatusButtonAction( event ); 95 } 96 } 97 ); 98 99 // define action for Add Paper button 100 JButton addPaperButton = new JButton( "Add Paper" ); 101 addPaperButton.addActionListener( 102 new ActionListener() { 103 104 publicvoid actionPerformed(ActionEvent event) { 105 addPaperButtonAction( event ); 106 } 107 } 108 ); 109 3.2 define buttons panel3.2.1 define actions for the buttons

  45. Define actions for the buttons Define events panel 110 // define action for Cancel Pending Print Jobs button 111 JButton cancelPendingPrintJobsButton = new JButton( 112 "Cancel Pending Print Jobs" ); 113 cancelPendingPrintJobsButton.addActionListener( 114 new ActionListener() { 115 116 publicvoid actionPerformed( ActionEvent event ) { 117 cancelPendingPrintJobsButtonAction( event ); 118 } 119 } 120 ); 121 122 // add three buttons to the panel 123 buttonPanel.add( checkStatusButton, null ); 124 buttonPanel.add( addPaperButton, null ); 125 buttonPanel.add( cancelPendingPrintJobsButton, null ); 126 127 // events panel 128 JPanel printerEventPanel = new JPanel(); 129 printerEventPanel.setPreferredSize( 130 new Dimension( 512, 200) ); 131 JScrollPane eventsScrollPane = new JScrollPane(); 132 eventsScrollPane.setAutoscrolls( true ); 133 eventsScrollPane.setPreferredSize( 134 new Dimension( 400, 150 ) ); 135 eventsScrollPane.getViewport().add( 136 printerEventTextArea, null ); 137 printerEventPanel.add( eventsScrollPane, null ); 138 139 // initialize the text 140 printerStatusTextArea.setText( "Printer Status: ---\n" ); 141 printerEventTextArea.setText( "Events: --- \n" ); 142 143 // put all the panels together 144 container.add( printerStatusPanel, BorderLayout.NORTH ); 145 container.add( printerEventPanel, BorderLayout.SOUTH ); 146 container.add( buttonPanel, BorderLayout.CENTER ); 3.3 define events panel

  46. Start printing via MBean Get PrinterEventHandler 147 148 // set RmiConnectorClient reference 149 client = rmiClient; 150 151 // invoke method startPrinting of the 152 // PrinterSimulator MBean 153 try { 154 String name = client.getDefaultDomain() 155 + ":type=" + "Printer"; 156 objectName = new ObjectName( name ); 157 client.invoke( objectName, "startPrinting", 158 new Object[ 0 ], new String[ 0 ] ); 159 } 160 161 // invalid object name 162 catch ( MalformedObjectNameException exception ) { 163 exception.printStackTrace(); 164 } 165 166 // if cannot invoke the method 167 catch ( ReflectionException exception) { 168 exception.printStackTrace(); 169 } 170 171 // if invoked method throws exception 172 catch ( MBeanException exception ) { 173 exception.printStackTrace(); 174 } 175 176 // if MBean is not registered with MBean server 177 catch ( InstanceNotFoundException exception ) { 178 exception.printStackTrace(); 179 } 180 181 // instantiate PrinterEventNotifier 182 PrinterEventHandler printerEventHandler = 183 new PrinterEventHandler( client, this ); 3.4 start printer simulator3.5 get PrinterEventHandler

  47. Unregister Mbeans when close window 184 185 // unregister MBean when close the window 186 addWindowListener( 187 new WindowAdapter() { 188 publicvoid windowClosing( WindowEvent event ) 189 { 190 // unregister MBean 191 try { 192 193 // unregister the PrinterSimulator MBean 194 client.unregisterMBean( objectName ); 195 196 // unregister the PrinterEventBroadcaster 197 // MBean 198 String name = client.getDefaultDomain() 199 + ":type=" + "PrinterEventBroadcaster"; 200 objectName = new ObjectName( name ); 201 client.unregisterMBean( objectName ); 202 } 203 204 // if invalid object name 205 catch ( MalformedObjectNameException exception) { 206 exception.printStackTrace(); 207 } 208 209 // if exception is caught from method preDeregister 210 catch ( MBeanRegistrationException exception ) { 211 exception.printStackTrace(); 212 } 213 214 // if MBean is not registered with MBean server 215 catch ( InstanceNotFoundException exception ) { 216 exception.printStackTrace(); 217 } 218 3.6 define actions when close window

  48. Display events 219 // terminate the program 220 System.exit( 0 ); 221 222 } // end method windowClosing 223 224 } // end WindowAdapter constructor 225 226 ); // end addWindowListener 227 228 } // end PrinterManagementGUI constructor 229 230 // out of paper events 231 publicvoid outOfPaper() 232 { 233 SwingUtilities.invokeLater( 234 new TextAppender(printerEventTextArea, 235 "\nEVENT: Out of Paper!\n" ) ); 236 } 237 238 // toner low events 239 publicvoid lowToner() 240 { 241 SwingUtilities.invokeLater( 242 new TextAppender(printerEventTextArea, 243 "\nEVENT: Toner Low!\n" ) ); 244 } 245 246 // paper jam events 247 publicvoid paperJam() 248 { 249 SwingUtilities.invokeLater( 250 new TextAppender(printerEventTextArea, 251 "\nEVENT: Paper Jam!\n" ) ); 252 } 4 display events in events panel

  49. Add paper to paper try when click Add Paper button Cancel pending print jobs when click Cancel Pending Print Jobs button 253 254 // add paper to the paper tray 255 publicvoid addPaperButtonAction( ActionEvent event ) 256 { 257 try { 258 client.invoke( objectName, "replenishPaperTray", 259 new Object[ 0 ], new String[ 0 ] ); 260 } 261 262 // if cannot invoke the method 263 catch ( ReflectionException exception) 264 { 265 exception.printStackTrace(); 266 } 267 268 // if invoked method throws exception 269 catch ( MBeanException exception ) { 270 exception.printStackTrace(); 271 } 272 273 // if MBean is not registered with MBean server 274 catch ( InstanceNotFoundException exception ) { 275 exception.printStackTrace(); 276 } 277 278 } // end method addPaperButtonAction 279 280 // cancel pending print jobs 281 publicvoid cancelPendingPrintJobsButtonAction( 282 ActionEvent event ) 283 { 284 try { 285 client.invoke( objectName, "cancelPendingPrintJobs", 286 new Object[ 0 ], new String[ 0 ] ); 287 } 5. Add paper to printer6. Cancel pending print jobs

  50. Check printer’s status when click Check Status button 288 289 // if cannot invoke the method 290 catch ( ReflectionException exception) 291 { 292 exception.printStackTrace(); 293 } 294 295 // if invoked method throws exception 296 catch ( MBeanException exception ) { 297 exception.printStackTrace(); 298 } 299 300 // if MBean is not registered with MBean server 301 catch ( InstanceNotFoundException exception ) { 302 exception.printStackTrace(); 303 } 304 305 } // end method cancelPendingPrintJobsButtonAction 306 307 publicvoid checkStatusButtonAction( ActionEvent event ) 308 { 309 Object onlineResponse = null; 310 Object paperJamResponse = null; 311 Object printingResponse = null; 312 Object paperTrayResponse = null; 313 Object pendingPrintJobsResponse = null; 314 315 // manage printer remotely 316 try { 317 318 // check if the printer is on line 319 onlineResponse = client.invoke( objectName, 320 "isOnline", new Object[ 0 ], new String[ 0 ] ); 321 7. Check printer’s status7.1 get printer’s status

More Related