Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. Chapter 24 – Java Management Extensions (JMX) Outline 24.1Introduction 24.2Installation 24.3Case Study.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. Chapter 24 – Java Management Extensions (JMX) Outline 24.1Introduction 24.2Installation 24.3Case Study."— Presentation transcript:

1  2001 Prentice Hall, Inc. All rights reserved. Chapter 24 – Java Management Extensions (JMX) Outline 24.1Introduction 24.2Installation 24.3Case Study 24.3.1Instrument Resources 24.3.2Implement the JMX Management Agent 24.3.3Broadcasting and Receiving Notifications 24.3.4Management Application 24.3.5Compiling and Running the example 24.4Internet and World Wide Web Resources

2  2001 Prentice Hall, Inc. All rights reserved. 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  2001 Prentice Hall, Inc. All rights reserved. 24.1 Introduction JMX provides –Platform-independence –Protocol-independence –Reusable –Intelligent agent –Scalability

4  2001 Prentice Hall, Inc. All rights reserved. 24.1 Introduction Management Application Java Dynamic Management Agent Managed Resource Instrumentation Level Agent Level Manager Level

5  2001 Prentice Hall, Inc. All rights reserved. 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  2001 Prentice Hall, Inc. All rights reserved. 24.3 Case Study Management solution –Resource –Agent –Application MBean MBean server

7  2001 Prentice Hall, Inc. All rights reserved. 24.3 Case Study Printer MBean RmiConnectorClient: 5555 RmiConnectorServer: 5555 MBeanServer PrinterEventBroadcaster MBean PrinterSimulator Management Application

8  2001 Prentice Hall, Inc. All rights reserved. 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  2001 Prentice Hall, Inc. All rights reserved. 24.3.1 Instrument Resources MBean class design pattern –Implement MBean interface –Public and concrete –Public constructor

10  2001 Prentice Hall, Inc. All rights reserved. Outline Interface PrinterMBean 1. Declarations 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 public interface 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 public void setOnline ( Boolean online ); 33 Management interface Interface name with MBean suffix Return value is serializable is, get, set methods

11  2001 Prentice Hall, Inc. All rights reserved. Outline Interface PrinterEventListener 34 // fills up paper tray again with paper 35 public void replenishPaperTray(); 36 37 // cancel pending print jobs 38 public void cancelPendingPrintJobs(); 39 40 // start printing process 41 public void startPrinting(); 42 } 1 // Fig. 22.4: PrinterEventListener.java 2 // The listener interface for printer events. 3 4 // Deitel package 5 package com.deitel.advjhtp1.jmx.Printer; 6 7 public interface PrinterEventListener { 8 9 public void outOfPaper(); 10 11 public void lowToner(); 12 13 public void paperJam(); 14 } operations

12  2001 Prentice Hall, Inc. All rights reserved. Outline Class Printer 1. Declarations 2. Constructor 2.1 connect to printer device 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 public class 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 Connect to printer device MBean class implements responding MBean interface Public constructor

13  2001 Prentice Hall, Inc. All rights reserved. Outline 2.2 find MBean servers 2.3 specify MBean server requirement 2.4 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 } Find MBean servers Specify MBean server requirement Identify MBean server

14  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Implement management solution 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 public void stop() 82 { 83 printerSimulator.stop(); 84 } 85 86 // Is it printing? 87 public Boolean isPrinting() 88 { 89 return new 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 }

15  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Implement management solution 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 public void 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 public void replenishPaperTray() 139 { 140 printerSimulator.replenishPaperTray ( 141 Printer.PAPER_STACK_SIZE ); 142 } 143

16  2001 Prentice Hall, Inc. All rights reserved. Outline 4. Send out-of-paper notification 4.1 specify notification 4.2 send notification 144 // cancel pending print jobs 145 public void cancelPendingPrintJobs() 146 { 147 printerSimulator.cancelPendingPrintJobs(); 148 } 149 150 // start the printing process 151 public void startPrinting() 152 { 153 printerSimulator.startPrintingProcess(); 154 } 155 156 // send out of paper event to JMX layer 157 public void 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 Specify notificationSend notification

17  2001 Prentice Hall, Inc. All rights reserved. Outline 5. Send low-toner notification 5.1 specify notification 5.2 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 public void 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 Specify notificationSend notification

18  2001 Prentice Hall, Inc. All rights reserved. Outline 6. Send paper-jam notification 6.1 specify 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 public void 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 Specify notificationSend notification

