Announcement Announcement Module
No announcement yet.
JVM does not terminate when using JmsTemplate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JVM does not terminate when using JmsTemplate


    sorry, this is probably a newbie question ...

    I'm currently learning how to use JMS. I'm using the new JMS book from O'Reilly for that. At the moment I'm reading the JmsTemplate parts of the book.

    Here's my problem:

    When I use a JmsTemplate (as explained in the book examples) to send a Message everything works as expected. The message is send to my JMS provider (ActiveMQ 5.2). But the JVM never terminates and keeps running. In the Eclipse Debug perspective the following threads are listed as running:

    1 ActiveMQ transport thread
    2 inactivity monitors daemons
    1 DestroyJavaVM thread

    When I use System.exit(0), the program terminates.

    For testing I used a Spring 3.0 snapshot release (337).

    Can you help? Thanks.


  • #2
    The information you're looking for can be found (at least for version 2.5.x of spring) in the reference manual.

    section: Shutting down the Spring IoC container gracefully in non-web applications

    and here it is copied and pasted for your convenience, though I recommend checking this document out next time!!!

    If you are using Spring's IoC container in a non-web application environment, for example in a rich client desktop environment, and you want the container to shutdown gracefully and call the relevant destroy callbacks on your singleton beans, you will need to register a shutdown hook with the JVM. This is quite easy to do (see below), and will ensure that your Spring IoC container shuts down gracefully and that all resources held by your singletons are released. Of course it is still up to you to both configure the destroy callbacks for your singletons and implement such destroy callbacks correctly.

    So to register a shutdown hook that enables the graceful shutdown of the relevant Spring IoC container, you simply need to call the registerShutdownHook() method that is declared on the AbstractApplicationContext class. To wit...

    public final class Boot {
        public static void main(final String[] args) throws Exception {
            AbstractApplicationContext ctx
                = new ClassPathXmlApplicationContext(new String []{"beans.xml"});
            // add a shutdown hook for the above context... 
            // app runs here...
            // main method exits, hook is called prior to the app shutting down...


    • #3
      That is, that should call the appropriate methods to close the context. You may want to add tags to catch @PostCreate/@PreDestroy annotations or you may want to assign the init-method and destroy-method properties on some of your beans.

      I'm not sure if this will work automatically with the JMS components or not but if it doesn't registration through init/destroy-method and annotations should work. You can look at the JMS code yourself to double check (or just try the code). Look for any "Bean"-aware or "Spring"-aware interfaces or annotations and you'll be able to see what's going on.


      • #4

        thanks soks86.

        I tried the registerShutdownHook tip (so far I only developed spring web apps). This seems to be a hot candidate.

        But it has no effect.

        In my bean context I only use these classes:

        org.springframework.jms.connection.CachingConnecti onFactory stinationResolver

        So - no classes I can edit. I tried to use default-destroy-method in the beans tag but with no effect.

        Any ideas?




        • #5
          Hi there,

          here I got the same problem! I am using ActiveMQ and the same template classes. Registering a shutdown hook does not work when my jvm is closing.

          Interesting so: When i use "appCtx.close()" right before my main methods exits, the Spring IoC container is gracefully shut down and all JMS threads are closed...

          So, any ideas?