Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
F I N
Plus de contenu associé
Transcription de la présentation
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
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
24.3 Case Study • Management solution • Resource • Agent • Application • MBean • MBean server
Management Application RmiConnectorClient: 5555 RmiConnectorServer: 5555 MBeanServer Printer MBean PrinterEventBroadcaster MBean PrinterSimulator 24.3 Case Study
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
24.3.1 Instrument Resources • MBean class design pattern • Implement MBean interface • Public and concrete • Public constructor
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
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 }
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
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
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
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
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
24.3.4 Management Application • Management application for the case study • GUI for managing the printer • ClientPrinterManagement • ManagerSidePrinterEventListener • PrinterManagementGUI
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
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
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
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
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
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
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
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
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
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
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
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.