8.3 Accessing the Reaction OSGi Service

Once activated, the Reaction OSGi service can be used by any number of client OSGi bundles to provide full asynchronous programming support. However, there are various lifecycle considerations which must be taken into account when designing a client OSGi bundle. There are three lifecycle events which the client must observe which can be summarised by the methods provided by the example LifecycleHandler class shown in Listing 8.2.


\begin{listing}
% latex2html id marker 1743\begin{verbatim}public class Life...
...verbatim}
\caption{Example of a Reaction Client Lifecycle Handler}
\end{listing}

The first lifecycle event is client startup. This should only occur once the OSGi client bundle has been started and it has been successfully bound to the Reaction service. In the example, the client startup method is used to start a repeating timer which will print a `liveness' message to the console every second.

The second lifecycle event is client teardown. This occurs when the client bundle is stopped and the Reaction service is still running. Teardown implies a clean shutdown of the client, with the client cancelling all outstanding transactions with the Reaction service. In the case of the example, this involves cancelling the repeating timer which was set up during startup.

The third potential lifecycle event is client abort. This occurs when the client bundle is still running, but the Reaction service is shut down. In normal operation this should not occur, but clients should make every attempt to clean up on receiving an abort condition. Note that when a client is aborted it is no longer able to access the Reaction service, so cleaning up is restricted to tidying up independent resources. This will typically include such things as closing open files, killing local threads and closing GUI windows.

The lifecycle event handler shown in Listing 8.2 needs to be wrapped in the standard OSGi activation API in order to integrate as an OSGi client of the Reaction service. An example of such a wrapper is shown in Listing 8.3 which implements the standard OSGi activator and service tracker interfaces. Note that the OSGi specification makes no guarantees about the thread context to be used when issuing bundle activator and service tracker callbacks, so all method calls have been made synchronised.


\begin{listing}
% latex2html id marker 1754\begin{verbatim}public class Osgi...
...}
}\end{verbatim}
\caption{Example of a Reaction Client Activator}
\end{listing}

A full implementation of this OSGi Reaction client example is provided in the osgi examples package. This includes a bundle definition file to be used in conjunction with the bnd bundle creation tool.



Subsections