+1

Regards
JB

On 11/21/2016 10:25 AM, Guillaume Nodet wrote:
We could easily add an option to the shutdown command for that.

2016-11-21 10:21 GMT+01:00 CLEMENT Jean-Philippe
<jean-philippe.clem...@fr.thalesgroup.com
<mailto:jean-philippe.clem...@fr.thalesgroup.com>>:

    Ok, things clear, thank you for the detailed explanation :)____

    __ __

    In fact I just want to ensure Karaf to shut down within a given time
    limit. Is there a way to do so?____

    __ __

    Regards,____

    JP____

    __ __

    *De :*Guillaume Nodet [mailto:gno...@apache.org
    <mailto:gno...@apache.org>]
    *Envoyé :* vendredi 18 novembre 2016 20:16


    *À :* user
    *Objet :* Re: Karaf shutdown____

    __ __

    __ __

    __ __

    2016-11-18 17:58 GMT+01:00 CLEMENT Jean-Philippe
    <jean-philippe.clem...@fr.thalesgroup.com
    <mailto:jean-philippe.clem...@fr.thalesgroup.com>>:____

    Well, I’m not too sure to understand :)____

     ____

    I will only focus on bundle 0 and the stopping event I’m expecting
    from it. So, I put “that” bundle listener on the bundle 0 not on any
    other bundle. But, the way I get the bundle 0 is via a bundleContext
    which is injected via Blueprint.____

     ____

    That said, the spec says I should first get the event. Should not be
    tied to the actual stopping order, isn’t it (Blueprint or not)?____

    __ __

    The STOPPING events are distributed to various listeners
    synchronously.  The blueprint extender will receive it before your
    own listener (because it's registered before), so the preemptive
    shutdown will take place before you can do anything.  This also
    means that your bundle containing the listener will be stopped
    before receiving the STOPPING event.____

    Note that the purpose of this behavior is to avoid having blueprint
    bundles trying to call a missing mandatory dependency during
    shutdown, amongst other things.  If your bundle stuck waiting for a
    blueprint dependency or something else ?  There is certainly still
    some room for improvements in blueprint-core when shutting down
    bundles.____

    __ __

     ____

         ____

        Regards,____

        JP____

         ____

        *De :*Guillaume Nodet [mailto:gno...@apache.org
        <mailto:gno...@apache.org>]
        *Envoyé :* vendredi 18 novembre 2016 17:04____


        *À :* user
        *Objet :* Re: Karaf shutdown____

         ____

        You have 2 events for each bundle (but for the system bundle,
        the first event which is a STOPPING event).____

        For each of those 2 events, the first one is a STOPPING, then
        the framework calls the activator stop, then fire a STOPPED
        event.____

         ____

        Are you sure you're using activators ? because the problem may
        be completely different if using a blueprint extender and a
        blueprint event that does not stop.____

        In that case, what happens is that the blueprint extender
        intercepts the STOPPING event from the system bundle and
        preemptively shutdown all blueprint bundles in an orderly
        fashion.  This usually helps by choosing a better stopping order
        than the default one, depending on service used between
        bundles.  The problem is that given the extender stops the
        blueprint bundles preemptively, the bundles will be stopped
        before the framework has a change to send the STOPPING event for
        the bundle.  It's actually the same as if you would stop the
        blueprint extender in a running framework: all blueprint
        containers will be destroyed, but the lifecycle of the bundles
        would not be affected.  This behavior can be turned off by using____

          *org.apache.aries.blueprint.preemptiveShutdown=false*____

        in the etc/config.properties file.____

         ____

        2016-11-18 16:49 GMT+01:00 CLEMENT Jean-Philippe
        <jean-philippe.clem...@fr.thalesgroup.com
        <mailto:jean-philippe.clem...@fr.thalesgroup.com>>:____

        In fact I guess that’s the problem, the event seems not to be
        fired before stopping bundles.____

         ____

        When all bundles react normally, i.e. they stop, I get the
        stopping event. When at least one does not stop as expected,
        then I get no stopping event. Would you be kind enough to try
        again with a non-stopping-bundle and displaying the event type?____

         ____

        Regards,____

        JP____

         ____

        *De :*Guillaume Nodet [mailto:gno...@apache.org
        <mailto:gno...@apache.org>]
        *Envoyé :* vendredi 18 novembre 2016 16:40____


        *À :* user
        *Objet :* Re: Karaf shutdown____

         ____

         ____

         ____

        2016-11-18 15:01 GMT+01:00 CLEMENT Jean-Philippe
        <jean-philippe.clem...@fr.thalesgroup.com
        <mailto:jean-philippe.clem...@fr.thalesgroup.com>>:____

        Did you also add a bundle which prevents stopping ?____

         ____

        No, but the first event is sent before actually stopping any
        bundle.____

         ____

             ____

            PS: in your traces which event is the shutdown itself?____

         ____

        It's not, because I paste a wrong output.____

        Here's the full output, and the interesting event is the first
        one.  It's should be a */STOPPING /*event.____

         ____

        *karaf*@root()> ($.contextbundle 0)stop 1



         
org.osgi.framework.BundleEvent[source=org.eclipse.osgi_3.10.101.v20150820-1432
        [0]]____

        org.osgi.framework.BundleEvent[source=org.jline_3.0.1 [34]]____

        org.osgi.framework.BundleEvent[source=org.jline_3.0.1 [34]]____

        org.osgi.framework.BundleEvent[source=org.fusesource.jansi_1.14.0 
[33]]____

        org.osgi.framework.BundleEvent[source=org.fusesource.jansi_1.14.0 
[33]]____

        org.osgi.framework.BundleEvent[source=org.apache.sshd.core_1.2.0
        [32]]____

        org.osgi.framework.BundleEvent[source=org.apache.sshd.core_1.2.0
        [32]]____

        
org.osgi.framework.BundleEvent[source=org.apache.servicemix.bundles.not-yet-commons-ssl_0.3.11.1
        [31]]____

        
org.osgi.framework.BundleEvent[source=org.apache.servicemix.bundles.not-yet-commons-ssl_0.3.11.1
        [31]]____

        ...____

         ____

             ____

            Regards,____

            JP____

             ____

            *De :*Guillaume Nodet [mailto:gno...@apache.org
            <mailto:gno...@apache.org>]
            *Envoyé :* vendredi 18 novembre 2016 14:26____


            *À :* user
            *Objet :* Re: Karaf shutdown____

             ____

            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()> ($.contextbundle 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
            <mailto: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
            <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
            <mailto: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
            <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
            <mailto:jean-philippe.clem...@fr.thalesgroup.com>>:____

            Is there a way to get a shutdown event?____

             ____

            Regards,____

            JP____

             ____

            *De :*CLEMENT Jean-Philippe
            [mailto:jean-philippe.clem...@fr.thalesgroup.com
            <mailto:jean-philippe.clem...@fr.thalesgroup.com>]
            *Envoyé :* jeudi 17 novembre 2016 17:03
            *À :* user@karaf.apache.org <mailto: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 <mailto:gno...@redhat.com>
            Web: http://fusesource.com <http://fusesource.com/>
            Blog: http://gnodet.blogspot.com/
            <http://gnodet.blogspot.com/>____

             ____



            ____

             ____

            -- ____

            ------------------------
            Guillaume Nodet
            ------------------------____

            Red Hat, Open Source Integration____

             ____

            Email: gno...@redhat.com <mailto:gno...@redhat.com>
            Web: http://fusesource.com <http://fusesource.com/>
            Blog: http://gnodet.blogspot.com/
            <http://gnodet.blogspot.com/>____

             ____



            ____

             ____

            -- ____

            ------------------------
            Guillaume Nodet
            ------------------------____

            Red Hat, Open Source Integration____

             ____

            Email: gno...@redhat.com <mailto:gno...@redhat.com>
            Web: http://fusesource.com <http://fusesource.com/>
            Blog: http://gnodet.blogspot.com/
            <http://gnodet.blogspot.com/>____

             ____



        ____

         ____

        -- ____

        ------------------------
        Guillaume Nodet
        ------------------------____

        Red Hat, Open Source Integration____

         ____

        Email: gno...@redhat.com <mailto:gno...@redhat.com>
        Web: http://fusesource.com <http://fusesource.com/>
        Blog: http://gnodet.blogspot.com/ <http://gnodet.blogspot.com/>____

         ____



        ____

         ____

        -- ____

        ------------------------
        Guillaume Nodet
        ------------------------____

        Red Hat, Open Source Integration____

         ____

        Email: gno...@redhat.com <mailto:gno...@redhat.com>
        Web: http://fusesource.com <http://fusesource.com/>
        Blog: http://gnodet.blogspot.com/ <http://gnodet.blogspot.com/>____

         ____



    ____

    __ __

    -- ____

    ------------------------
    Guillaume Nodet
    ------------------------____

    Red Hat, Open Source Integration____

    __ __

    Email: gno...@redhat.com <mailto:gno...@redhat.com>
    Web: http://fusesource.com <http://fusesource.com/>
    Blog: http://gnodet.blogspot.com/ <http://gnodet.blogspot.com/>____

    __ __




--
------------------------
Guillaume Nodet
------------------------
Red Hat, Open Source Integration

Email: gno...@redhat.com <mailto:gno...@redhat.com>
Web: http://fusesource.com <http://fusesource.com/>
Blog: http://gnodet.blogspot.com/


--
Jean-Baptiste Onofré
jbono...@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Reply via email to