Chapter 24 Java Management Extensions JMX Outline 24

  • Slides: 59
Download presentation
Chapter 24 – Java Management Extensions (JMX) Outline 24. 1 Introduction 24. 2 Installation

Chapter 24 – Java Management Extensions (JMX) Outline 24. 1 Introduction 24. 2 Installation 24. 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 example 24. 4 Internet and World Wide Web Resources 2001 Prentice Hall, Inc. All rights reserved.

24. 1 Introduction • Network management problems • Current network management solutions – agents

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 2001 Prentice Hall, Inc. All rights reserved.

24. 1 Introduction • JMX provides – – – Platform-independence Protocol-independence Reusable Intelligent agent

24. 1 Introduction • JMX provides – – – Platform-independence Protocol-independence Reusable Intelligent agent Scalability 2001 Prentice Hall, Inc. All rights reserved.

24. 1 Introduction Manager Level Agent Level Instrumentation Level 2001 Prentice Hall, Inc. All

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

24. 2 Installation • Implementation of JMX – Java Dynamic Management Kit (JDMK) •

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 2001 Prentice Hall, Inc. All rights reserved.

24. 3 Case Study • Management solution – Resource – Agent – Application •

24. 3 Case Study • Management solution – Resource – Agent – Application • MBean server 2001 Prentice Hall, Inc. All rights reserved.

24. 3 Case Study Management Application Rmi. Connector. Client: 5555 Rmi. Connector. Server: 5555

24. 3 Case Study Management Application Rmi. Connector. Client: 5555 Rmi. Connector. Server: 5555 MBean. Server Printer MBean Printer. Simulator 2001 Prentice Hall, Inc. All rights reserved. Printer. Event. Broadcaster MBean

24. 3. 1 Instrument Resources • Instrument a resource – Standard MBean • MBean

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 2001 Prentice Hall, Inc. All rights reserved.

24. 3. 1 Instrument Resources • MBean class design pattern – Implement MBean interface

24. 3. 1 Instrument Resources • MBean class design pattern – Implement MBean interface – Public and concrete – Public constructor 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 // Fig. 22. 3: Printer. MBean. java 2 // This class specifies

Outline 1 // Fig. 22. 3: Printer. MBean. 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. advjhtp 1. jmx. Printer. Simulator; 7 8 public interface Printer. MBean { 9 10 // is it printing? Interface name 11 public Boolean is. Printing(); 12 with MBean suffix 13 // is it online? 14 public Boolean is. Online(); 15 16 // is paper jammed? Interface 17 public Boolean is. Paper. Jam(); Printer. MBean 18 Management interface 19 // returns paper amount in tray 1. Declarations 20 public Integer get. Paper. Tray(); 21 22 // returns ink level in toner cartridge 23 public Integer get. Toner(); Return value is serializable 24 25 // returns ID of print job that is currently printing 26 public String get. Current. Print. Job(); 27 28 // returns array of all queued up print jobs is, get, set 29 public String [] get. Pending. Print. Jobs(); methods 30 31 // sets availability status of printer 32 public void set. Online ( Boolean online ); 33 2001 Prentice Hall, Inc. All rights reserved.

Outline 34 // fills up paper tray again with paper 35 public void replenish.

Outline 34 // fills up paper tray again with paper 35 public void replenish. Paper. Tray(); 36 37 // cancel pending print jobs 38 public void cancel. Pending. Print. Jobs(); 39 40 // start printing process 41 public void start. Printing(); 42 } 1 // Fig. 22. 4: Printer. Event. Listener. java 2 // The listener interface for printer events. 3 4 // Deitel package 5 package com. deitel. advjhtp 1. jmx. Printer; 6 7 public interface Printer. Event. Listener { 8 9 public void out. Of. Paper(); 10 11 public void low. Toner(); 12 13 public void paper. Jam(); 14 } operations Interface Printer. Event. Listener 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 // Fig. 22. 5: Printer. java 2 // This class provides implementation

Outline 1 // Fig. 22. 5: Printer. java 2 // This class provides implementation for Printer. MBean 3 // interface and registers a managing MBean for the Printer 4 // device, which is simulated by Printer. Simulator. java. 5 6 // deitel package 7 package com. deitel. advjhtp 1. jmx. Printer. Simulator; 8 9 // Java core package 10 import java. lang. Thread; 11 import java. util. Array. List; 12 13 // JMX core packages 14 import javax. management. *; Class 15 Printer 16 // Deitel packages 17 import com. deitel. advjhtp 1. jmx. Printer. *; 1. Declarations 18 19 public class Printer implements Printer. MBean, 2. Constructor 20 Printer. Event. Listener { MBean class implements 21 responding MBean interface 2. 1 connect to 22 private Printer. Simulator printer. Simulator ; printer device 23 private static final int PAPER_STACK_SIZE = 50; 24 private Object. Instance event. Broadcaster. Instance ; 25 private Object. Name event. Broadcaster. Name ; 26 private Object. Name printer. Name; 27 private MBean. Server m. Bean. Server ; 28 Connect to 29 public Printer() Public constructor 30 { printer device 31 // connect to the printer device 32 printer. Simulator = new Printer. Simulator( this ) ; 33 Thread my. Thread = new Thread( printer. Simulator ) ; 34 my. Thread. start(); 2001 Prentice Hall, Inc. 35 All rights reserved.

36 // find all MBean servers in current JVM Find MBean 37 Array. List

36 // find all MBean servers in current JVM Find MBean 37 Array. List array. List = 38 MBean. Server. Factory. find. MBean. Server( null ); servers 39 40 // retrieve the MBean. Server reference 41 if ( array. List. size() == 0) 42 System. out. println( "Cannot find a MBean. Server!" ); 43 44 else { 45 46 // get the MBean. Server that has the 47 // Printer. Event. Broadcaster MBean registered with it 48 for ( int i = 0; i < array. List. size(); i++ ) { 49 MBean. Server found. MBean. Server = 50 ( MBean. Server )array. List. get( i ); 2. 2 find MBean servers 51 Specify MBean 52 // obtain the object name for the 2. 3 specify MBean 53 // Printer. Event. Broadcaster MBean server requirement 54 try { requirement 55 String name = found. MBean. Server. get. Default. Domain() 2. 4 identify MBean 56 + ": type=" + "Printer. Event. Broadcaster"; server 57 event. Broadcaster. Name = new Object. Name( name ); 58 } 59 60 // handle exception when creating Object. Name 61 catch ( Malformed. Object. Name. Exception exception ) { 62 exception. print. Stack. Trace(); 63 } 64 65 // check whether the Printer. Event. Broadcaster MBean is 66 // registered with this MBean. Server 67 if ( found. MBean. Server. is. Registered( Identify MBean server 68 event. Broadcaster. Name ) ) { 69 m. Bean. Server = found. MBean. Server; 2001 Prentice Hall, Inc. 70 break; All rights reserved. 71 } Outline

