LibraryLink ToToggle FramesPrintFeedback

Publishing a Service in an OSGi Container

When you develop an application that will be deployed into an OSGi container, you need to coordinate the publishing and stopping of your endpoints with the life-cycle of the bundle in which it is packaged. You want your endpoints published when the bundle is started and you want the endpoints stopped when the bundle is stopped.

You tie your endpoints life-cycle to the bundle's life-cycle by implementing an OSGi bundle activator. A bundle activator is used by the OSGi container to create the resource for a bundle when it is started. The container also uses the bundle activator to clean up the bundles resources when it is stopped.

You create a bundle activator for your application by implementing the org.osgi.framework.BundleActivator interface. The BundleActivator interface, shown in Example 9.4, it has two methods that need to be implemented.


The start() method is called by the container when it starts the bundle. This is where you instantiate and publish the endpoints.

The stop() method is called by the container when it stops the bundle. This is where you would stop the endpoints.

The bundle activator's start method is where you publish your endpoints. To publish your endpoints the start method must do the following:

  1. Instantiate an javax.xml.ws.Endpoint object for the service provider.

  2. Create an optional server context to use when publishing the service provider.

  3. Publish the service provider using one of the publish() methods.

Example 9.5 shows code for publishing a service provider.


The code in Example 9.5 does the following:

1

Instantiates a copy of the service's implementation object.

2

Creates an unpublished Endpoint for the service implementation.

3

Publish the service provider at http://localhost:9000/SoapContext/SoapPort.

The bundle activator's stop method is where you clean up the resources used by your application. Its implementation should include logic for stopping all of the endpoint's published by the application.

Example 9.6 shows a stop method for stopping a published endpoint.


You must add inform the container that the application's bundle includes a bundle activator. You do this by adding the Bundle-Activator property to the bundle's manifest. This property tells the container which class in the bundle to use when activating the bundle. Its value is the fully qualified name of the class implementing the bundle activator.

Example 9.7 shows a manifest entry for a bundle whose activator is implemented by the class com.widgetvendor.osgi.widgetActivator.