4 Replies Latest reply on Mar 25, 2013 2:56 PM by wl880504

    Is there a general way to implement stopping a bundle?

    wl880504

      I have created a OSGi bundle from mavenized floodlight. I use blueprint config.xml to activate the bundle. Here is the blueprint config.xml:

       

         

       

      I create a class FloodlightBean which provides init() and stop() methods to start and stop the bundle:

       

      public class FloodlightBean {

       

          public void init() throws FloodlightModuleException {

              System.out.println("start floodlight");

              // Setup logger

              System.setProperty("org.restlet.engine.loggerFacadeClass",

                      "org.restlet.ext.slf4j.Slf4jLoggerFacade");

       

              CmdLineSettings settings = new CmdLineSettings();

              /*CmdLineParser parser = new CmdLineParser(settings);

              try {

                  parser.parseArgument(args);

              } catch (CmdLineException e) {

                  parser.printUsage(System.out);

                  System.exit(1);

              }*/

       

              // Load modules

              FloodlightModuleLoader fml = new FloodlightModuleLoader();

              IFloodlightModuleContext moduleContext = fml

                      .loadModulesFromConfig(settings.getModuleFile());

              // Run REST server

              IRestApiService restApi = moduleContext

                      .getServiceImpl(IRestApiService.class);

              restApi.run();

              // Run the main floodlight module

              IFloodlightProviderService controller = moduleContext

                      .getServiceImpl(IFloodlightProviderService.class);

              // This call blocks, it has to be the last line in the main

              controller.run();

          }

       

          public void destroy() {

       

              System.out.println("stop floodlight");

          }

      }

       

      Once the bundle is started, it runs forever. I have tried:

       

      public void destroy() {

       

              System.exit(0);

          }

       

      But this will cause Fuse esb to exit too. When I restart Fues ESB, the bundle runs forever again. So I am wondering if there is a general way to implement destroy() to stop a running process without making Fuse ESB exit.

        • 1. Re: Is there a general way to implement stopping a bundle?
          ffang

          Hi,

           

          You can use bundle Activator instead.

           

          In the Activator start method, you can start a thread to run the controller, and in the Activator stop method, you can do some cleanup job(like end the thread you started).

           

          Never use System.exit(0) in OSGi container, as it will terminate whole process

           

          Freeman

          • 2. Re: Is there a general way to implement stopping a bundle?
            wl880504

            Hi Freeman,

             

            I just realize that the problem is not only about the controller.run method. Before controller.run() is executed, many threads are already invoked using ScheduledExecutorService.

             

            I want to know how to stop the process with multiple running threads in osgi. Like in command line, I can use Ctrl + c to stop the floodlight process.

            • 3. Re: Is there a general way to implement stopping a bundle?
              ffang

              As I already mentioned, you can have an OSGi activator appended to your bundle, where you can do any cleanup job in stop method in that activator, so when you use osgi:stop your_bundle_id, the stop method in that activator get invoked, that provide a graceful way to shutdown your bundle.

               

              I really can't understand why you wanna kill whole process when shutdown a single bundle.

               

              OSGi container is a process basically, you can install any bundles in this container, and manage the bundle's lifecycle gracefully. 

               

              Freeman

              • 4. Re: Is there a general way to implement stopping a bundle?
                wl880504

                I will make it clearer. Floodlight is an existing java project. My research is to move it to OSGi container and do some work. It is developed as a multi-thread project and it does not provide any stop methods to stop it. Most of the threads runs forever once get started.

                 

                It is kind of a big project and right now I am not familiar with how it implements multiple threads. Thus the problem is that I do not know how to implement specific cleanup job to stop it by myself.

                 

                So I am wondering if OSGi provides any mechanism to help find and stop these threads.