72 73 } // end for loop 74 75 } // end if-else to

72 73 } // end for loop 74 75 } // end if-else to get the MBean. Server reference 76 77 } // end Printer. Simulator constructor 78 79 // will stop the printer thread from executing 80 // once execution should stop. 81 public void stop() 82 { 83 printer. Simulator. stop(); 84 } 85 86 // Is it printing? 87 public Boolean is. Printing() 88 { 89 return new Boolean( printer. Simulator. is. Printing() ); 90 } 91 92 // is online? 93 public Boolean is. Online() 94 { 95 return printer. Simulator. is. Online(); 96 } 97 98 // is paper jammed? 99 public Boolean is. Paper. Jam() 100 { 101 return printer. Simulator. is. Paper. Jam(); 102 } 103 104 // is paper tray empty? 105 public Integer get. Paper. Tray() 106 { 107 return printer. Simulator. get. Paper. Tray(); 108 } Outline 3. Implement management solution 2001 Prentice Hall, Inc. All rights reserved.

109 110 // is toner low? 111 public Integer get. Toner() 112 { 113

109 110 // is toner low? 111 public Integer get. Toner() 112 { 113 return printer. Simulator. get. Toner(); 114 } 115 116 // returns ID of print job that is currently printing 117 public String get. Current. Print. Job() 118 { 119 return printer. Simulator. get. Current. Print. Job(); 120 } 121 122 // returns array of all queued up print jobs 123 public String[] get. Pending. Print. Jobs() 124 { 125 return printer. Simulator. get. Pending. Print. Jobs(); 126 } 127 128 // sets status availability of printer 129 public void set. Online( Boolean online ) 130 { 131 if ( online. boolean. Value() == true ) 132 printer. Simulator. set. Online(); 133 else 134 printer. Simulator. set. Offline(); 135 } 136 137 // fills up the paper tray again with paper. 138 public void replenish. Paper. Tray() 139 { 140 printer. Simulator. replenish. Paper. Tray ( 141 Printer. PAPER_STACK_SIZE ); 142 } 143 Outline 3. Implement management solution 2001 Prentice Hall, Inc. All rights reserved.

144 // cancel pending print jobs 145 public void cancel. Pending. Print. Jobs() 146

144 // cancel pending print jobs 145 public void cancel. Pending. Print. Jobs() 146 { 147 printer. Simulator. cancel. Pending. Print. Jobs(); 148 } 149 150 // start the printing process 151 public void start. Printing() 152 { 153 printer. Simulator. start. Printing. Process(); 154 } 155 156 // send out of paper event to JMX layer 157 public void fire. Out. Of. Paper. Event() 158 { Specify 159 // construct parameters and signatures 160 Object[] parameter = new Object[ 1 ]; 161 parameter[ 0 ] = new Notification( 162 "Printer. Event. OUT_OF_PAPER", this, 0 L ); 163 String[] signature = new String[ 1 ]; 164 signature[ 0 ] = "javax. management. Notification" ; 165 166 // invoke notification 167 try { 168 m. Bean. Server. invoke( event. Broadcaster. Name, 169 "send. Notification", parameter, signature ); 170 } 171 172 // handle exception when invoking method 173 catch ( Reflection. Exception exception ) { 174 exception. print. Stack. Trace(); 175 } 176 Outline 4. Send out-of-paper notification 4. 1 specify notification 4. 2 send notification Send notification 2001 Prentice Hall, Inc. All rights reserved.

177 // handle exception when communicating with MBean 178 catch ( MBean. Exception exception

177 // handle exception when communicating with MBean 178 catch ( MBean. Exception exception ) { 179 exception. print. Stack. Trace(); 180 } 181 182 // handle exception if MBean not found 183 catch ( Instance. Not. Found. Exception exception ) { 184 exception. print. Stack. Trace(); 185 } 186 187 } // end method out. Of. Paper. Event 188 189 // send low toner event to JMX layer 190 public void fire. Low. Toner. Event() 191 { 192 // construct parameters and signatures 193 Object[] parameter = new Object[ 1 ]; 194 parameter[ 0 ] = new Notification( 195 "Printer. Event. LOW_TONER", this, 0 L ); 196 String[] signature = new String[ 1 ]; 197 signature[ 0 ] = "javax. management. Notification" ; 198 199 // invoke notification 200 try { 201 m. Bean. Server. invoke( event. Broadcaster. Name, 202 "send. Notification", parameter, signature ); 203 } 204 205 // handle exception when invoking method 206 catch ( Reflection. Exception exception ) { 207 exception. print. Stack. Trace(); 208 } 209 Outline Specify 5. Send low-toner notification 5. 1 specify notification 5. 2 send notification Send notification 2001 Prentice Hall, Inc. All rights reserved.

210 // handle exception communicating with MBean 211 catch ( MBean. Exception exception )

210 // handle exception communicating with MBean 211 catch ( MBean. Exception exception ) { 212 exception. print. Stack. Trace(); 213 } 214 215 // handle exception if MBean not found 216 catch ( Instance. Not. Found. Exception exception ) { 217 exception. print. Stack. Trace(); 218 } 219 220 } // end method low. Toner. Event 221 222 // send paper jam event to JMX layer 223 public void fire. Paper. Jam. Event() 224 { 225 // construct parameters and signatures 226 Object[] parameter = new Object[ 1 ]; 227 parameter[ 0 ] = new Notification( 228 "Printer. Event. PAPER_JAM", this, 0 L ); 229 String[] signature = new String[ 1 ]; 230 signature[ 0 ] = "javax. management. Notification" ; 231 232 // invoke notification 233 try { 234 m. Bean. Server. invoke( event. Broadcaster. Name, 235 "send. Notification", parameter, signature ); 236 } 237 238 // handle exception when invoking method 239 catch( Reflection. Exception exception ) { 240 exception. print. Stack. Trace(); 241 } 242 Outline 6. Send paper-jam notification Specify notification 6. 1 specify notification Send notification 2001 Prentice Hall, Inc. All rights reserved.

243 // handle exception communicating with MBean 244 catch( MBean. Exception exception ) {

243 // handle exception communicating with MBean 244 catch( MBean. Exception exception ) { 245 exception. print. Stack. Trace(); 246 } 247 248 // handle exception if MBean not found 249 catch( Instance. Not. Found. Exception exception ) { 250 exception. print. Stack. Trace(); 251 } 252 253 } // end method paper. Jam. Event 254 255 // interface implementation 256 public void out. Of. Paper() 257 { 258 // delegate call 259 fire. Out. Of. Paper. Event (); 260 } 261 262 // interface implementation Interface 263 public void low. Toner() 264 { 265 // delegate call 266 fire. Low. Toner. Event(); 267 } 268 269 // interface implementation 270 public void paper. Jam() 271 { 272 // delegate call 273 fire. Paper. Jam. Event(); 274 } 275 } Outline 6. 2 send notification implementation 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 // Fig. 22. 6: Printer. Simulator. java 2 // This class simulates

