Results 1 to 4 of 4

Thread: JVM termination, daemon threads

  1. #1
    Join Date
    Jul 2007
    Posts
    10

    Default JVM termination, daemon threads

    Day 1 of Spring Integration here...I've got a simple directory monitor working but the JVM doesn't exit when I interrupt the main thread with Control-C. I tried specifying daemon=false as described here but that had no effect.

    My configuration (with failed attempt at daemon=false) is as follows. What configuration do I need to make the JVM exit?

    Code:
    	
    	<int:channel id="fileChannel" datatype="java.io.File">
    		<int:dispatcher task-executor="fileChannelTaskExecutor" />
    	</int:channel>
    
    	<bean id="fileChannelTaskExecutor"
    		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    		<property name="corePoolSize" value="2"/>
    		<property name="daemon" value="false"/>
    	</bean>	
    
    	<int:poller default="true" max-messages-per-poll="1"
    		fixed-rate="1000" />
    
    	<int-file:inbound-channel-adapter 
    		directory="file:c:/watch" channel="fileChannel" prevent-duplicates="true" />
    
    	<int:outbound-channel-adapter channel="fileChannel" 
    		ref="target" method="handleMessage" >
    	</int:outbound-channel-adapter>
    
    	<bean id="target" class="...

  2. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,036

    Default

    Can you attach a thread dump? (I think it's Ctrl-Break on Windows, but you can use jstack or VisualVM too).
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  3. #3
    Join Date
    Sep 2012
    Location
    Medford, MA
    Posts
    11

    Default

    I have no problems with CTRL-C ; but I am an using a JVM Shutdown and a Spring Context Listener Shutdown hook. The
    JVM Shutdown will tell Spring to close... The Spring Context listener will tell the Sched/Executor to passivate. Seems to work for me!


    Code:
    class Main {
      private static ClassPathXmlApplicationContext springCtx;
      public static void main(String [] args) {
    
        // Create a global shutdown handler to take down the Spring Container
        Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    if (springCtx != null) {
                        springCtx.close();
                    }
                    log.info("*** Started Spring shutdown...");
                }
            });
    
            // Bootstrap Spring Container
            springCtx = new ClassPathXmlApplicationContext(new String[]{
                        "nbpump-spring-context.xml",
                        "activemq-jms-config.xml"
                    });
            // When the Spring Container is told to shutdown, add a Listener to terminate the Task Scheduler and its thread pool
            springCtx.addApplicationListener(new TaskSchedulerShutdownHandler());
           //
           //  The rest of your main method goes here
      }
    
      // Task Shutdown handler - uses static Spring Context to locate Task Scheduler and Executor and take it down easy.
    static class TaskSchedulerShutdownHandler implements ApplicationListener<ContextClosedEvent> {
            public void onApplicationEvent(ContextClosedEvent event) {
                ThreadPoolTaskExecutor executor = springCtx.getBean(ThreadPoolTaskExecutor.class);
                ThreadPoolTaskScheduler scheduler = springCtx.getBean(ThreadPoolTaskScheduler.class);
                if (executor != null) {
                    executor.shutdown();
                }
                if (scheduler != null) {
                    scheduler.shutdown();
                }
                log.info("*** Shutdown Spring scheduler and executor...");
            }
        }

  4. #4
    Join Date
    Jul 2007
    Posts
    10

    Default

    Quote Originally Posted by Gary Russell View Post
    Can you attach a thread dump? (I think it's Ctrl-Break on Windows, but you can use jstack or VisualVM too).
    Thanks for the push in the right direction; it turns out this had nothing to do with threads.
    I had been starting the windows cmd shell with Cygwin shell as parent process. Turns out
    I can't interrupt any Java process with a cmd shell started that way. If I start cmd shell
    the ordinary way, everything works fine.

Posting Permissions

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