Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to shutdown an OSGi Container (specifically equinox)

I'm looking for a best practice on shutting down an OSGi container.

Currently we are using a small launcher app which calls EclipseStarter.startup() and installs some core bundles. After that the launcher terminates.

When the test GUI (running as a bundle) is closed it calls a System.exit(0) to shutdown the container, but there must be a more elegant solution than this.

Thanks

like image 684
James Carr Avatar asked Dec 16 '09 17:12

James Carr


People also ask

How do I stop OSGi bundle?

How to deactivate current bundle if in its activate method is thrown particular exception? public void activate(BundleContext bundleContext) { try{ if(something) throw new Exception(); } catch(Exception e) { //deactivate bundle... } }

What is an OSGi container?

OSGi facilitates creating and managing modular Java components (called bundles) that can be deployed in a container. As a developer, you use the OSGi specification and tools to create one or more bundles. OSGi defines the lifecycle for these bundles. It also hosts them and supports their interactions in a container.


2 Answers

Please, don't use System.exit(0) to shut down an OSGi framework. You should to it by stopping the bundle with the ID 0, the System bundle. This way, you give all bundles a chance to shut down in an orderly manner (e.g. to free resources etc).

The OSGi specification defines the following (Core Specification, R4.x, 4.2.6 Stopping a Framework).

Shutdown can be initiated by stopping the system bundle, [...] or calling the stop method on the framework object.

In that chapter a detailed description is given what happens when a framework is shut down.

The system bundle resp. the framework object is also defined (chapter 4.6 The System Bundle):

The system bundle resembles the framework object when a framework is launched, but implementations are not required to use the same object for the framework object and the system bundle. However, both objects must have bundle id 0, same location, and bundle symbolic name.

The OSGi spec is available for free at the OSGi Alliance's website (http://www.osgi.org/Specifications/HomePage).

like image 128
Andreas Kraft Avatar answered Nov 10 '22 01:11

Andreas Kraft


i usually terminate the framework like this:

bundlecontext.getBundle(0).stop();

The bundle with id = 0 is the system-bundle

like image 37
Roland Avatar answered Nov 10 '22 00:11

Roland