Outline 1 // Fig. 22. 6: Printer. Simulator. java 2 // This class simulates a printer device on a network. 3 4 // Deitel package 5 package com. deitel. advjhtp 1. jmx. Printer; 6 7 // java core package 8 import java. util. Stack; 9 10 public class Printer. Simulator implements Runnable { 11 12 private Stack printer. Stack = new Stack(); 13 private boolean is. Online = true; 14 private boolean is. Printing = false; Class 15 private boolean is. Paper. Jam = false; Printer. Simulator 16 17 // 50 sheets of paper in tray 1. Declarations 18 private Integer paper. In. Tray = new Integer( 50 ); 19 2. Constructor 20 // 100% full of ink 21 private Integer toner. Cartridge = new Integer( 100 ); 22 23 private String current. Print. Job; 24 private boolean is. Alive = true; 25 private Printer. Event. Listener event. Listener ; 26 27 // default public constructor 28 public Printer. Simulator( Constructor takes 29 Printer. Event. Listener listener ) Printer. Event. Listener 30 { as input 31 event. Listener = listener; 32 } 33 2001 Prentice Hall, Inc. All rights reserved.

34 // stops execution of thread 35 public void stop() 36 { 37 is.

34 // stops execution of thread 35 public void stop() 36 { 37 is. Alive = 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 ( is. Alive ) { 48 49 // pause if offline 50 if ( !is. Online ) { 51 synchronized ( this ) { 52 53 // waits for printer become online 54 try { 55 wait(); 56 } 57 58 // if interrupt occurs 59 catch ( Interrupted. Exception exception ) { 60 exception. print. Stack. Trace(); 61 System. exit( -1 ); 62 } 63 64 } // end synchronized 65 66 } // end if 67 Outline 3. Simulate printer activities 2001 Prentice Hall, Inc. All rights reserved.

Outline 68 // prints one job from print job stack 69 start. Printing. Process

Outline 68 // prints one job from print job stack 69 start. Printing. Process (); 70 71 } // end while 72 } 73 74 public void start. Printing. Process() 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 ( ( paper. In. Tray. int. Value() > 0 ) && 83 ( toner. Cartridge. int. Value() > 10 ) && 84 ( !is. Paper. Jam ) ) { Simulate printing process 85 3. Simulate printer 86 // start the printing process activities 87 current. Print. Job = get. Next. Print. Job(); 88 is. Printing = true; 89 90 // 12 seconds to print a normal document 91 Thread. sleep( 1000 * 12 ); 92 93 // each print job uses 10 pages 94 update. Paper. In. Tray ( paper. In. Tray. int. Value() - 10 ); 95 update. Toner(); 96 update. Paper. Jam (); 97 is. Printing = false; 98 99 // make sure no references are left dangling 100 current. Print. Job = null; 101 } 2001 Prentice Hall, Inc. 102 } All rights reserved.

103 104 // if interrupt occurs 105 catch ( Interrupted. Exception exception ) {

103 104 // if interrupt occurs 105 catch ( Interrupted. Exception exception ) { 106 exception. print. Stack. Trace(); 107 System. exit( -1 ); 108 } 109 110 } // end method start. Printing. Process 111 112 // returns current printed job 113 public String get. Current. Print. Job() 114 { 115 return current. Print. Job; 116 } 117 118 // is printer online? 119 public Boolean is. Online() 120 { 121 return new Boolean ( is. Online ); 122 } 123 124 // update amount of paper in paper tray 125 public synchronized void update. Paper. In. Tray( int new. Value ) 126 { 127 paper. In. Tray = new Integer ( new. Value ); 128 Add 129 // fire event if paper tray low 130 if ( paper. In. Tray. int. Value() <= 0 ) { 131 event. Listener. out. Of. Paper. Event(); 132 } 133 } 134 135 // is paper jammed? 136 public Boolean is. Paper. Jam() 137 { 138 return new Boolean( is. Paper. Jam ); 139 } Outline 3. Simulate printer activities paper to paper tray 2001 Prentice Hall, Inc. All rights reserved.

140 141 // cancel pending print jobs 142 public void cancel. Pending. Print. Jobs()

140 141 // cancel pending print jobs 142 public void cancel. Pending. Print. Jobs() 143 { 144 synchronized ( printer. Stack ) { 145 printer. Stack. clear(); 146 } 147 } 148 149 // update amount of toner available in toner cartridge 150 public synchronized void update. Toner() 151 { 152 // after every print job, toner levels drop 1% 153 toner. Cartridge = new Integer ( 154 toner. Cartridge. int. Value() - 1 ); 155 156 // fire event if toner is low Update toner in 157 if ( toner. Cartridge. int. Value() <= 10 ) { toner cartridge 3. Simulate printer 158 event. Listener. low. Toner. Event(); activities 159 } 160 } 161 162 public synchronized void update. Paper. Jam() 163 { 164 if ( Math. random() > 0. 9 ) { Issue paper jam 165 is. Paper. Jam = true; 166 event. Listener. paper. Jam. Event(); event randomly 167 } 168 } 169 170 // returns number of pages in paper tray 171 public synchronized Integer get. Paper. Tray() 172 { 173 return paper. In. Tray; 174 } 2001 Prentice Hall, Inc. 175 All rights reserved. Outline

Outline 176 // returns amount of toner in toner cartridge 177 public synchronized Integer

Outline 176 // returns amount of toner in toner cartridge 177 public synchronized Integer get. Toner() 178 { 179 return toner. Cartridge; 180 } 181 182 // generates a random number of print jobs with varying IDs 183 public void populate. Print. Stack() 184 { 185 int num. Of. Jobs = ( int ) ( Math. random ( ) * 10 ) + 1; 186 187 // generate print jobs 188 synchronized ( printer. Stack ) { Generate print jobs 189 for ( int i = 0 ; i < num. Of. Jobs ; i++ ) { 190 printer. Stack. add ( "PRINT_JOB_ID #" + i ); 191 } 3. Simulate printer 192 } activities 193 } 194 195 // returns next print job in stack, populating the stack 196 // if it is empty 197 public String get. Next. Print. Job() 198 { 199 if ( printer. Stack. is. Empty() ) { 200 populate. Print. Stack ( ); 201 202 // simulates absence of print jobs 203 try { 204 Thread. sleep ( 205 ( int ) ( Math. random() * 1000 * 10 ) ); 206 } 207 2001 Prentice Hall, Inc. All rights reserved.

208 // if interrupt occurs 209 catch ( Interrupted. Exception exception ) { 210

208 // if interrupt occurs 209 catch ( Interrupted. Exception exception ) { 210 exception. print. Stack. Trace() ; 211 System. exit ( -1 ) ; 212 } 213 } 214 215 // Remove topmost queued resource. 216 String print. Job; 217 218 synchronized ( printer. Stack ) { 219 print. Job = ( String ) printer. Stack. pop(); 220 } 221 222 return print. Job; 223 224 } // end method get. Next. Print. Job 225 226 // returns all jobs yet to be printed 227 public String[] get. Pending. Print. Jobs() 228 { 229 String[] pending. Print. Jobs; 230 Get 231 // create array of pending print jobs 232 synchronized ( printer. Stack ) { 233 Object[] temp = printer. Stack. to. Array() ; 234 pending. Print. Jobs = new String[ temp. length ] ; 235 236 for ( int i = 0 ; i < pending. Print. Jobs. length ; i++ ) { 237 pending. Print. Jobs [ i ] = ( String )temp[ i ]; 238 } 239 } 240 241 return pending. Print. Jobs; 242 243 } // end method get. Pending. Print. Jobs Outline 3. Simulate printer activities all jobs to be printed 2001 Prentice Hall, Inc. All rights reserved.

Outline 244 245 // sets printer status to online 246 public void set. Online()

Outline 244 245 // sets printer status to online 246 public void set. Online() 247 { 248 is. Online = true; 249 250 // notify all waiting states 251 synchronized ( this ) { 252 notify. All() ; When printer becomes online, 253 } notify all waiting states 254 } 255 256 // sets printer status to offline 257 public void set. Offline() 258 { 3. Simulate printer 259 is. Online = false; activities 260 } 261 262 // replenishes amount of paper in paper tray to specified 263 // value 264 public void replenish. Paper. Tray ( int paper. Stack ) 265 { 266 update. Paper. In. Tray( paper. Stack ) ; 267 } 268 269 // is printer printing? 270 public boolean is. Printing() 271 { 272 return is. Printing; 273 } 274 } 2001 Prentice Hall, Inc. All rights reserved.

24. 3. 2 Implementation of the JMX Management Agent • JMX agent – MBean

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

1 // Fig. 22. 8: Printer. Management. Agent. java 2 // This application creates

1 // Fig. 22. 8: Printer. Management. Agent. java 2 // This application creates an MBean. Server and starts an RMI 3 // connector MBean service. 4 5 // deitel package 6 package com. deitel. advjhtp 1. jmx. Printer. Management; 7 8 // JMX core packages 9 import javax. management. *; 10 11 public class Printer. Management. Agent { 12 13 public static void main( String[] args ) Class 14 { Printer. Management. Agent 15 Object. Instance rmi. Connector. Server = null; 16 Object. Instance printer = null; 1. main 17 Object. Instance broadcaster = null; 18 Object. Name object. Name = null; 1. 1 create MBean. Server 19 20 // create an MBean. Server Create MBean server 1. 2 create RMI 21 MBean. Server server = connector server 22 MBean. Server. Factory. create. MBean. Server(); 23 1. 3 create broadcaster 24 // create an RMI connector service, a printer simulator MBean 25 // MBean and a broadcaster MBean 26 try { 27 Create RMI connector server 28 // create an RMI connector server 29 rmi. Connector. Server = server. create. MBean ( 30 "com. sun. jdmk. comm. Rmi. Connector. Server", null ); 31 32 // create a broadcaster MBean Create broaddcaster MBean 33 String name = server. get. Default. Domain() 34 + ": type=" + "Printer. Event. Broadcaster"; 35 String class. Name = "com. deitel. advjhtp 1. jmx. " 2001 Prentice Hall, Inc. 36 + "Printer. Simulator. Printer. Event. Broadcaster" All rights reserved. Outline

37 38 object. Name = new Object. Name( name ); 39 printer = server.

37 38 object. Name = new Object. Name( name ); 39 printer = server. create. MBean( 40 class. Name, object. Name ); 41 42 // create a printer simulator MBean 43 name = server. get. Default. Domain() 44 + ": type=" + "Printer"; 45 class. Name = "com. deitel. advjhtp 1. jmx. " 46 + "Printer. Simulator. Printer"; 47 48 object. Name = new Object. Name( name ); 49 broadcaster = server. create. MBean( 50 class. Name, object. Name ); 51 52 } // end try 53 54 // handle class not JMX-compliant MBean exception 55 catch ( Not. Compliant. MBean. Exception exception ) { 56 exception. print. Stack. Trace(); 57 } 58 59 // handle MBean constructor exception 60 catch ( MBean. Exception exception ) { 61 exception. print. Stack. Trace(); 62 } 63 64 // handle MBean already exists exception 65 catch ( Instance. Already. Exists. Exception exception ) { 66 exception. print. Stack. Trace(); 67 } 68 69 // handle MBean constructor exception 70 catch ( Reflection. Exception exception ) { 71 exception. print. Stack. Trace(); 72 } Outline Create printer simulator MBean 1. 4 create printer simulator MBean 1. 5 handle exceptions 2001 Prentice Hall, Inc. All rights reserved.

73 74 // handle invalid object name exception 75 catch ( Malformed. Object. Name.

73 74 // handle invalid object name exception 75 catch ( Malformed. Object. Name. Exception exception) { 76 exception. print. Stack. Trace(); 77 } Specify RMI 78 connector 79 // set port number server port 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 set. Port on Rmi. Connector. Server MBean 86 // start the RMI connector service 87 try { 88 server. invoke( 89 rmi. Connector. Server. get. Object. Name(), "set. Port", 90 parameter, signature ); 91 server. invoke( 92 rmi. Connector. Server. get. Object. Name(), "start" , 93 new Object[ 0 ], new String[ 0 ] ); 94 } 95 96 // handle exception when executing method 97 catch ( Reflection. Exception exception ) { 98 exception. print. Stack. Trace(); 99 } 100 101 // handle exception communicating with MBean 102 catch ( MBean. Exception exception ) { 103 exception. print. Stack. Trace(); 104 } 105 Outline 1. 6 specify RMI Set RMI connector server port 1. 7 set RMI connector server port Start RMI 1. 8 start RMI connector server 2001 Prentice Hall, Inc. All rights reserved.

Outline 106 // handle exception if MBean not found 107 catch ( Instance. Not.

