Java Dynamic Management Kit 3.2 Programming Guide | ||||
---|---|---|---|---|
![]() | ![]() | Chapter 15. Event Handling, Alarm Clock, and Monitoring Services | ![]() | ![]() |
The Java Dynamic Management Kit provides event handling services for forwarding events through different communications protocols. For an m-bean that provides event handling services, you have to write code that defines:
The event object to be listened for
A listener interface for the event
Methods in an m-bean for adding and removing listeners
Any event class you define must directly extend the event class defined by java.util.EventObject, or extend a subclass of java.util.EventObject. For example, you may define a series of event object interfaces for representing certain event categories. The definition of an event object named SimpleEvent is given in Example 15-1.
Example 15-1. Definition of an Event Object
public class SimpleEvent extends java.util.EventObject implements java.io.Serializable { public SimpleEvent(Simple source, Integer val) { super(source); nbChanges= val; } public Integer getNbChanges() { return nbChanges; } private Integer nbChanges; } |
Any listener you define must be an interface containing one or more methods. Each method must have exactly one input parameter. The input parameter must specify the event object that the listener listens for. The event object must be as described in Event Objects. An event listener class must always be declared as a public class. The definition of a listener interface named SimpleListener interface is given in Example 15-2. The listener in this example listens for SimpleEvent objects as defined in Example 15-1.
Example 15-2. Definition of a Listener Interface
public interface SimpleListener extends java.util.EventListener { public void handleEvent1(SimpleEvent evt); public void handleEvent2(SimpleEvent evt); } |
When writing an m-bean that provides event handling services, you have to define methods for adding and removing listeners. These methods must conform to the design patterns for event sources defined in Chapter 3. An example of how to register and remove listeners is shown in Example 15-3.
Example 15-3. Listeners in an M-Bean
public synchronized void addSimpleListener(SimpleListener x) { simpleListeners.addElement(x); } public synchronized void removeSimpleListener(SimpleListener x){ simpleListeners.removeElement(x); } |
The steps in developing a manager to receive events from an m-bean are:
Use mogen to generate interfaces and stubs for listeners and events
Implement the manager by using the code that mogen generates
Example 15-4 shows code for receiving framework events in a Java manager through an adaptor client.
Example 15-4. Receiving Framework Events
ObjectName fmkName = new ObjectName(":" + ServiceName.FRAME) ; Vector objVect = adaptor.getObject(fmkName, null) ; FrameworkMO fmkObj = (FrameworkMO)objVect.firstElement(); fmkObj.addFrameworkListenerMO(this) ; |
The mogen compiler is described in detail in Chapter 6.
An m-bean that uses a listener is associated with Java classes that represent the listener and the event that is listened for. For example, an m-bean that defines the Java class example is associated with:
The listener that the m-bean uses (exampleListener)
The event that the listener listens for (exampleEvent)
For further information, see Chapter 3.
For such an m-bean, the mogen compiler generates:
A Java interface and stub for the m-bean (exampleMO and exampleMOStub) as described in Output of the mogen Compiler in Chapter 6
A Java interface for the listener (exampleListenerMO) to be included in the Java interface (exampleMO)
A listener stub (exampleListenerStub) that is an implementation of the m-bean listener for catching m-bean events and forwarding them to the core management framework
A Java manager's view (exampleEventMO) of the event for which the listener listens
For example, an m-bean representing the Java class named Simple defines a listener interface called SimpleListener. In this example, SimpleListener listens for an event called SimpleEvent. The Java source code defining this example is shown in Example 15-1, Example 15-2 and Example 15-3. When compiling the Simple m-bean, mogen generates interfaces and stubs as illustrated in Figure 15-1. The interfaces and stubs generated are described in Table 15-1.
Table 15-1. Output for an M-Bean That Uses Listeners and Events
M-Bean Class | C-Bean Code Generated | Comment |
---|---|---|
Simple.class | SimpleMO.java | Defines which of the methods of the m-bean are accessible to a Java manager. Example 15-5 shows the generated code corresponding to the methods in the m-bean for adding and removing listeners. |
SimpleMOStub.java | Implements the methods defined in the Java interface SimpleMO. | |
SimpleListener.class | SimpleListenerMO.java | Defines the interface to be implemented to register for events on SimpleMO. In the SimpleListenerMO interface, the event object is called SimpleEventMO. |
SimpleListenerStub.java | Implements the SimpleListener interface for catching events emitted by the Simple m-bean and forwarding them to SimpleMO. | |
SimpleEvent.class | SimpleEventMO.java | Defines the event type emitted by the SimpleMO c-bean whenever its associated Simple m-bean emits an event of type SimpleEvent |
Example 15-5 shows the code generated in SimpleMO.java corresponding to the methods in the Simple m-bean for adding and removing listeners. The code shown in Example 15-5 is a subset of the code mogen generates in the SimpleMO.java when compiling the Simple m-bean.
Example 15-5. Generated Code for the SimpleMO Interface
public interface SimpleMO extends ManagedObject { ... public void addSimpleListenerMO(SimpleListenerMO x ) throws InstanceNotFoundException, IllegalAccessException, InstantiationException, ClassNotFoundException; InstanceNotFoundException, ClassNotFoundException; public void removeSimpleListenerMO(SimpleListenerMO x ); ... } |
Options of the mogen compiler enable you to generate a c-bean without any code for listeners or events. This is useful if you want to reduce the size of a c-bean, or if you want to prevent a user of the Java manager from listening for events. If you modify an m-bean without modifying its listeners, you can use these options to regenerate the c-bean without affecting code already generated for listeners and events.
To generate a c-bean without any code for listeners or events, specify any combination of these options in the command to start mogen:
-nl to generate a c-bean without code for listeners, even if the m-bean contains listeners
-nlmo to generate a c-bean without code for the Java interface for the listener
-nlas to generate a c-bean without code for the listener stub
-ne to generate a c-bean without code for events, even if the m-bean contains events
For a full list of the mogen compiler options, see Options for mogen in Chapter 6.
To use a c-bean generated for a listener, you have to implement the beanListenerMO interface to add or remove the listener on the c-bean. To add a beanListenerMO on a beanMO object, include a call to addbeanListenerMO() in the Java manager you develop. The listener waits for events sent by the corresponding bean object. Example 15-6 shows code for adding and removing a listener in a manager.
Example 15-6. Creating and Removing an Event Listener in a Manager
//Create an event listener ClientListener listener = new ClientListener(); // Add this manager as a listener for SimpleEvent events // mo.addSimpleListenerMO(listener); ... // Remove the listener // mo.removeSimpleListenerMO(listener); ... |
In the example shown in Example 15-6:
ClientListener is a class that implements the SimpleListenerMO interface.
The object mo is an instance of the SimpleMO class.
The manager listens for events sent by instances of the Simple m-bean.
When addbeanListenerMO() is called to add the listener beanListenerMO on the c-bean, the adaptor transparently instantiates a listener beanListenerStub on the remote m-bean. The code generated for beanListenerStub includes the code required for registering itself as a listener on the m-bean.
When the m-bean emits an event (beanEvent), the registered beanListenerStub forwards the event to the c-bean. The c-bean (beanMOStub) then calls all the beanListenerMO listeners registered. The mechanism for adding event listeners is illustrated in Figure 15-2.
Any event handling service you define must implement the com.sun.jaw.reference.agent.services.EventHandlerIf interface. To enable an event handling service to be identified, the class part of its object name must be ServiceName.EVENT.
![]() | ![]() | ![]() |
Event Handling, Alarm Clock, and Monitoring Services | ![]() | Alarm Clock Service |