You must be doing something wrong. I added this small bit of code to one of karaf's activators:
bundleContext.getBundle(0).getBundleContext().addBundleListener(new SynchronousBundleListener() { @Override public void bundleChanged(BundleEvent event) { System.out.println(event); } }); Here's the output when stopping under equinox: *karaf*@root()> ($.context bundle 0) stop 1 org.osgi.framework.BundleEvent[source=org.apache.karaf.shell.core_4.1.0.SNAPSHOT [28]] org.osgi.framework.BundleEvent[source=org.apache.karaf.shell.commands_4.1.0.SNAPSHOT [27]] org.osgi.framework.BundleEvent[source=org.apache.karaf.shell.commands_4.1.0.SNAPSHOT [27]] org.osgi.framework.BundleEvent[source=org.apache.karaf.service.core_4.1.0.SNAPSHOT [26]] org.osgi.framework.BundleEvent[source=org.apache.karaf.service.core_4.1.0.SNAPSHOT [26]] org.osgi.framework.BundleEvent[source=org.apache.karaf.package.core_4.1.0.SNAPSHOT [25]] org.osgi.framework.BundleEvent[source=org.apache.karaf.package.core_4.1.0.SNAPSHOT [25]] org.osgi.framework.BundleEvent[source=org.apache.karaf.management.server_4.1.0.SNAPSHOT [24]] org.osgi.framework.BundleEvent[source=org.apache.karaf.management.server_4.1.0.SNAPSHOT [24]] org.osgi.framework.BundleEvent[source=org.apache.karaf.log.core_4.1.0.SNAPSHOT [23]] org.osgi.framework.BundleEvent[source=org.apache.karaf.log.core_4.1.0.SNAPSHOT [23]] org.osgi.framework.BundleEvent[source=org.apache.karaf.kar.core_4.1.0.SNAPSHOT [22]] org.osgi.framework.BundleEvent[source=org.apache.karaf.kar.core_4.1.0.SNAPSHOT [22]] org.osgi.framework.BundleEvent[source=org.apache.karaf.jaas.modules_4.1.0.SNAPSHOT [21]] org.osgi.framework.BundleEvent[source=org.apache.karaf.jaas.modules_4.1.0.SNAPSHOT [21]] org.osgi.framework.BundleEvent[source=org.apache.karaf.jaas.config_4.1.0.SNAPSHOT [20]] org.osgi.framework.BundleEvent[source=org.apache.karaf.jaas.config_4.1.0.SNAPSHOT [20]] org.osgi.framework.BundleEvent[source=org.apache.karaf.jaas.command_4.1.0.SNAPSHOT [19]] org.osgi.framework.BundleEvent[source=org.apache.karaf.jaas.command_4.1.0.SNAPSHOT [19]] org.osgi.framework.BundleEvent[source=org.apache.karaf.instance.core_4.1.0.SNAPSHOT [18]] org.osgi.framework.BundleEvent[source=org.apache.karaf.instance.core_4.1.0.SNAPSHOT [18]] org.osgi.framework.BundleEvent[source=org.apache.karaf.features.command_4.1.0.SNAPSHOT [17]] org.osgi.framework.BundleEvent[source=org.apache.karaf.features.command_4.1.0.SNAPSHOT [17]] org.osgi.framework.BundleEvent[source=org.apache.karaf.diagnostic.core_4.1.0.SNAPSHOT [16]] org.osgi.framework.BundleEvent[source=org.apache.karaf.diagnostic.core_4.1.0.SNAPSHOT [16]] org.osgi.framework.BundleEvent[source=org.apache.karaf.config.core_4.1.0.SNAPSHOT [13]] org.osgi.framework.BundleEvent[source=org.apache.karaf.config.core_4.1.0.SNAPSHOT [13]] org.osgi.framework.BundleEvent[source=org.apache.karaf.bundle.core_4.1.0.SNAPSHOT [12]] org.osgi.framework.BundleEvent[source=org.apache.karaf.bundle.core_4.1.0.SNAPSHOT [12]] org.osgi.framework.BundleEvent[source=org.apache.aries.jmx.whiteboard_1.1.5 [10]] org.osgi.framework.BundleEvent[source=org.apache.aries.jmx.whiteboard_1.1.5 [10]] org.osgi.framework.BundleEvent[source=org.apache.aries.jmx.core_1.1.7 [9]] org.osgi.framework.BundleEvent[source=org.apache.aries.jmx.core_1.1.7 [9]] org.osgi.framework.BundleEvent[source=org.apache.aries.jmx.api_1.1.5 [8]] org.osgi.framework.BundleEvent[source=org.apache.aries.jmx.api_1.1.5 [8]] org.osgi.framework.BundleEvent[source=org.apache.karaf.deployer.features_4.1.0.SNAPSHOT [14]] org.osgi.framework.BundleEvent[source=org.apache.karaf.deployer.features_4.1.0.SNAPSHOT [14]] org.osgi.framework.BundleEvent[source=org.apache.karaf.deployer.kar_4.1.0.SNAPSHOT [15]] org.osgi.framework.BundleEvent[source=org.apache.karaf.deployer.kar_4.1.0.SNAPSHOT [15]] org.osgi.framework.BundleEvent[source=org.apache.aries.util_1.1.3 [11]] org.osgi.framework.BundleEvent[source=org.apache.aries.util_1.1.3 [11]] org.osgi.framework.BundleEvent[source=org.apache.karaf.features.core_4.1.0.SNAPSHOT [7]] org.osgi.framework.BundleEvent[source=org.apache.karaf.features.core_4.1.0.SNAPSHOT [7]] org.osgi.framework.BundleEvent[source=org.apache.felix.fileinstall_3.5.6 [6]] org.osgi.framework.BundleEvent[source=org.apache.felix.fileinstall_3.5.6 [6]] org.osgi.framework.BundleEvent[source=org.apache.felix.configadmin_1.8.12 [5]] org.osgi.framework.BundleEvent[source=org.apache.felix.configadmin_1.8.12 [5]] org.osgi.framework.BundleEvent[source=org.ops4j.pax.logging.pax-logging-log4j2_1.9.1 [4]] org.osgi.framework.BundleEvent[source=org.ops4j.pax.logging.pax-logging-log4j2_1.9.1 [4]] org.osgi.framework.BundleEvent[source=org.ops4j.pax.logging.pax-logging-api_1.9.1 [3]] org.osgi.framework.BundleEvent[source=org.ops4j.pax.logging.pax-logging-api_1.9.1 [3]] org.osgi.framework.BundleEvent[source=org.ops4j.pax.url.mvn_2.5.1 [2]] org.osgi.framework.BundleEvent[source=org.ops4j.pax.url.mvn_2.5.1 [2]] gnodet•apache-karaf-minimal/target/assembly*(*master*⚡**)*» 2016-11-18 13:29 GMT+01:00 CLEMENT Jean-Philippe < jean-philippe.clem...@fr.thalesgroup.com>: > That’s exactly what I did. The listener is not called at all. Looking at > bundles, some are Active, some Resolved, one Installed (OSGi System Bundle > 3.10.2.v20150203-1939) and one Waiting (one of mine). > > > > public final class KarafStopper { > > private static final Logger LOG = LogManager.getLogger( > KarafStopper.class); > > private static final long TIMEOUT = 3 * > 1000; // in milliseconds > > > > private final Bundle karaf; > > > > public KarafStopper(final BundleContext context) { > > System.out.println("=> blabla"); > > karaf = context.getBundle(0); > > karaf.getBundleContext().addBundleListener(new > SynchronousBundleListener() { > > @Override > > public void bundleChanged(final BundleEvent event) { > > System.out.println("Karaf: " + event.getType()); > > } > > }); > > } > > > > public void shutdown() { > > try { > > final Thread watchdog = new Thread(() -> { > > try { > > Thread.sleep(TIMEOUT); > > // Karaf refuses to stop > > LOG.error(() -> "Karaf did not stop gracefully"); > > System.exit(-1); > > } catch (final InterruptedException e) { > > // Karaf stopped normally > > } > > }); > > > > watchdog.setDaemon(true); > > watchdog.start(); > > System.out.println("Bye bye"); > > karaf.stop(Bundle.STOP_TRANSIENT); > > System.out.println("So long!"); > > } catch (final BundleException e) { > > System.out.println("Oooooooooooops: " + e); > > LOG.error(() -> "Issue while stopping karaf", e); > > } > > } > > } > > > > Regards, > > JP > > > > *De :* Guillaume Nodet [mailto:gno...@apache.org] > *Envoyé :* vendredi 18 novembre 2016 13:01 > > *À :* user > *Objet :* Re: Karaf shutdown > > > > Can you write a simple SynchronousBundleListener that prints to stdout all > received events and paste the output when stopping the framework for felix > and equinox ? > > > > 2016-11-18 12:10 GMT+01:00 CLEMENT Jean-Philippe < > jean-philippe.clem...@fr.thalesgroup.com>: > > Well, that’s not the case when a bundle refuses to stop – I get no event. > Ah I forgot to mention that Equinox is used instead of Felix. > > > > May I open a bug for this? > > > > Regards, > > JP > > > > > > *De :* Guillaume Nodet [mailto:gno...@apache.org] > *Envoyé :* vendredi 18 novembre 2016 11:52 > *À :* user > *Objet :* Re: Karaf shutdown > > > > The SynchronousBundleListener is the way to go. > > You should receive a STOPPING event for the system bundle before any other > bundle is actually stopped. > > > > 2016-11-18 11:38 GMT+01:00 CLEMENT Jean-Philippe < > jean-philippe.clem...@fr.thalesgroup.com>: > > Is there a way to get a shutdown event? > > > > Regards, > > JP > > > > *De :* CLEMENT Jean-Philippe [mailto:jean-philippe.clement@ > fr.thalesgroup.com] > *Envoyé :* jeudi 17 novembre 2016 17:03 > *À :* user@karaf.apache.org > *Objet :* Karaf shutdown > > > > Dear Karaf addicts :) > > > > I would like Karaf to stop. This may fail to happen when (at least) a > bundle does not end. So I added a watchdog which exits Java when a time > limit has been exceeded (note: maybe there is already a way to do so - ?). > The way I coded it does not work when the shutdown is made via the Karaf > prompt, so I decided to enhance it. > > > > Googling “Karaf shutdown hook”, I found an old thread talking about > SynchronousBundleListener. The issue I’m facing is that I get no event when > performing bundle0.stop(Bundle.STOP_TRANSIENT) and one of the bundles > refuses to stop. The listener is registered via > bundle0.getBundleContext().addBundleListener(new > SynchronousBundleListener() { … }). > > > > Is it normal? How to force Karaf to shutdown? > > > > PS: Karaf version is 4.0.7 > > > > JP > > > > > > -- > > ------------------------ > Guillaume Nodet > ------------------------ > > Red Hat, Open Source Integration > > > > Email: gno...@redhat.com > Web: http://fusesource.com > Blog: http://gnodet.blogspot.com/ > > > > > > > > -- > > ------------------------ > Guillaume Nodet > ------------------------ > > Red Hat, Open Source Integration > > > > Email: gno...@redhat.com > Web: http://fusesource.com > Blog: http://gnodet.blogspot.com/ > > > -- ------------------------ Guillaume Nodet ------------------------ Red Hat, Open Source Integration Email: gno...@redhat.com Web: http://fusesource.com Blog: http://gnodet.blogspot.com/