Outline 106 // handle exception if MBean not found 107 catch ( Instance. Not. Found. Exception exception ) { 108 exception. print. Stack. Trace(); 109 } 110 111 } // end method main 112 } __________________________________ 1 // Fig. 22. 9: Printer. Event. Broadcaster. MBean. java 2 // This class defines the MBean interface. 3 4 // deitel package 5 package com. deitel. advjhtp 1. jmx. Printer. Simulator; 6 7 // JMX core packages 8 import javax. management. Notification; 9 10 public interface Printer. Event. Broadcaster. MBean { 11 12 public void send. Notification( Notification notification ); 13 } Interface Printer. Event. Broadcaster. MBean 1. Declarations 2001 Prentice Hall, Inc. All rights reserved.

24. 3. 3 Broadcasting and Receiving Notifications • Broadcast notifications – Notification broadcaster MBean

24. 3. 3 Broadcasting and Receiving Notifications • Broadcast notifications – Notification broadcaster MBean • Receive notifications 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 // Fig. 22. 10: Printer. Event. Broadcaster. java 2 // This class

Outline 1 // Fig. 22. 10: Printer. Event. Broadcaster. java 2 // This class defines an MBean that 3 // provides events information. 4 5 // deitel package 6 package com. deitel. advjhtp 1. jmx. Printer. Simulator; 7 8 // JMX core packages 9 import javax. management. MBean. Notification. Info; 10 import javax. management. Notification. Broadcaster. Support ; 11 12 // extends Notification. Broadcaster. Support to adopt its 13 // functionality. 14 public class Printer. Event. Broadcaster 15 extends Notification. Broadcaster. Support 16 implements Printer. Event. Broadcaster. MBean { 17 18 private static final String OUT_OF_PAPER = 19 "Printer. Event. OUT_OF_PAPER"; 20 private static final String LOW_TONER = 21 "Printer. Event. LOW_TONER"; 22 private static final String PAPER_JAM = 23 "Printer. Event. PAPER_JAM"; 24 Class Printer. Event. Broadcaster 1. Declarations 2001 Prentice Hall, Inc. All rights reserved.

Outline 25 // provide information about deliverable events 26 public MBean. Notification. Info[] get.

Outline 25 // provide information about deliverable events 26 public MBean. Notification. Info[] get. Notification. Info() 27 { 28 // array containing descriptor objects 29 MBean. Notification. Info [] descriptor. Array = 30 new MBean. Notification. Info[ 1 ]; Implement 31 32 // different event types get. Notification. Info 33 String[] notification. Types = new String[ 3 ]; to describe events 34 notification. Types[ 0 ] = 35 Printer. Event. Broadcaster. OUT_OF_PAPER; 36 notification. Types[ 1 ] = 37 Printer. Event. Broadcaster. LOW_TONER; Specify event types 38 notification. Types[ 2 ] = 39 Printer. Event. Broadcaster. PAPER_JAM; 2. Implement 40 get. Notification. Info 41 // notification class type to describe events 42 String class. Type = "javax. management. Notification" ; 43 44 // description of MBean. Notification. Info Event description 45 String description = 46 "Notification types for Printer. Event. Broadcaster "; 47 48 // populate descriptor array 49 descriptor. Array[ 0 ] = new MBean. Notification. Info( 50 notification. Types , class. Type, description ); 51 52 return descriptor. Array; 53 54 } // end method get. Notification. Info 55 } 2001 Prentice Hall, Inc. All rights reserved.

1 // Fig. 22. 11: Printer. Event. Handler. java 2 // The class adds

1 // Fig. 22. 11: Printer. Event. Handler. 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. advjhtp 1. jmx. Client; 7 8 // JMX core packages 9 import javax. management. *; 10 11 // JDMK core packages 12 import com. sun. jdmk. comm. Rmi. Connector. Client; 13 import com. sun. jdmk. comm. Client. Notification. Handler ; 14 15 // Deitel packages Class 16 import com. deitel. advjhtp 1. jmx. Printer. *; Printer. Eventhandler 17 18 public class Printer. Event. Handler { 19 1. Inner class 20 private Rmi. Connector. Client rmi. Client ; 21 private Printer. Event. Listener event. Target ; Inner Notification. Listener class 22 Notification. Listener 23 // notification listener annonymous inner class 24 private Notification. Listener notification. Listener = 25 new Notification. Listener() { 26 public void handle. Notification( 27 Notification notification, Object handback ) 28 { 29 // retrieve notification type 30 String notification. Type = notification. get. Type(); 31 32 // handle different notifications 33 if ( notification. Type. equals( 34 "Printer. Event. OUT_OF_PAPER" ) ) { 35 handle. Out. Of. Paper. Event(); 2001 Prentice Hall, Inc. 36 return; All rights reserved. 37 } Outline

39 if ( notification. Type. equals( 40 "Printer. Event. LOW_TONER" ) ) { 41

39 if ( notification. Type. equals( 40 "Printer. Event. LOW_TONER" ) ) { 41 handle. Low. Toner. Event(); 42 return; 43 } 44 45 if ( notification. Type. equals( 46 "Printer. Event. PAPER_JAM" ) ) { 47 handle. Paper. Jam. Event(); 48 return; 49 } 50 51 } // end method handle. Notification 52 53 }; // end annonymous inner class 2. Constructor 54 55 // default constructor 2. 1 set notification 56 public Printer. Event. Handler( mode 57 Rmi. Connector. Client input. Rmi. Client , 58 Manager. Side. Printer. Event. Listener input. Event. Target ) 2. 2 register listener 59 { to RMI connector 60 rmi. Client = input. Rmi. Client; client 61 event. Target = input. Event. Target; Set notification mode 62 63 // set notification push mode 64 rmi. Client. set. Mode( Client. Notification. Handler. PUSH_MODE ); 65 66 // register listener 67 try { 68 Object. Name object. Name = new Object. Name( Register listener to 69 rmi. Client. get. Default. Domain() RMI connector client 70 + ": type=" + "Printer. Event. Broadcaster" ); 71 72 rmi. Client. add. Notification. Listener( object. Name, 73 notification. Listener, null ); 2001 Prentice Hall, Inc. 74 } All rights reserved. Outline

Outline 75 76 // if MBean does not exist in the MBean server 77

Outline 75 76 // if MBean does not exist in the MBean server 77 catch ( Instance. Not. Found. Exception exception) { 78 exception. print. Stack. Trace(); 79 } 80 81 // if the format of the object name is wrong 82 catch ( Malformed. Object. Name. Exception exception ) { 83 exception. print. Stack. Trace(); 84 } 85 86 } // end Printer. Event. Handler constructor 87 88 // delegate out of paper event 89 private void handle. Out. Of. Paper. Event() 3. Delegate printer 90 { events 91 event. Target. out. Of. Paper(); 92 } 93 Delegate printer events 94 // delegate low toner event 95 private void handle. Low. Toner. Event() 96 { 97 event. Target. low. Toner(); 98 } 99 100 // delegate paper jam event 101 private void handle. Paper. Jam. Event() 102 { 103 event. Target. paper. Jam(); 104 } 105 } 2001 Prentice Hall, Inc. All rights reserved.