19  2001 Prentice Hall, Inc. All rights reserved. Outline 6.2 send notification 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 public void outOfPaper() 257 { 258 // delegate call 259 fireOutOfPaperEvent(); 260 } 261 262 // interface implementation 263 public void lowToner() 264 { 265 // delegate call 266 fireLowTonerEvent(); 267 } 268 269 // interface implementation 270 public void paperJam() 271 { 272 // delegate call 273 firePaperJamEvent(); 274 } 275 } Interface implementation

20  2001 Prentice Hall, Inc. All rights reserved. Outline Class PrinterSimulator 1. Declarations 2. Constructor 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 public class PrinterSimulator implements Runnable { 11 12 private Stack printerStack = new Stack(); 13 private boolean isOnline = true; 14 private boolean isPrinting = false; 15 private boolean 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 Constructor takes PrinterEventListener as input

21  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 34 // stops execution of thread 35 public void 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 public void 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

22  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 68 // prints one job from print job stack 69 startPrintingProcess(); 70 71 } // end while 72 } 73 74 public void 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 } Simulate printing process

23  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 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 return new Boolean ( isOnline ); 122 } 123 124 // update amount of paper in paper tray 125 public synchronized void 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 return new Boolean( isPaperJam ); 139 } Add paper to paper tray

24  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 140 141 // cancel pending print jobs 142 public void cancelPendingPrintJobs() 143 { 144 synchronized ( printerStack ) { 145 printerStack.clear(); 146 } 147 } 148 149 // update amount of toner available in toner cartridge 150 public synchronized void 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 public synchronized void 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 public synchronized Integer getPaperTray() 172 { 173 return paperInTray; 174 } 175 Update toner in toner cartridge Issue paper jam event randomly

25  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 176 // returns amount of toner in toner cartridge 177 public synchronized Integer getToner() 178 { 179 return tonerCartridge; 180 } 181 182 // generates a random number of print jobs with varying IDs 183 public void 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 Generate print jobs

26  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 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 Get all jobs to be printed

27  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Simulate printer activities 244 245 // sets printer status to online 246 public void 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 public void setOffline() 258 { 259 isOnline = false; 260 } 261 262 // replenishes amount of paper in paper tray to specified 263 // value 264 public void replenishPaperTray ( int paperStack ) 265 { 266 updatePaperInTray( paperStack ) ; 267 } 268 269 // is printer printing? 270 public boolean isPrinting() 271 { 272 return isPrinting; 273 } 274 } When printer becomes online, notify all waiting states

28  2001 Prentice Hall, Inc. All rights reserved. 24.3.2 Implementation of the JMX Management Agent JMX agent –MBean server –MBeans (managed resource) –Protocol adaptor or connector

29  2001 Prentice Hall, Inc. All rights reserved. Outline Class PrinterManagementAgent 1. main 1.1 create MBeanServer 1.2 create RMI connector server 1.3 create broadcaster 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 public class PrinterManagementAgent { 12 13 public static void 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" Create MBean serverCreate RMI connector serverCreate broaddcaster MBean

30  2001 Prentice Hall, Inc. All rights reserved. Outline 1.4 create printer simulator MBean 1.5 handle exceptions 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 } Create printer simulator MBean

31  2001 Prentice Hall, Inc. All rights reserved. Outline 1.6 specify RMI connector server port 1.7 set RMI connector server port 1.8 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 Specify RMI connector server port Set RMI connector server port Start RMI connector server

32  2001 Prentice Hall, Inc. All rights reserved. Outline Interface PrinterEventBroadcasterMBean 1. Declarations 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 public interface PrinterEventBroadcasterMBean { 11 12 public void sendNotification( Notification notification ); 13 }

33  2001 Prentice Hall, Inc. All rights reserved. 24.3.3 Broadcasting and Receiving Notifications Broadcast notifications –Notification broadcaster MBean Receive notifications

34  2001 Prentice Hall, Inc. All rights reserved. Outline Class PrinterEventBroadcaster 1. Declarations 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 public class PrinterEventBroadcaster 15 extends NotificationBroadcasterSupport 16 implements PrinterEventBroadcasterMBean { 17 18 private static final String OUT_OF_PAPER = 19 "PrinterEvent.OUT_OF_PAPER"; 20 private static final String LOW_TONER = 21 "PrinterEvent.LOW_TONER"; 22 private static final String PAPER_JAM = 23 "PrinterEvent.PAPER_JAM"; 24

35  2001 Prentice Hall, Inc. All rights reserved. Outline 2. Implement getNotificationInfo to describe events 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 } Implement getNotificationInfo to describe events Event description Specify event types

36  2001 Prentice Hall, Inc. All rights reserved. Outline Class PrinterEventhandler 1. 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; 14 15 // Deitel packages 16 import com.deitel.advjhtp1.jmx.Printer.*; 17 18 public class 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 public void 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 } Inner class NotificationListener

37  2001 Prentice Hall, Inc. All rights reserved. Outline 2. Constructor 2.1 set notification mode 2.2 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 } Set notification modeRegister listener to RMI connector client

38  2001 Prentice Hall, Inc. All rights reserved. Outline 3. 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 private void handleOutOfPaperEvent() 90 { 91 eventTarget.outOfPaper(); 92 } 93 94 // delegate low toner event 95 private void handleLowTonerEvent() 96 { 97 eventTarget.lowToner(); 98 } 99 100 // delegate paper jam event 101 private void handlePaperJamEvent() 102 { 103 eventTarget.paperJam(); 104 } 105 } Delegate printer events

39  2001 Prentice Hall, Inc. All rights reserved. 24.3.4 Management Application Management application for the case study –GUI for managing the printer ClientPrinterManagement ManagerSidePrinterEventListener PrinterManagementGUI

40  2001 Prentice Hall, Inc. All rights reserved. Outline Class ClientPrinterManagement 1. Constructor 1.1 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 public class 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 Get RMI connector client

41  2001 Prentice Hall, Inc. All rights reserved. Outline 1.2 set RMI client port 1.3 establish connection 2. Get RmiConnectorClient reference 3. main 3.1 invoke constructor 3.2 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 public static void 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 } Set RMI client port Establish connection Invoke GUI

42  2001 Prentice Hall, Inc. All rights reserved. Outline Class PrinterManagementGUI 1. Import packages 2. 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; 21 22 // Deitel packages 23 import com.deitel.advjhtp1.jmx.Printer.*; 24 25 public class 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 Inner class TextAppender

43  2001 Prentice Hall, Inc. All rights reserved. Outline 3. Constructor 3.1 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 ) ); Define status panel

44  2001 Prentice Hall, Inc. All rights reserved. Outline 3.2 define buttons panel 3.2.1 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 public void 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 public void actionPerformed(ActionEvent event) { 105 addPaperButtonAction( event ); 106 } 107 } 108 ); 109 Define buttons panel Define actions for the buttons

45  2001 Prentice Hall, Inc. All rights reserved. Outline 3.3 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 public void 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 ); Define actions for the buttons Define events panel

46  2001 Prentice Hall, Inc. All rights reserved. Outline 3.4 start printer simulator 3.5 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 ); Start printing via MBean Get PrinterEventHandler

47  2001 Prentice Hall, Inc. All rights reserved. Outline 3.6 define actions when close window 184 185 // unregister MBean when close the window 186 addWindowListener( 187 new WindowAdapter() { 188 public void 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 Unregister Mbeans when close window

48  2001 Prentice Hall, Inc. All rights reserved. Outline 4 display events in events panel 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 public void outOfPaper() 232 { 233 SwingUtilities.invokeLater( 234 new TextAppender( printerEventTextArea, 235 "\nEVENT: Out of Paper!\n" ) ); 236 } 237 238 // toner low events 239 public void lowToner() 240 { 241 SwingUtilities.invokeLater( 242 new TextAppender( printerEventTextArea, 243 "\nEVENT: Toner Low!\n" ) ); 244 } 245 246 // paper jam events 247 public void paperJam() 248 { 249 SwingUtilities.invokeLater( 250 new TextAppender( printerEventTextArea, 251 "\nEVENT: Paper Jam!\n" ) ); 252 } Display events

49  2001 Prentice Hall, Inc. All rights reserved. Outline 5. Add paper to printer 6. Cancel pending print jobs 253 254 // add paper to the paper tray 255 public void 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 public void cancelPendingPrintJobsButtonAction( 282 ActionEvent event ) 283 { 284 try { 285 client.invoke( objectName, "cancelPendingPrintJobs", 286 new Object[ 0 ], new String[ 0 ] ); 287 } Add paper to paper try when click Add Paper button Cancel pending print jobs when click Cancel Pending Print Jobs button

50  2001 Prentice Hall, Inc. All rights reserved. Outline 7. Check printer’s status 7.1 get printer’s status 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 public void 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 Check printer’s status when click Check Status button

