Results 1 to 4 of 4

Thread: Register Shutdown Hook Behavior Diffrent- in stanlone & Spring DM

  1. #1
    Join Date
    Nov 2008
    Location
    London,UK
    Posts
    299

    Default Register Shutdown Hook Behavior Diffrent- in stanlone & Spring DM

    I have my initialization like this

    AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("")

    In one of my beans i have a init method which starts thread and a destroy method which has a sysout.

    public void init() {
    thr = new MyThread(service);
    thr.start();
    }


    public void cleanup()
    {
    System.out.println("CLEAN UP BEING CALLED ");

    }


    I register a shutdown hook as follows

    appContext.registerShutdownHook();


    Test cases
    ------------

    In my standalone use cases
    --------------------------
    I get the destroy methods called only after teh threads execution is complete

    In Dm Continer
    ---------------

    When i delete the bundle, destroy method gets called , and thread still continues to run.

    Isn't the behavior different in nature ?

  2. #2
    Join Date
    Oct 2008
    Posts
    493

    Default

    In the standalone case, what's causing destroy to be called? If you're shutting down the VM then it's most likely the shutdown hook that's calling it during JVM shutdown, at which point the thread that you started will also be terminated as part of the VM's shutdown processing.

    In the dm Server case destroy is being called as part of closing the application context for the bundle. This happens as part of the bundle's stop processing. During this shutdown there's nothing in place to stop the thread that your bean started.

    To ensure that this works reliably, and without having to rely upon VM shutdown, I would recommend that, in your bean's destroy method, you update a flag that's frequently checked by MyThread to tell it to stop running.
    Andy Wilkinson
    SpringSource

  3. #3
    Join Date
    Nov 2008
    Location
    London,UK
    Posts
    299

    Default

    I am exactly following the step u said within the bundle inside Dm server, that is the destroy method sets a flag which is checked in the threads run method .

    But my problem is that I cannot use the same code in my standalone applications. This is because my destroy methods gets called only after the threads execution is complete ..

    What do i do so that the same code is used in both places ?

  4. #4
    Join Date
    Nov 2008
    Location
    London,UK
    Posts
    299

    Default

    To be more precise , since we are migrating to Spring DM gradually i want the DM behavior in standalone code also , that is i want my destroy method to be called where i can do my thread related cleanup.

    Since destroy method itself is waiting for thread execution to complete . i am stuck :-( . Please help me out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •