Message Passing with Java Events

During the course of this chapter, we’ve built up our own message-handling framework from scratch, relying on basic sockets, I/O streams, and object serialization to implement a protocol for sending, receiving, and handling messages. In this section, we’ll look at merging a message-passing framework with the Java event model that’s used in the AWT package for handling GUI events. The advantage of using events is the possibility of integrating your distributed system with other systems based on the Java event model, including AWT-based applications or applets.

Event Model Overview

The event model included in the Java API (version 1.1 and later) is generic enough to build event-handling protocols for general applications, not just GUI-related ones. The Java event model is based on EventObjects that are created by various event sources and handled by classes that implement an EventListener interface.

Different types of events are defined by creating subclasses of the EventObject class. The EventObject class only contains a source Object. Subclasses of EventObject can add additional data to represent event specifics. For example, the AWT package defines a KeyEvent subclass that represents keyboard events. The KeyEvent class contains data fields that specify which key was pressed to generate the event.

The Java event model is called a delegation model; events are generated by a source of some kind, and EventListeners register themselves with the event ...

Get Java Distributed Computing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.