24. 3. 4 Management Application • Management application for the case study – GUI

24. 3. 4 Management Application • Management application for the case study – GUI for managing the printer • Client. Printer. Management • Manager. Side. Printer. Event. Listener • Printer. Management. GUI 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 // Fig. 22. 12: Client. Printer. Management. java 2 // This application

Outline 1 // Fig. 22. 12: Client. Printer. Management. java 2 // This application establishes a connection to the MBean. Server 3 // and creates an MBean for Printer. Simulator. 4 5 // deitel package 6 package com. deitel. advjhtp 1. 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 Class 15 // JDMX core packages Client. Printer. Management 16 import com. sun. jdmk. comm. Rmi. Connector. Client; 17 import com. sun. jdmk. comm. Rmi. Connector. Address; 1. Constructor 18 19 public class Client. Printer. Management { 1. 1 get RMI connector 20 client 21 private Rmi. Connector. Client rmi. Client ; 22 23 // instantiate client connection Get RMI connector client 24 public Client. Printer. Management () 25 { 26 // create connector client instance 27 rmi. Client = new Rmi. Connector. Client(); 28 29 // create address instance 30 Rmi. Connector. Address rmi. Address = 31 new Rmi. Connector. Address(); 32 2001 Prentice Hall, Inc. All rights reserved.

Set RMI 33 // specify port 34 rmi. Address. set. Port( 5555 ); client

Set RMI 33 // specify port 34 rmi. Address. set. Port( 5555 ); client port 35 36 // establish connection Establish 37 rmi. Client. connect( rmi. Address ); 38 connection 39 } // end Clinet. Printer. Management constructor 40 41 // return Rmi. Connector. Client reference 42 public Rmi. Connector. Client get. Client () 43 { 44 return rmi. Client; 45 } 46 47 public static void main( String[] args ) 48 { 49 // instantiate client connection 50 Client. Printer. Management client. Manager = 51 new Client. Printer. Management (); 52 53 // get RMIConnector. Client handle 54 Rmi. Connector. Client client = client. Manager. get. Client(); 55 56 // start GUI 57 Printer. Management. GUI printer. Management. GUI = Invoke 58 new Printer. Management. GUI( client ); 59 60 // display the output 61 printer. Management. GUI. set. Size( 62 new Dimension( 500, 500 ) ); 63 printer. Management. GUI. set. Visible( true ); 64 65 } // end method main 66 } Outline 1. 2 set RMI client port 1. 3 establish connection 2. Get Rmi. Connector. Client reference 3. main 3. 1 invoke constructor GUI 3. 2 invoke GUI 2001 Prentice Hall, Inc. All rights reserved.

1 // Fig. 22. 14: Printer. Management. GUI. java 2 // This class defines

1 // Fig. 22. 14: Printer. Management. GUI. java 2 // This class defines the GUI for the 3 // printer management application. 4 5 // deitel package 6 package com. deitel. advjhtp 1. 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 Class 16 import javax. management. *; Printer. Management. GUI 17 18 // JDMX core packages 1. Import packages 19 import com. sun. jdmk. comm. Rmi. Connector. Client; 20 import com. sun. jdmk. comm. Rmi. Connector. Address; 2. Inner class 21 Text. Appender 22 // Deitel packages 23 import com. deitel. advjhtp 1. jmx. Printer. *; 24 25 public class Printer. Management. GUI extends JFrame 26 implements Printer. Event. Listener { 27 28 // Text. Appender appends text to a JText. Area. This Runnable 29 // object should be executed only using Swing. Utilities 30 // methods invoke. Later or invoke. And. Wait as it modifies 31 // a live Swing component. 32 private class Text. Appender implements Runnable { 33 Inner class 34 private String text; Text. Appender 35 private JText. Area text. Area; 2001 Prentice Hall, Inc. 36 All rights reserved. Outline

37 // Text. Appender constructor 38 public Text. Appender( JText. Area area, String new.

37 // Text. Appender constructor 38 public Text. Appender( JText. Area area, String new. Text ) 39 { 40 text = new. Text; 41 text. Area = area; 42 } 43 44 // display new text in JText. Area 45 public void run() 46 { 47 // append new message 48 text. Area. append( text ); 49 50 // move caret to end of message. Area to ensure new 51 // message is visible on screen 52 text. Area. set. Caret. Position( 53 text. Area. get. Text(). length() ); 3. Constructor 54 } 55 3. 1 define status 56 } // end Text. Appender inner class panel 57 58 private Object. Name object. Name; 59 private Rmi. Connector. Client client; 60 private JText. Area printer. Status. Text. Area = new JText. Area(); 61 private JText. Area printer. Event. Text. Area = new JText. Area(); 62 63 public Printer. Management. GUI( Rmi. Connector. Client rmi. Client ) 64 { 65 super( "JMX Printer Management Example" ); 66 67 Container container = get. Content. Pane(); Define status 68 panel 69 // status panel 70 JPanel printer. Status. Panel = new JPanel(); 71 printer. Status. Panel. set. Preferred. Size( 2001 Prentice Hall, Inc. 72 new Dimension( 512, 200 ) ); All rights reserved. Outline

73 JScroll. Pane status. Scroll. Pane = new JScroll. Pane(); 74 status. Scroll. Pane.

73 JScroll. Pane status. Scroll. Pane = new JScroll. Pane(); 74 status. Scroll. Pane. set. Autoscrolls( true ); 75 status. Scroll. Pane. set. Preferred. Size( 76 new Dimension( 400, 150 ) ); 77 status. Scroll. Pane. get. Viewport(). add( 78 printer. Status. Text. Area, null ); 79 printer. Status. Panel. add( status. Scroll. Pane, null ); 80 81 // buttons panel 82 JPanel button. Panel = new JPanel(); 83 button. Panel. set. Preferred. Size( 84 new Dimension( 512, 200 ) ); Define buttons 85 panel 86 // define action for Check Status button 87 JButton check. Status. Button = 88 new JButton( "Check Status" ); 89 check. Status. Button. add. Action. Listener( 3. 2 define buttons 90 panel 91 new Action. Listener() { 92 3. 2. 1 define actions 93 public void action. Performed( Action. Event event ) { for the buttons 94 check. Status. Button. Action( event ); Define actions for 95 } the buttons 96 } 97 ); 98 99 // define action for Add Paper button 100 JButton add. Paper. Button = new JButton( "Add Paper" ); 101 add. Paper. Button. add. Action. Listener( 102 new Action. Listener() { 103 104 public void action. Performed(Action. Event event) { 105 add. Paper. Button. Action( event ); 106 } 107 } 2001 Prentice Hall, Inc. 108 ); All rights reserved. 109 Outline

