- Implement the com.ibm.btt.event.Notifier interface. For example,
say a client application needs know about all printer error messages from
any printer in the network. To implement this, create a BTTNotifier subclass
called SampleNotifier that will be instantiated on the BTT server or external
server running the printer services. This class will also implement a printerError
method that will be called by any service that is accessing a printer when
the printer fails. SampleNotifier will signal a toolkit event with the following
arguments:
- The name of the error message passed as a printerError argument
- Attributes identifying the printer address in the network
- The document to be printed when the failure occurs
public class SampleNotifier extends BTTNotifier {
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("printerEvent",this,eventAttributes);
signalEvent(printerEvent);
}
- Implement the signalEvent method for each event that the notifier
can fire.
- Create a DSE Handler in client application for the event and implement
a dispatchEvent 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;
}
- 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() {
CSClientService clientServerSession = (CSClientService)getService("CSSessionTID");
EventManager.registerInterestInRemoteEvent("printerEvent","SampleNotifier");
// 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("printerEvent", "SampleNotifier", getContext());
// The application can continue its operation flow. The handler
// processes the event as soon as it is fired.
}
When the SampleNotifier instance fires an event, it calls the
dispatchEvent method of the PrinterEventsHandler. In the handler displays
a message in the system standard output.