5.3. Synchronous and Asynchronous Events

The service events are guaranteed to be delivered synchronously, whereas the framework and bundle events do not have such a guarantee. Figure 5.1 illustrates synchronous service event dispatching when a service is unregistered.

Figure 5.1. Synchronous service event dispatching. The shaded area is code belonging to the framework implementation.


Evidently, the framework waits for all listeners to complete their work before it proceeds with the service unregistration process. This is designed to give client bundles an opportunity to prepare for the consequences of registration, unregistration, or the property change of another service in their service listeners.

Delivery of other types of events (namely bundle events and framework events) does not have such characteristics. They are asynchronous, because the framework places a pending event in an event queue and returns immediately. It does not wait for an invocation of every listener to be completed. A separate and dedicated event dispatcher thread picks up the next event from the queue and notifies all interested listeners. This scenario is depicted in Figure 5.2, where a bundle is uninstalled.

Figure 5.2. Asynchronous event dispatching with the bundle event


Although the bundle events and framework events are delivered asynchronously, they are not delivered out of order by the Java Embedded Server framework. For example, if bundle A is installed, then an active bundle B is updated, and finally A is uninstalled. Bundle event listeners are guaranteed to get this sequence of events in order: BundleEvent.INSTALLED event caused by A's installation, BundleEvent .STOPPED, BundleEvent.UPDATED, BundleEvent.STARTED events caused by B's update, and BundleEvent.UNINSTALLED event caused by A's removal.

If a bundle registers and unregisters a service in its activator, it poses an interesting situation in which a mix of asynchronous and synchronous events are involved. With respect to any one bundle, a listener is always notified in the following sequence: ServiceEvent.REGISTERED before BundleEvent.STARTED, ServiceEvent.UNREGISTERING before BundleEvent.STOPPED events. In both cases, the framework won't queue the bundle events before all service listeners have been notified. Otherwise, the dispatch order should not be assumed among asynchronous and synchronous events.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.235