110 // define action for Cancel Pending Print Jobs button 111 JButton cancel. Pending.

110 // define action for Cancel Pending Print Jobs button 111 JButton cancel. Pending. Print. Jobs. Button = new JButton( 112 "Cancel Pending Print Jobs" ); 113 cancel. Pending. Print. Jobs. Button. add. Action. Listener( 114 new Action. Listener() { Define actions 115 116 public void action. Performed( Action. Event event ) { for the buttons 117 cancel. Pending. Print. Jobs. Button. Action ( event ); 118 } 119 } 120 ); 121 122 // add three buttons to the panel 123 button. Panel. add( check. Status. Button, null ); 124 button. Panel. add( add. Paper. Button, null ); 125 button. Panel. add( cancel. Pending. Print. Jobs. Button , null ); 126 127 // events panel 128 JPanel printer. Event. Panel = new JPanel(); 129 printer. Event. Panel. set. Preferred. Size( 3. 3 define events Define panel events panel 130 new Dimension( 512, 200) ); 131 JScroll. Pane events. Scroll. Pane = new JScroll. Pane(); 132 events. Scroll. Pane. set. Autoscrolls( true ); 133 events. Scroll. Pane. set. Preferred. Size( 134 new Dimension( 400, 150 ) ); 135 events. Scroll. Pane. get. Viewport(). add( 136 printer. Event. Text. Area, null ); 137 printer. Event. Panel. add( events. Scroll. Pane, null ); 138 139 // initialize the text 140 printer. Status. Text. Area. set. Text( "Printer Status: ---n" ); 141 printer. Event. Text. Area. set. Text( "Events: --- n" ); 142 143 // put all the panels together 144 container. add( printer. Status. Panel, Border. Layout. NORTH ); 2001 Prentice Hall, Inc. 145 container. add( printer. Event. Panel, Border. Layout. SOUTH ); All rights reserved. 146 container. add( button. Panel, Border. Layout. CENTER ); Outline

147 148 // set Rmi. Connector. Client reference 149 client = rmi. Client; 150

147 148 // set Rmi. Connector. Client reference 149 client = rmi. Client; 150 Start printing 151 // invoke method start. Printing of the via MBean 152 // Printer. Simulator MBean 153 try { 154 String name = client. get. Default. Domain() 155 + ": type=" + "Printer"; 156 object. Name = new Object. Name( name ); 157 client. invoke( object. Name, "start. Printing", 158 new Object[ 0 ], new String[ 0 ] ); 159 } 160 161 // invalid object name 162 catch ( Malformed. Object. Name. Exception exception ) { 3. 4 start printer 163 exception. print. Stack. Trace(); simulator 164 } 165 3. 5 get 166 // if cannot invoke the method Printer. Event. Handler 167 catch ( Reflection. Exception exception) { 168 exception. print. Stack. Trace(); 169 } 170 171 // if invoked method throws exception 172 catch ( MBean. Exception exception ) { 173 exception. print. Stack. Trace(); 174 } 175 176 // if MBean is not registered with MBean server 177 catch ( Instance. Not. Found. Exception exception ) { 178 exception. print. Stack. Trace(); 179 } Get Printer. Event. Handler 180 181 // instantiate Printer. Event. Notifier 2001 Prentice Hall, Inc. 182 Printer. Event. Handler printer. Event. Handler = All rights reserved. 183 new Printer. Event. Handler( client, this ); Outline

184 185 // unregister MBean when close the window 186 add. Window. Listener( 187

184 185 // unregister MBean when close the window 186 add. Window. Listener( 187 new Window. Adapter() { 188 public void window. Closing( Window. Event event ) 189 { 190 // unregister MBean 191 try { 192 193 // unregister the Printer. Simulator MBean 194 client. unregister. MBean( object. Name ); 195 196 // unregister the Printer. Event. Broadcaster 197 // MBean 198 String name = client. get. Default. Domain() 199 + ": type=" + "Printer. Event. Broadcaster"; 200 object. Name = new Object. Name( name ); 201 client. unregister. MBean( object. Name ); 202 } 203 204 // if invalid object name 205 catch ( Malformed. Object. Name. Exception exception) { 206 exception. print. Stack. Trace(); 207 } 208 209 // if exception is caught from method pre. Deregister 210 catch ( MBean. Registration. Exception exception ) { 211 exception. print. Stack. Trace(); 212 } 213 214 // if MBean is not registered with MBean server 215 catch ( Instance. Not. Found. Exception exception ) { 216 exception. print. Stack. Trace(); 217 } 218 Outline Unregister Mbeans when close window 3. 6 define actions when close window 2001 Prentice Hall, Inc. All rights reserved.

219 // terminate the program 220 System. exit( 0 ); 221 222 } //

219 // terminate the program 220 System. exit( 0 ); 221 222 } // end method window. Closing 223 224 } // end Window. Adapter constructor 225 226 ); // end add. Window. Listener 227 228 } // end Printer. Management. GUI constructor 229 230 // out of paper events 231 public void out. Of. Paper() 232 { 233 Swing. Utilities. invoke. Later( 234 new Text. Appender( printer. Event. Text. Area, 235 " n. EVENT: Out of Paper!n" ) ); 236 } 237 238 // toner low events 239 public void low. Toner() 240 { 241 Swing. Utilities. invoke. Later( 242 new Text. Appender( printer. Event. Text. Area, 243 "n. EVENT: Toner Low!n" ) ); 244 } 245 246 // paper jam events 247 public void paper. Jam() 248 { 249 Swing. Utilities. invoke. Later( 250 new Text. Appender( printer. Event. Text. Area, 251 "n. EVENT: Paper Jam!n" ) ); 252 } Outline 4 display events in events panel Display events 2001 Prentice Hall, Inc. All rights reserved.

253 254 // add paper to the paper tray 255 public void add. Paper.

253 254 // add paper to the paper tray 255 public void add. Paper. Button. Action( Action. Event event ) 256 { 257 try { 258 client. invoke( object. Name, "replenish. Paper. Tray", 259 new Object[ 0 ], new String[ 0 ] ); 260 } 261 262 // if cannot invoke the method Add paper to paper try when 263 catch ( Reflection. Exception exception) 264 { click Add Paper button 265 exception. print. Stack. Trace(); 266 } 267 268 // if invoked method throws exception 269 catch ( MBean. Exception exception ) { 5. Add paper to 270 exception. print. Stack. Trace(); printer 271 } 272 6. Cancel pending 273 // if MBean is not registered with MBean server print jobs 274 catch ( Instance. Not. Found. Exception exception ) { 275 exception. print. Stack. Trace(); 276 } 277 Cancel pending print jobs 278 } // end method add. Paper. Button. Action 279 when click Cancel Pending 280 // cancel pending print jobs Print Jobs button 281 public void cancel. Pending. Print. Jobs. Button. Action ( 282 Action. Event event ) 283 { 284 try { 285 client. invoke( object. Name, "cancel. Pending. Print. Jobs", 286 new Object[ 0 ], new String[ 0 ] ); 287 } 2001 Prentice Hall, Inc. Outline All rights reserved.

