Wednesday, March 21, 2001 09:48 PM
Event-driven programming in languages like C has traditionally used function pointers to pass references to functions, which are then asynchronously invoked in response to an "event" (timers, mouse clicks, and the like). Java uses interfaces to give you access to the same functionality in an object-oriented world. Here, the interface defines the methods that may be invoked by any object with access to the interface. The real "functionality" is present within some other object that actually implements this interface and it is "called back" by the target object.
Implementing callbacks, however, is not without challenges -- particularly in a distributed-object environment like RMI. Callbacks are implemented in RMI by setting the client itself as an exportable remote object (that is, make it implement some remote interface), then registering a reference of this with the remote-server object. This way, the server can asynchronously invoke the remote methods of any connected client in the same way the client can asynchronously invoke the methods implemented within the remote-server objects. See the Dr. Dobb's article on "Implementing callbacks with Java RMI" for complete details.