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
• Recent technological advances
• JMX
– Three-level management architecture
• Instrumentation level
• Agent level
• Management level
24.1 Introduction
• JMX provides
– Platform-independence – Protocol-independence – Reusable
– Intelligent agent
– Scalability
2001 Prentice Hall, Inc. All rights reserved.
24.1 Introduction
Management Application
Java Dynamic Management Agent
Managed Resource Managed Resource Managed Resource
Instrumentation Level Agent Level
Manager Level
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
• MBean
• MBean server
24.3 Case Study
Printer MBean
RmiConnectorClient: 5555
RmiConnectorServer:
5555 MBeanServer
PrinterEventBroadcaster MBean
PrinterSimulator
Management Application
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
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
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
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
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
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;
Find MBean servers
Specify MBean server
requirement
Identify MBean server
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 }
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 }
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 notification
Send notification
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 notification
Send notification
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 notification
Send notification
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
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
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
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
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 {
Add paper to paper tray
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
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
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
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
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
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."
Create MBean server
Create RMI connector server
Create broaddcaster MBean
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
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
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 }
24.3.3 Broadcasting and Receiving Notifications
• Broadcast notifications
– Notification broadcaster MBean
• Receive notifications
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
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
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
2001 Prentice Hall, Inc.
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,
Set notification mode
Register listener to RMI connector client
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
24.3.4 Management Application
• Management application for the case study
– GUI for managing the printer
• ClientPrinterManagement
• ManagerSidePrinterEventListener
• PrinterManagementGUI
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
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
Set RMI client port
Establish connection
Invoke GUI
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
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(
Define status panel