Outline 288 289 // if cannot invoke the method 290 catch ( Reflection. Exception

Outline 288 289 // if cannot invoke the method 290 catch ( Reflection. Exception exception) 291 { 292 exception. print. Stack. Trace(); 293 } 294 295 // if invoked method throws exception 296 catch ( MBean. Exception exception ) { 297 exception. print. Stack. Trace(); 298 } 299 300 // if MBean is not registered with MBean server 301 catch ( Instance. Not. Found. Exception exception ) { 302 exception. print. Stack. Trace(); 303 } 7. Check printer’s 304 status 305 } // end method cancel. Pending. Print. Jobs. Button. Action 306 7. 1 get printer’s 307 public void check. Status. Button. Action ( Action. Event event ) status 308 { 309 Object online. Response = null; 310 Object paper. Jam. Response = null; Check printer’s status 311 Object printing. Response = null; when click Check 312 Object paper. Tray. Response = null; 313 Object pending. Print. Jobs. Response = null; Status button 314 315 // manage printer remotely 316 try { 317 318 // check if the printer is on line 319 online. Response = client. invoke( object. Name, 320 "is. Online", new Object[ 0 ], new String[ 0 ] ); 321 2001 Prentice Hall, Inc. All rights reserved.

322 // check if the printer is paper jammed 323 paper. Jam. Response =

322 // check if the printer is paper jammed 323 paper. Jam. Response = client. invoke( object. Name, 324 "is. Paper. Jam", new Object[ 0 ], new String[ 0 ] ); 325 326 // check if the printing is pringint 327 printing. Response = client. invoke( object. Name, 328 "is. Printing", new Object[ 0 ], new String[ 0 ] ); 329 330 // get the paper tray 331 paper. Tray. Response = client. invoke( object. Name, 332 "get. Paper. Tray", new Object[ 0 ], new String[ 0 ] ); 333 334 // get pending print jobs 335 pending. Print. Jobs. Response = client. invoke( object. Name, 336 "get. Pending. Print. Jobs" , new Object[ 0 ], 337 new String[ 0 ] ); 338 } 339 340 // if cannot invoke the method 341 catch ( Reflection. Exception exception ) { 342 exception. print. Stack. Trace(); 343 } 344 345 // if invoked method throws exception 346 catch ( MBean. Exception exception ) { 347 exception. print. Stack. Trace(); 348 } 349 350 // if MBean is not registered with MBean server 351 catch ( Instance. Not. Found. Exception exception ) { 352 exception. print. Stack. Trace(); 353 } 354 355 // status for the online condition 356 boolean is. Online = 357 ( ( Boolean ) online. Response ). boolean. Value(); Outline Check printer’s status by calling methods on Printer MBean 7. 2 prepare output 2001 Prentice Hall, Inc. All rights reserved.

358 359 // display status 360 if ( is. Online ) { 361 Swing.

358 359 // display status 360 if ( is. Online ) { 361 Swing. Utilities. invoke. Later( new Text. Appender( 362 printer. Status. Text. Area, 363 "n. Printer is ONLINE. n" ) ); 364 } 365 else { 366 Swing. Utilities. invoke. Later( new Text. Appender( 367 printer. Status. Text. Area, 368 "n. Printer is OFFLINE. n" ) ); 369 } 370 371 // status for the paper jam condition 372 boolean is. Paper. Jam = 373 ( ( Boolean ) paper. Jam. Response ). boolean. Value(); 374 375 // display status 376 if ( is. Paper. Jam ) { 377 Swing. Utilities. invoke. Later( new Text. Appender( 378 printer. Status. Text. Area, 379 "Paper jammed. n" ) ); 380 } 381 else { 382 Swing. Utilities. invoke. Later( new Text. Appender( 383 printer. Status. Text. Area, 384 "No Paper Jam. n" ) ); 385 } 386 387 // status for the printing condition 388 boolean is. Printing = 389 ( ( Boolean )printing. Response ). boolean. Value(); 390 Outline Append printer status to output area 7. 2 prepare output 2001 Prentice Hall, Inc. All rights reserved.

391 // display status 392 if ( is. Printing ) { 393 Swing. Utilities.

391 // display status 392 if ( is. Printing ) { 393 Swing. Utilities. invoke. Later( new Text. Appender( 394 printer. Status. Text. Area, 395 "Printer is currently printing. n" ) ); 396 } 397 else { 398 Swing. Utilities. invoke. Later( new Text. Appender( 399 printer. Status. Text. Area, 400 "Printer is not printing. n" ) ); 401 } 402 403 // status for paper tray condition 404 int paper. Remaining = 405 ( ( Integer )paper. Tray. Response ). int. Value(); 406 407 // display status 408 Swing. Utilities. invoke. Later( new Text. Appender( 409 printer. Status. Text. Area, 410 "Printer paper tray has " + paper. Remaining + 411 " pages remaining. n" ) ); 412 413 // status for pending print jobs 414 Object[] pending. Print. Jobs = 415 ( Object[] ) pending. Print. Jobs. Response ; 416 int pending. Print. Jobs. Number = pending. Print. Jobs. length; 417 418 // display status 419 Swing. Utilities. invoke. Later( new Text. Appender( 420 printer. Status. Text. Area, 421 "Number of pending print jobs: " + 422 pending. Print. Jobs. Number + "n" ) ); 423 424 425 } // end method check. Status. Button. Action 426 } Outline Append printer status to output area 7. 2 prepare output 2001 Prentice Hall, Inc. All rights reserved.

22. 3. 5 Compiling and Running the example • CLASSPATH – jdmkrt. jar and

22. 3. 5 Compiling and Running the example • CLASSPATH – jdmkrt. jar and jdmktk. jar • Compile – Files in package com. deitel. advjhtp 1. jmx. Printer. Management – Files in package com. deitel. advjhtp 1. jmx. Printer. Simulator • Run – Printer. Management. Agent – Client. Printer. Management 2001 Prentice Hall, Inc. All rights reserved.

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

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

Outline Status after out-of-paper event happened Program output Out-of-paper event happened 2001 Prentice Hall,

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

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

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

Outline Status after out-of-paper event happened Program output Out-of-paper event happened 2001 Prentice Hall,

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

Outline Status after clicked Cancel Pending Print Jobs button Program output 2001 Prentice Hall,

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