51  2001 Prentice Hall, Inc. All rights reserved. Outline 7.2 prepare output 322 // check if the printer is paper jammed 323 paperJamResponse = client.invoke( objectName, 324 "isPaperJam", new Object[ 0 ], new String[ 0 ] ); 325 326 // check if the printing is pringint 327 printingResponse = client.invoke( objectName, 328 "isPrinting", new Object[ 0 ], new String[ 0 ] ); 329 330 // get the paper tray 331 paperTrayResponse = client.invoke( objectName, 332 "getPaperTray", new Object[ 0 ], new String[ 0 ] ); 333 334 // get pending print jobs 335 pendingPrintJobsResponse = client.invoke( objectName, 336 "getPendingPrintJobs", new Object[ 0 ], 337 new String[ 0 ] ); 338 } 339 340 // if cannot invoke the method 341 catch ( ReflectionException exception ) { 342 exception.printStackTrace(); 343 } 344 345 // if invoked method throws exception 346 catch ( MBeanException exception ) { 347 exception.printStackTrace(); 348 } 349 350 // if MBean is not registered with MBean server 351 catch ( InstanceNotFoundException exception ) { 352 exception.printStackTrace(); 353 } 354 355 // status for the online condition 356 boolean isOnline = 357 ( ( Boolean ) onlineResponse ).booleanValue(); Check printer’s status by calling methods on Printer MBean

52  2001 Prentice Hall, Inc. All rights reserved. Outline 7.2 prepare output 358 359 // display status 360 if ( isOnline ) { 361 SwingUtilities.invokeLater( new TextAppender( 362 printerStatusTextArea, 363 "\nPrinter is ONLINE.\n" ) ); 364 } 365 else { 366 SwingUtilities.invokeLater( new TextAppender( 367 printerStatusTextArea, 368 "\nPrinter is OFFLINE.\n" ) ); 369 } 370 371 // status for the paper jam condition 372 boolean isPaperJam = 373 ( ( Boolean ) paperJamResponse ).booleanValue(); 374 375 // display status 376 if ( isPaperJam ) { 377 SwingUtilities.invokeLater( new TextAppender( 378 printerStatusTextArea, 379 "Paper jammed.\n" ) ); 380 } 381 else { 382 SwingUtilities.invokeLater( new TextAppender( 383 printerStatusTextArea, 384 "No Paper Jam.\n" ) ); 385 } 386 387 // status for the printing condition 388 boolean isPrinting = 389 ( ( Boolean )printingResponse ).booleanValue(); 390 Append printer status to output area

53  2001 Prentice Hall, Inc. All rights reserved. Outline 7.2 prepare output 391 // display status 392 if ( isPrinting ) { 393 SwingUtilities.invokeLater( new TextAppender( 394 printerStatusTextArea, 395 "Printer is currently printing.\n" ) ); 396 } 397 else { 398 SwingUtilities.invokeLater( new TextAppender( 399 printerStatusTextArea, 400 "Printer is not printing.\n" ) ); 401 } 402 403 // status for paper tray condition 404 int paperRemaining = 405 ( ( Integer )paperTrayResponse ).intValue(); 406 407 // display status 408 SwingUtilities.invokeLater( new TextAppender( 409 printerStatusTextArea, 410 "Printer paper tray has " + paperRemaining + 411 " pages remaining.\n" ) ); 412 413 // status for pending print jobs 414 Object[] pendingPrintJobs = 415 ( Object[] ) pendingPrintJobsResponse; 416 int pendingPrintJobsNumber = pendingPrintJobs.length; 417 418 // display status 419 SwingUtilities.invokeLater( new TextAppender( 420 printerStatusTextArea, 421 "Number of pending print jobs: " + 422 pendingPrintJobsNumber + "\n" ) ); 423 424 425 } // end method checkStatusButtonAction 426 } Append printer status to output area

54  2001 Prentice Hall, Inc. All rights reserved. 22.3.5 Compiling and Running the example CLASSPATH –jdmkrt.jar and jdmktk.jar Compile –Files in package com.deitel.advjhtp1.jmx.PrinterManagement –Files in package com.deitel.advjhtp1.jmx.PrinterSimulator Run –PrinterManagementAgent –ClientPrinterManagement

55  2001 Prentice Hall, Inc. All rights reserved. Outline Program output Initial window

56  2001 Prentice Hall, Inc. All rights reserved. Outline Program output Out-of-paper event happened Status after out-of-paper event happened

57  2001 Prentice Hall, Inc. All rights reserved. Outline Program output Status after clicked Add Paper button

58  2001 Prentice Hall, Inc. All rights reserved. Outline Program output Status after out-of-paper event happened Out-of-paper event happened

59  2001 Prentice Hall, Inc. All rights reserved. Outline Program output Status after clicked Cancel Pending Print Jobs button


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. Chapter 24 – Java Management Extensions (JMX) Outline 24.1Introduction 24.2Installation 24.3Case Study."

Similar presentations


Ads by Google