To implement event notification, perform the following tasks:
1 Implement the com.ibm.btt.base.Notifier interface. For example, suppose a client application needs to know about all printer error messages from any printer in the network. To accomplish this purpose you need to create a DSENotifier subclass called SampleNotifier that instantiate on the server workstation for the printer services. This class also implements a printerError method called by any service that is accessing a printer when the printer fails. SampleNotifier signals a toolkit event with the following arguments:
▪ The name of the error message passed as an argument of printerError method
▪ Attributes identifying the printer address in the network
▪ The document to be printed when the failure occurred
public class SampleNotifier extends DSENotifier { public DSEEventObject printerEvent; } public SampleNotifier () { super(); } public SampleNotifier(String aName) { super(aName); } public void printerError(String aPrt, String aDocName, String message) { Hashtable eventAttributes = new Hashtable(); eventAttributes.put ("printerAddress", aPrt); eventAttributes.put ("document", aDocName); printerEvent = new DSEEventObject("mMessage",this,eventAttributes); signalEvent(printerEvent); }
You can integrate the standard Java events with the event management in the toolkit.
2 Implement the signalEvent method for each event that the notifier can send out.
Note The standard Java event notification mechanism will call any other local listeners for that event.
3 Create a handler for the event and implement a dispatchEvent method to provide the behavior to process the event. For example, the SampleNotifier uses the PrinterEventsHandler:
public class PrinterEventsHandler extends DSEHandler { } public Handler dispatchEvent(DSEEventObject anEvent) { // The dispatch method displays a string in the system standard output // containing the event information System.out.println("The event"+ anEvent.getName()+ "has been signaled. Printer:"+ anEvent.getParameters().get("printerAddress")+" Document:" +anEvent.getParamenters().get("document")); // The handler enables further event propagation by returning this return this; }
In this case, the dispatchEvent method passes a message to the system standard output.
Note If you do not want to propagate this event to the next registered handler, the return value of dispatchEvent must be null.
4 Add the handler registration to the client operation that requires event notification as shown in the following example:
import com.ibm.dse.base.*; import com.ibm.dse.clientserver.*; public class myClientOperation extends DSEClientOperation { public PrinterEventsHandler eventsHandler; } public void execute() throws Exception() { // If the client workstation where the application is running has // sessions with different server workstations, the application must // know which CSClient instance identifies the session with the // server that fires the event. This can be requested by name // from the Context class. In this case, the name of the CSClient // instance that establishes the session with a server // is "CSSessionTID". CSClientService clientServerSession = (CSClientService)getService("CSSessionTID"); EventManager.registerInterestInRemoteEvent("allEvents", "SampleNotifier", clientServerSession); // The previous statements can be run only once in the lifecycle // of the application, possibly at application startup. // At any time, the application can instantiate a handler and // register it for events coming from the SampleNotifier object. eventsHandler= new PrinterEventsHandler; eventsHandler.handleEvent("allEvents", "SampleNotifier", getContext()); // The application can continue its operation flow. The handler // processes the event as soon as it is fired. }
When the SampleNotifier instance sends out an event, it calls the dispatchEvent method of the PrinterEventsHandler. The handler displays a message in the system standard output.
▪ Create a DSEEventObject instance. DSEEventObject is a subclass of EventObject with the following attributes:
▪ name - the name of the event to which a handler should be registered
▪ sourceName - the name of the notifier that signals the event
▪ parameters - a Hash table containing the event information required by the event handler
▪ originWorkstation - the TID of the workstation generating the event
One of the signalEvent methods accepts the DSEEventObject argument.
▪ If the application is already working with an EventObject instance, use the signalEvent method that accepts a string with the event name and, if required, a Hash table containing the event attributes as arguments. This method builds a DSEEventObject that the event manager sends to the listening remote workstation.